InvalidateAfterEnablingOrDisablingSourcePlugin.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\Framework\Indexer\IndexerRegistry;
  9. use Magento\InventoryApi\Api\Data\SourceInterface;
  10. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  11. use Magento\InventoryIndexer\Indexer\InventoryIndexer;
  12. use Magento\InventoryIndexer\Model\ResourceModel\IsInvalidationRequiredForSource;
  13. /**
  14. * Invalidate Inventory Indexer after Source was enabled or disabled.
  15. */
  16. class InvalidateAfterEnablingOrDisablingSourcePlugin
  17. {
  18. /**
  19. * @var IndexerRegistry
  20. */
  21. private $indexerRegistry;
  22. /**
  23. * @var IsInvalidationRequiredForSource
  24. */
  25. private $isInvalidationRequiredForSource;
  26. /**
  27. * @param IndexerRegistry $indexerRegistry
  28. * @param IsInvalidationRequiredForSource $isInvalidationRequiredForSource
  29. */
  30. public function __construct(
  31. IndexerRegistry $indexerRegistry,
  32. IsInvalidationRequiredForSource $isInvalidationRequiredForSource
  33. ) {
  34. $this->indexerRegistry = $indexerRegistry;
  35. $this->isInvalidationRequiredForSource = $isInvalidationRequiredForSource;
  36. }
  37. /**
  38. * Invalidate Inventory Indexer after Source was enabled or disabled.
  39. *
  40. * @param SourceRepositoryInterface $subject
  41. * @param callable $proceed
  42. * @param SourceInterface $source
  43. * @return void
  44. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  45. */
  46. public function aroundSave(
  47. SourceRepositoryInterface $subject,
  48. callable $proceed,
  49. SourceInterface $source
  50. ) {
  51. $invalidationRequired = false;
  52. if ($source->getSourceCode()) {
  53. $invalidationRequired = $this->isInvalidationRequiredForSource->execute(
  54. $source->getSourceCode(),
  55. (bool)$source->isEnabled()
  56. );
  57. }
  58. $proceed($source);
  59. if ($invalidationRequired) {
  60. $indexer = $this->indexerRegistry->get(InventoryIndexer::INDEXER_ID);
  61. if ($indexer->isValid()) {
  62. $indexer->invalidate();
  63. }
  64. }
  65. }
  66. }