MassSchedule.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AsynchronousOperations\Model;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\DataObject\IdentityGeneratorInterface;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\AsynchronousOperations\Api\Data\ItemStatusInterfaceFactory;
  12. use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterface;
  13. use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterfaceFactory;
  14. use Magento\AsynchronousOperations\Api\Data\ItemStatusInterface;
  15. use Magento\Framework\Bulk\BulkManagementInterface;
  16. use Magento\Framework\Exception\BulkException;
  17. use Psr\Log\LoggerInterface;
  18. use Magento\AsynchronousOperations\Model\ResourceModel\Operation\OperationRepository;
  19. use Magento\Authorization\Model\UserContextInterface;
  20. /**
  21. * Class MassSchedule used for adding multiple entities as Operations to Bulk Management with the status tracking
  22. *
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Suppressed without refactoring to not introduce BiC
  24. */
  25. class MassSchedule
  26. {
  27. /**
  28. * @var \Magento\Framework\DataObject\IdentityGeneratorInterface
  29. */
  30. private $identityService;
  31. /**
  32. * @var AsyncResponseInterfaceFactory
  33. */
  34. private $asyncResponseFactory;
  35. /**
  36. * @var ItemStatusInterfaceFactory
  37. */
  38. private $itemStatusInterfaceFactory;
  39. /**
  40. * @var \Magento\Framework\Bulk\BulkManagementInterface
  41. */
  42. private $bulkManagement;
  43. /**
  44. * @var LoggerInterface
  45. */
  46. private $logger;
  47. /**
  48. * @var OperationRepository
  49. */
  50. private $operationRepository;
  51. /**
  52. * @var \Magento\Authorization\Model\UserContextInterface
  53. */
  54. private $userContext;
  55. /**
  56. * Initialize dependencies.
  57. *
  58. * @param IdentityGeneratorInterface $identityService
  59. * @param ItemStatusInterfaceFactory $itemStatusInterfaceFactory
  60. * @param AsyncResponseInterfaceFactory $asyncResponseFactory
  61. * @param BulkManagementInterface $bulkManagement
  62. * @param LoggerInterface $logger
  63. * @param OperationRepository $operationRepository
  64. * @param UserContextInterface $userContext
  65. */
  66. public function __construct(
  67. IdentityGeneratorInterface $identityService,
  68. ItemStatusInterfaceFactory $itemStatusInterfaceFactory,
  69. AsyncResponseInterfaceFactory $asyncResponseFactory,
  70. BulkManagementInterface $bulkManagement,
  71. LoggerInterface $logger,
  72. OperationRepository $operationRepository,
  73. UserContextInterface $userContext = null
  74. ) {
  75. $this->identityService = $identityService;
  76. $this->itemStatusInterfaceFactory = $itemStatusInterfaceFactory;
  77. $this->asyncResponseFactory = $asyncResponseFactory;
  78. $this->bulkManagement = $bulkManagement;
  79. $this->logger = $logger;
  80. $this->operationRepository = $operationRepository;
  81. $this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
  82. }
  83. /**
  84. * Schedule new bulk operation based on the list of entities
  85. *
  86. * @param string $topicName
  87. * @param array $entitiesArray
  88. * @param string $groupId
  89. * @param string $userId
  90. * @return AsyncResponseInterface
  91. * @throws BulkException
  92. * @throws LocalizedException
  93. */
  94. public function publishMass($topicName, array $entitiesArray, $groupId = null, $userId = null)
  95. {
  96. $bulkDescription = __('Topic %1', $topicName);
  97. if ($userId == null) {
  98. $userId = $this->userContext->getUserId();
  99. }
  100. if ($groupId == null) {
  101. $groupId = $this->identityService->generateId();
  102. /** create new bulk without operations */
  103. if (!$this->bulkManagement->scheduleBulk($groupId, [], $bulkDescription, $userId)) {
  104. throw new LocalizedException(
  105. __('Something went wrong while processing the request.')
  106. );
  107. }
  108. }
  109. $operations = [];
  110. $requestItems = [];
  111. $bulkException = new BulkException();
  112. foreach ($entitiesArray as $key => $entityParams) {
  113. /** @var \Magento\AsynchronousOperations\Api\Data\ItemStatusInterface $requestItem */
  114. $requestItem = $this->itemStatusInterfaceFactory->create();
  115. try {
  116. $operations[] = $this->operationRepository->createByTopic($topicName, $entityParams, $groupId);
  117. $requestItem->setId($key);
  118. $requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
  119. $requestItems[] = $requestItem;
  120. } catch (\Exception $exception) {
  121. $this->logger->error($exception);
  122. $requestItem->setId($key);
  123. $requestItem->setStatus(ItemStatusInterface::STATUS_REJECTED);
  124. $requestItem->setErrorMessage($exception);
  125. $requestItem->setErrorCode($exception);
  126. $requestItems[] = $requestItem;
  127. $bulkException->addException(new LocalizedException(
  128. __('Error processing %key element of input data', ['key' => $key]),
  129. $exception
  130. ));
  131. }
  132. }
  133. if (!$this->bulkManagement->scheduleBulk($groupId, $operations, $bulkDescription, $userId)) {
  134. throw new LocalizedException(
  135. __('Something went wrong while processing the request.')
  136. );
  137. }
  138. /** @var AsyncResponseInterface $asyncResponse */
  139. $asyncResponse = $this->asyncResponseFactory->create();
  140. $asyncResponse->setBulkUuid($groupId);
  141. $asyncResponse->setRequestItems($requestItems);
  142. if ($bulkException->wasErrorAdded()) {
  143. $asyncResponse->setErrors(true);
  144. $bulkException->addData($asyncResponse);
  145. throw $bulkException;
  146. } else {
  147. $asyncResponse->setErrors(false);
  148. }
  149. return $asyncResponse;
  150. }
  151. }