FlushCacheByProductIds.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\InventoryCache\Model;
  8. use Magento\Framework\EntityManager\EventManager;
  9. use Magento\Framework\Indexer\CacheContext;
  10. /**
  11. * Clean cache for given product ids.
  12. */
  13. class FlushCacheByProductIds
  14. {
  15. /**
  16. * @var CacheContext
  17. */
  18. private $cacheContext;
  19. /**
  20. * @var EventManager
  21. */
  22. private $eventManager;
  23. /**
  24. * @var string
  25. */
  26. private $productCacheTag;
  27. /**
  28. * @param CacheContext $cacheContext
  29. * @param EventManager $eventManager
  30. * @param string $productCacheTag
  31. */
  32. public function __construct(
  33. CacheContext $cacheContext,
  34. EventManager $eventManager,
  35. string $productCacheTag
  36. ) {
  37. $this->cacheContext = $cacheContext;
  38. $this->eventManager = $eventManager;
  39. $this->productCacheTag = $productCacheTag;
  40. }
  41. /**
  42. * Clean cache for given product ids.
  43. *
  44. * @param array $productIds
  45. * @return void
  46. */
  47. public function execute(array $productIds)
  48. {
  49. if ($productIds) {
  50. $this->cacheContext->registerEntities($this->productCacheTag, $productIds);
  51. $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
  52. }
  53. }
  54. }