Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkInventoryTransfer.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 
23 {
27  private $bulkInventoryTransferValidator;
28 
32  private $bulkInventoryTransfer;
33 
37  private $getProductIdsBySkus;
38 
42  private $legacyIndexer;
43 
47  private $defaultSourceProvider;
48 
52  private $sourceIndexer;
53 
64  public function __construct(
65  BulkInventoryTransferValidatorInterface $inventoryTransferValidator,
66  BulkInventoryTransferResource $bulkInventoryTransfer,
67  SourceIndexer $sourceIndexer,
68  DefaultSourceProviderInterface $defaultSourceProvider,
69  GetProductIdsBySkusInterface $getProductIdsBySkus,
70  LegacyIndexer $legacyIndexer
71  ) {
72  $this->bulkInventoryTransferValidator = $inventoryTransferValidator;
73  $this->bulkInventoryTransfer = $bulkInventoryTransfer;
74  $this->getProductIdsBySkus = $getProductIdsBySkus;
75  $this->legacyIndexer = $legacyIndexer;
76  $this->defaultSourceProvider = $defaultSourceProvider;
77  $this->sourceIndexer = $sourceIndexer;
78  }
79 
84  private function reindexLegacy(array $productIds): void
85  {
86  $this->legacyIndexer->executeList($productIds);
87  }
88 
93  public function execute(
94  array $skus,
95  string $originSource,
96  string $destinationSource,
97  bool $unassignFromOrigin
98  ): bool {
99  $validationResult = $this->bulkInventoryTransferValidator->validate(
100  $skus,
101  $originSource,
102  $destinationSource
103  );
104 
105  if (!$validationResult->isValid()) {
106  throw new ValidationException(__('Validation Failed'), null, 0, $validationResult);
107  }
108 
109  $this->bulkInventoryTransfer->execute(
110  $skus,
111  $originSource,
112  $destinationSource,
113  $unassignFromOrigin
114  );
115 
116  $this->sourceIndexer->executeList([$originSource, $destinationSource]);
117 
118  if (($this->defaultSourceProvider->getCode() === $originSource) ||
119  ($this->defaultSourceProvider->getCode() === $destinationSource)) {
120  $productIds = array_values($this->getProductIdsBySkus->execute($skus));
121  $this->reindexLegacy($productIds);
122  }
123 
124  return true;
125  }
126 }
__()
Definition: __.php:13
foreach($websiteCodes as $websiteCode) $skus
execute(array $skus, string $originSource, string $destinationSource, bool $unassignFromOrigin)
__construct(BulkInventoryTransferValidatorInterface $inventoryTransferValidator, BulkInventoryTransferResource $bulkInventoryTransfer, SourceIndexer $sourceIndexer, DefaultSourceProviderInterface $defaultSourceProvider, GetProductIdsBySkusInterface $getProductIdsBySkus, LegacyIndexer $legacyIndexer)