Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassSchedule.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
14 use Magento\AsynchronousOperations\Api\Data\ItemStatusInterfaceFactory;
16 use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterfaceFactory;
20 use Psr\Log\LoggerInterface;
23 
30 {
34  private $identityService;
35 
39  private $asyncResponseFactory;
40 
44  private $itemStatusInterfaceFactory;
45 
49  private $bulkManagement;
50 
54  private $logger;
55 
59  private $operationRepository;
60 
64  private $userContext;
65 
77  public function __construct(
78  IdentityGeneratorInterface $identityService,
79  ItemStatusInterfaceFactory $itemStatusInterfaceFactory,
80  AsyncResponseInterfaceFactory $asyncResponseFactory,
81  BulkManagementInterface $bulkManagement,
82  LoggerInterface $logger,
83  OperationRepository $operationRepository,
84  UserContextInterface $userContext = null
85  ) {
86  $this->identityService = $identityService;
87  $this->itemStatusInterfaceFactory = $itemStatusInterfaceFactory;
88  $this->asyncResponseFactory = $asyncResponseFactory;
89  $this->bulkManagement = $bulkManagement;
90  $this->logger = $logger;
91  $this->operationRepository = $operationRepository;
92  $this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
93  }
94 
106  public function publishMass($topicName, array $entitiesArray, $groupId = null, $userId = null)
107  {
108  $bulkDescription = __('Topic %1', $topicName);
109 
110  if ($userId == null) {
111  $userId = $this->userContext->getUserId();
112  }
113 
114  if ($groupId == null) {
115  $groupId = $this->identityService->generateId();
116 
118  if (!$this->bulkManagement->scheduleBulk($groupId, [], $bulkDescription, $userId)) {
119  throw new LocalizedException(
120  __('Something went wrong while processing the request.')
121  );
122  }
123  }
124 
125  $operations = [];
126  $requestItems = [];
127  $bulkException = new BulkException();
128  foreach ($entitiesArray as $key => $entityParams) {
130  $requestItem = $this->itemStatusInterfaceFactory->create();
131 
132  try {
133  $operations[] = $this->operationRepository->createByTopic($topicName, $entityParams, $groupId);
134  $requestItem->setId($key);
135  $requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
136  $requestItems[] = $requestItem;
137  } catch (\Exception $exception) {
138  $this->logger->error($exception);
139  $requestItem->setId($key);
140  $requestItem->setStatus(ItemStatusInterface::STATUS_REJECTED);
141  $requestItem->setErrorMessage($exception);
142  $requestItem->setErrorCode($exception);
143  $requestItems[] = $requestItem;
144  $bulkException->addException(new LocalizedException(
145  __('Error processing %key element of input data', ['key' => $key]),
146  $exception
147  ));
148  }
149  }
150 
151  if (!$this->bulkManagement->scheduleBulk($groupId, $operations, $bulkDescription, $userId)) {
152  throw new LocalizedException(
153  __('Something went wrong while processing the request.')
154  );
155  }
157  $asyncResponse = $this->asyncResponseFactory->create();
158  $asyncResponse->setBulkUuid($groupId);
159  $asyncResponse->setRequestItems($requestItems);
160 
161  if ($bulkException->wasErrorAdded()) {
162  $asyncResponse->setErrors(true);
163  $bulkException->addData($asyncResponse);
164  throw $bulkException;
165  } else {
166  $asyncResponse->setErrors(false);
167  }
168 
169  return $asyncResponse;
170  }
171 }
$operations
Definition: bulk.php:55
__construct(IdentityGeneratorInterface $identityService, ItemStatusInterfaceFactory $itemStatusInterfaceFactory, AsyncResponseInterfaceFactory $asyncResponseFactory, BulkManagementInterface $bulkManagement, LoggerInterface $logger, OperationRepository $operationRepository, UserContextInterface $userContext=null)
__()
Definition: __.php:13
$logger