Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkTransferPost.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 use Psr\Log\LoggerInterface;
19 
20 class BulkTransferPost extends Action
21 {
25  const ADMIN_RESOURCE = 'Magento_Catalog::products';
26 
30  private $bulkSessionProductsStorage;
31 
35  private $bulkInventoryTransfer;
36 
40  private $bulkOperationsConfig;
41 
45  private $authSession;
46 
50  private $massSchedule;
51 
55  private $logger;
56 
66  public function __construct(
67  Action\Context $context,
68  BulkInventoryTransferInterface $bulkInventoryTransfer,
69  BulkSessionProductsStorage $bulkSessionProductsStorage,
70  BulkOperationsConfig $bulkOperationsConfig,
71  LoggerInterface $logger,
72  MassSchedule $massSchedule
73  ) {
74  parent::__construct($context);
75 
76  $this->bulkSessionProductsStorage = $bulkSessionProductsStorage;
77  $this->bulkInventoryTransfer = $bulkInventoryTransfer;
78  $this->authSession = $context->getAuth();
79  $this->bulkOperationsConfig = $bulkOperationsConfig;
80  $this->massSchedule = $massSchedule;
81  $this->logger = $logger;
82  }
83 
92  private function runSynchronousOperation(
93  array $skus,
94  string $originSource,
95  string $destinationSource,
96  bool $unassignSource
97  ): void {
98  $count = $this->bulkInventoryTransfer->execute($skus, $originSource, $destinationSource, $unassignSource);
99  $this->messageManager->addSuccessMessage(__('Bulk operation was successful: %count inventory transfers.', [
100  'count' => $count
101  ]));
102  }
103 
113  private function runAsynchronousOperation(
114  array $skus,
115  string $originSource,
116  string $destinationSource,
117  bool $unassignSource
118  ): void {
119  $batchSize = $this->bulkOperationsConfig->getBatchSize();
120  $userId = (int) $this->authSession->getUser()->getId();
121 
122  $skusChunks = array_chunk($skus, $batchSize);
123  $operations = [];
124  foreach ($skusChunks as $skuChunk) {
125  $operations[] = [
126  'skus' => $skuChunk,
127  'originSource' => $originSource,
128  'destinationSource' => $destinationSource,
129  'unassignFromOrigin' => $unassignSource,
130  ];
131  }
132 
133  $this->massSchedule->publishMass(
134  'async.V1.inventory.bulk-product-source-transfer.POST',
135  $operations,
136  null,
137  $userId
138  );
139 
140  $this->messageManager->addSuccessMessage(__('Your request was successfully queued for asynchronous execution'));
141  }
142 
146  public function execute()
147  {
148  $originSource = $this->getRequest()->getParam('origin_source', '');
149  $destinationSource = $this->getRequest()->getParam('destination_source', '');
150 
151  $skus = $this->bulkSessionProductsStorage->getProductsSkus();
152  $unassignSource = (bool) $this->getRequest()->getParam('unassign_origin_source', false);
153 
154  $async = $this->bulkOperationsConfig->isAsyncEnabled();
155 
156  try {
157  if ($async) {
158  $this->runAsynchronousOperation($skus, $originSource, $destinationSource, $unassignSource);
159  } else {
160  $this->runSynchronousOperation($skus, $originSource, $destinationSource, $unassignSource);
161  }
162  } catch (\Exception $e) {
163  $this->logger->error($e->getMessage());
164  $this->messageManager->addErrorMessage(__('Something went wrong during the operation.'));
165  }
166 
167  $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
168  return $result->setPath('catalog/product/index');
169  }
170 }
$operations
Definition: bulk.php:55
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
foreach($websiteCodes as $websiteCode) $skus
$logger
__construct(Action\Context $context, BulkInventoryTransferInterface $bulkInventoryTransfer, BulkSessionProductsStorage $bulkSessionProductsStorage, BulkOperationsConfig $bulkOperationsConfig, LoggerInterface $logger, MassSchedule $massSchedule)
execute()
const ADMIN_RESOURCE