Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkUnassignPost.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
20 use Psr\Log\LoggerInterface;
21 
22 class BulkUnassignPost extends Action
23 {
27  const ADMIN_RESOURCE = 'Magento_Catalog::products';
28 
32  private $bulkSessionProductsStorage;
33 
37  private $bulkSourceUnassign;
38 
42  private $bulkOperationsConfig;
43 
47  private $massSchedule;
48 
52  private $authSession;
53 
57  private $logger;
58 
68  public function __construct(
69  Action\Context $context,
70  BulkSourceUnassignInterface $bulkSourceUnassign,
71  BulkSessionProductsStorage $bulkSessionProductsStorage,
72  BulkOperationsConfig $bulkOperationsConfig,
73  MassSchedule $massSchedule,
74  LoggerInterface $logger
75  ) {
76  parent::__construct($context);
77 
78  $this->bulkSessionProductsStorage = $bulkSessionProductsStorage;
79  $this->bulkSourceUnassign = $bulkSourceUnassign;
80  $this->bulkOperationsConfig = $bulkOperationsConfig;
81  $this->massSchedule = $massSchedule;
82  $this->authSession = $context->getAuth();
83  $this->logger = $logger;
84  }
85 
92  private function runSynchronousOperation(array $skus, array $sourceCodes): void
93  {
94  $count = $this->bulkSourceUnassign->execute($skus, $sourceCodes);
95  $this->messageManager->addSuccessMessage(__('Bulk operation was successful: %count unassignments.', [
96  'count' => $count
97  ]));
98  }
99 
107  private function runAsynchronousOperation(array $skus, array $sourceCodes): void
108  {
109  $batchSize = $this->bulkOperationsConfig->getBatchSize();
110  $userId = (int) $this->authSession->getUser()->getId();
111 
112  $skusChunks = array_chunk($skus, $batchSize);
113  $operations = [];
114  foreach ($skusChunks as $skuChunk) {
115  $operations[] = [
116  'skus' => $skuChunk,
117  'sourceCodes' => $sourceCodes,
118  ];
119  }
120 
121  $this->massSchedule->publishMass(
122  'async.V1.inventory.bulk-product-source-unassign.POST',
123  $operations,
124  null,
125  $userId
126  );
127 
128  $this->messageManager->addSuccessMessage(__('Your request was successfully queued for asynchronous execution'));
129  }
130 
134  public function execute()
135  {
136  $sourceCodes = $this->getRequest()->getParam('sources', []);
137  $skus = $this->bulkSessionProductsStorage->getProductsSkus();
138 
139  $async = $this->bulkOperationsConfig->isAsyncEnabled();
140 
141  try {
142  if ($async) {
143  $this->runAsynchronousOperation($skus, $sourceCodes);
144  } else {
145  $this->runSynchronousOperation($skus, $sourceCodes);
146  }
147  } catch (\Exception $e) {
148  $this->logger->error($e->getMessage());
149  $this->messageManager->addErrorMessage(__('Something went wrong during the operation.'));
150  }
151 
152  $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
153  return $result->setPath('catalog/product/index');
154  }
155 }
$operations
Definition: bulk.php:55
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
foreach($websiteCodes as $websiteCode) $skus
$logger
execute()
const ADMIN_RESOURCE
__construct(Action\Context $context, BulkSourceUnassignInterface $bulkSourceUnassign, BulkSessionProductsStorage $bulkSessionProductsStorage, BulkOperationsConfig $bulkOperationsConfig, MassSchedule $massSchedule, LoggerInterface $logger)