Collection.php 2.86 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */
23

24
namespace Adyen\Payment\Model\ResourceModel\Notification;
25

26
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
27
{
28 29 30
    /**
     * Construct
     */
31 32
    public function _construct()
    {
33
        $this->_init('Adyen\Payment\Model\Notification', 'Adyen\Payment\Model\ResourceModel\Notification');
34
    }
35 36 37 38 39 40 41 42 43 44 45 46 47 48

    /**
     * Filter the notifications table to see if there are any unprocessed ones that have been created more than 10 minutes ago
     */
    public function unprocessedNotificationsFilter()
    {
        $dateEnd = new \DateTime();
        $dateEnd->modify('-10 minute');
        $dateRange = ['to' => $dateEnd, 'datetime' => true];
        $this->addFieldToFilter('done', 0);
        $this->addFieldToFilter('processing', 0);
        $this->addFieldToFilter('created_at', $dateRange);
        return $this;
    }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    /**
     * Filter the notifications table to get non processed or done notifications without 5 or more errors older than 
     * 2 minutes but not older than 5 days, ordered by created_at and event_code columns
     *
     * @return $this
     */
    public function notificationsToProcessFilter()
    {
        // execute notifications from 2 minute or earlier because order could not yet been created by magento
        $dateStart = new \DateTime();
        $dateStart->modify('-5 day');
        $dateEnd = new \DateTime();
        $dateEnd->modify('-1 minute');
        $dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];

        $this->addFieldToFilter('done', 0);
        $this->addFieldToFilter('processing', 0);
        $this->addFieldToFilter('created_at', $dateRange);
        $this->addFieldToFilter('error_count', ['lt' => \Adyen\Payment\Model\Notification::MAX_ERROR_COUNT]);

        // Process the notifications in ascending order by creation date and event_code
        $this->getSelect()->order('created_at ASC')->order('event_code ASC');

        return $this;
    }
75
}