BulkNotificationManagement.php 4.74 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\AsynchronousOperations\Model;

use Magento\Framework\App\ResourceConnection;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory as BulkCollectionFactory;
use Magento\Framework\Data\Collection;

/**
 * Class for bulk notification manager
 */
class BulkNotificationManagement
{
    /**
     * @var MetadataPool
     */
    private $metadataPool;

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    /**
     * @var BulkCollectionFactory
     */
    private $bulkCollectionFactory;

    /**
     * BulkManagement constructor.
     *
     * @param MetadataPool $metadataPool
     * @param ResourceConnection $resourceConnection
     * @param BulkCollectionFactory $bulkCollectionFactory
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        MetadataPool $metadataPool,
        ResourceConnection $resourceConnection,
        BulkCollectionFactory $bulkCollectionFactory,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->metadataPool = $metadataPool;
        $this->resourceConnection = $resourceConnection;
        $this->bulkCollectionFactory = $bulkCollectionFactory;
        $this->logger = $logger;
    }

    /**
     * Mark given bulks as acknowledged.
     * Notifications related to these bulks will not appear in notification area.
     *
     * @param array $bulkUuids
     * @return bool true on success or false on failure
     */
    public function acknowledgeBulks(array $bulkUuids)
    {
        $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
        $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());

        try {
            $connection->insertArray(
                $this->resourceConnection->getTableName('magento_acknowledged_bulk'),
                ['bulk_uuid'],
                $bulkUuids
            );
        } catch (\Exception $exception) {
            $this->logger->critical($exception->getMessage());
            return false;
        }
        return true;
    }

    /**
     * Remove given bulks from acknowledged list.
     * Notifications related to these bulks will appear again in notification area.
     *
     * @param array $bulkUuids
     * @return bool true on success or false on failure
     */
    public function ignoreBulks(array $bulkUuids)
    {
        $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
        $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());

        try {
            $connection->delete(
                $this->resourceConnection->getTableName('magento_acknowledged_bulk'),
                ['bulk_uuid IN(?)' => $bulkUuids]
            );
        } catch (\Exception $exception) {
            $this->logger->critical($exception->getMessage());
            return false;
        }
        return true;
    }

    /**
     * Retrieve all bulks that were acknowledged by given user.
     *
     * @param int $userId
     * @return BulkSummaryInterface[]
     */
    public function getAcknowledgedBulksByUser($userId)
    {
        $bulks = $this->bulkCollectionFactory->create()
            ->join(
                ['acknowledged_bulk' => $this->resourceConnection->getTableName('magento_acknowledged_bulk')],
                'main_table.uuid = acknowledged_bulk.bulk_uuid',
                []
            )->addFieldToFilter('user_id', $userId)
            ->addOrder('start_time', Collection::SORT_ORDER_DESC)
            ->getItems();

        return $bulks;
    }

    /**
     * Retrieve all bulks that were not acknowledged by given user.
     *
     * @param int $userId
     * @return BulkSummaryInterface[]
     */
    public function getIgnoredBulksByUser($userId)
    {
        /** @var \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection $bulkCollection */
        $bulkCollection = $this->bulkCollectionFactory->create();
        $bulkCollection->getSelect()->joinLeft(
            ['acknowledged_bulk' => $this->resourceConnection->getTableName('magento_acknowledged_bulk')],
            'main_table.uuid = acknowledged_bulk.bulk_uuid',
            ['acknowledged_bulk.bulk_uuid']
        );
        $bulks = $bulkCollection->addFieldToFilter('user_id', $userId)
            ->addFieldToFilter('acknowledged_bulk.bulk_uuid', ['null' => true])
            ->addOrder('start_time', Collection::SORT_ORDER_DESC)
            ->getItems();

        return $bulks;
    }
}