ReindexAfterSourceItemsDeletePlugin.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\InventoryIndexer\Plugin\InventoryApi;
  8. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  9. use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
  10. use Magento\InventoryIndexer\Indexer\Source\SourceIndexer;
  11. /**
  12. * Reindex after source items delete plugin
  13. */
  14. class ReindexAfterSourceItemsDeletePlugin
  15. {
  16. /**
  17. * @var SourceIndexer
  18. */
  19. private $sourceIndexer;
  20. /**
  21. * @param SourceIndexer $sourceIndexer
  22. */
  23. public function __construct(SourceIndexer $sourceIndexer)
  24. {
  25. $this->sourceIndexer = $sourceIndexer;
  26. }
  27. /**
  28. * @param SourceItemsDeleteInterface $subject
  29. * @param callable $proceed
  30. * @param SourceItemInterface[] $sourceItems
  31. * @return void
  32. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  33. */
  34. public function aroundExecute(
  35. SourceItemsDeleteInterface $subject,
  36. callable $proceed,
  37. array $sourceItems
  38. ) {
  39. $sourceCodes = [];
  40. foreach ($sourceItems as $sourceItem) {
  41. $sourceCodes[] = $sourceItem->getSourceCode();
  42. }
  43. $proceed($sourceItems);
  44. if (count($sourceCodes)) {
  45. $this->sourceIndexer->executeList($sourceCodes);
  46. }
  47. }
  48. }