BulkNotificationManagement.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Model;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory as BulkCollectionFactory;
  11. use Magento\Framework\Data\Collection;
  12. /**
  13. * Class for bulk notification manager
  14. */
  15. class BulkNotificationManagement
  16. {
  17. /**
  18. * @var MetadataPool
  19. */
  20. private $metadataPool;
  21. /**
  22. * @var ResourceConnection
  23. */
  24. private $resourceConnection;
  25. /**
  26. * @var \Psr\Log\LoggerInterface
  27. */
  28. private $logger;
  29. /**
  30. * @var BulkCollectionFactory
  31. */
  32. private $bulkCollectionFactory;
  33. /**
  34. * BulkManagement constructor.
  35. *
  36. * @param MetadataPool $metadataPool
  37. * @param ResourceConnection $resourceConnection
  38. * @param BulkCollectionFactory $bulkCollectionFactory
  39. * @param \Psr\Log\LoggerInterface $logger
  40. */
  41. public function __construct(
  42. MetadataPool $metadataPool,
  43. ResourceConnection $resourceConnection,
  44. BulkCollectionFactory $bulkCollectionFactory,
  45. \Psr\Log\LoggerInterface $logger
  46. ) {
  47. $this->metadataPool = $metadataPool;
  48. $this->resourceConnection = $resourceConnection;
  49. $this->bulkCollectionFactory = $bulkCollectionFactory;
  50. $this->logger = $logger;
  51. }
  52. /**
  53. * Mark given bulks as acknowledged.
  54. * Notifications related to these bulks will not appear in notification area.
  55. *
  56. * @param array $bulkUuids
  57. * @return bool true on success or false on failure
  58. */
  59. public function acknowledgeBulks(array $bulkUuids)
  60. {
  61. $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
  62. $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
  63. try {
  64. $connection->insertArray(
  65. $this->resourceConnection->getTableName('magento_acknowledged_bulk'),
  66. ['bulk_uuid'],
  67. $bulkUuids
  68. );
  69. } catch (\Exception $exception) {
  70. $this->logger->critical($exception->getMessage());
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * Remove given bulks from acknowledged list.
  77. * Notifications related to these bulks will appear again in notification area.
  78. *
  79. * @param array $bulkUuids
  80. * @return bool true on success or false on failure
  81. */
  82. public function ignoreBulks(array $bulkUuids)
  83. {
  84. $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
  85. $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
  86. try {
  87. $connection->delete(
  88. $this->resourceConnection->getTableName('magento_acknowledged_bulk'),
  89. ['bulk_uuid IN(?)' => $bulkUuids]
  90. );
  91. } catch (\Exception $exception) {
  92. $this->logger->critical($exception->getMessage());
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * Retrieve all bulks that were acknowledged by given user.
  99. *
  100. * @param int $userId
  101. * @return BulkSummaryInterface[]
  102. */
  103. public function getAcknowledgedBulksByUser($userId)
  104. {
  105. $bulks = $this->bulkCollectionFactory->create()
  106. ->join(
  107. ['acknowledged_bulk' => $this->resourceConnection->getTableName('magento_acknowledged_bulk')],
  108. 'main_table.uuid = acknowledged_bulk.bulk_uuid',
  109. []
  110. )->addFieldToFilter('user_id', $userId)
  111. ->addOrder('start_time', Collection::SORT_ORDER_DESC)
  112. ->getItems();
  113. return $bulks;
  114. }
  115. /**
  116. * Retrieve all bulks that were not acknowledged by given user.
  117. *
  118. * @param int $userId
  119. * @return BulkSummaryInterface[]
  120. */
  121. public function getIgnoredBulksByUser($userId)
  122. {
  123. /** @var \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection $bulkCollection */
  124. $bulkCollection = $this->bulkCollectionFactory->create();
  125. $bulkCollection->getSelect()->joinLeft(
  126. ['acknowledged_bulk' => $this->resourceConnection->getTableName('magento_acknowledged_bulk')],
  127. 'main_table.uuid = acknowledged_bulk.bulk_uuid',
  128. ['acknowledged_bulk.bulk_uuid']
  129. );
  130. $bulks = $bulkCollection->addFieldToFilter('user_id', $userId)
  131. ->addFieldToFilter('acknowledged_bulk.bulk_uuid', ['null' => true])
  132. ->addOrder('start_time', Collection::SORT_ORDER_DESC)
  133. ->getItems();
  134. return $bulks;
  135. }
  136. }