AbstractIndexer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Model\Indexer;
  7. use Magento\Framework\Mview\ActionInterface as MviewActionInterface;
  8. use Magento\Framework\Indexer\ActionInterface as IndexerActionInterface;
  9. use Magento\Framework\DataObject\IdentityInterface;
  10. use Magento\Framework\Indexer\CacheContext;
  11. /**
  12. * Abstract class for CatalogRule indexers.
  13. */
  14. abstract class AbstractIndexer implements IndexerActionInterface, MviewActionInterface, IdentityInterface
  15. {
  16. /**
  17. * @var IndexBuilder
  18. */
  19. protected $indexBuilder;
  20. /**
  21. * Application Event Dispatcher
  22. *
  23. * @var \Magento\Framework\Event\ManagerInterface
  24. */
  25. protected $_eventManager;
  26. /**
  27. * @var \Magento\Framework\App\CacheInterface
  28. */
  29. private $cacheManager;
  30. /**
  31. * @var \Magento\Framework\Indexer\CacheContext
  32. */
  33. protected $cacheContext;
  34. /**
  35. * @param IndexBuilder $indexBuilder
  36. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  37. */
  38. public function __construct(
  39. IndexBuilder $indexBuilder,
  40. \Magento\Framework\Event\ManagerInterface $eventManager
  41. ) {
  42. $this->indexBuilder = $indexBuilder;
  43. $this->_eventManager = $eventManager;
  44. }
  45. /**
  46. * Execute materialization on ids entities
  47. *
  48. * @param int[] $ids
  49. * @return void
  50. */
  51. public function execute($ids)
  52. {
  53. $this->executeList($ids);
  54. }
  55. /**
  56. * Execute full indexation
  57. *
  58. * @return void
  59. */
  60. public function executeFull()
  61. {
  62. $this->indexBuilder->reindexFull();
  63. $this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
  64. $this->getCacheManager()->clean($this->getIdentities());
  65. }
  66. /**
  67. * Get affected cache tags
  68. *
  69. * @return array
  70. * @codeCoverageIgnore
  71. */
  72. public function getIdentities()
  73. {
  74. return [
  75. \Magento\Catalog\Model\Category::CACHE_TAG,
  76. \Magento\Catalog\Model\Product::CACHE_TAG,
  77. \Magento\Framework\App\Cache\Type\Block::CACHE_TAG
  78. ];
  79. }
  80. /**
  81. * Execute partial indexation by ID list
  82. *
  83. * @param int[] $ids
  84. * @throws \Magento\Framework\Exception\LocalizedException
  85. * @return void
  86. */
  87. public function executeList(array $ids)
  88. {
  89. if (!$ids) {
  90. throw new \Magento\Framework\Exception\LocalizedException(
  91. __('Could not rebuild index for empty products array')
  92. );
  93. }
  94. $this->doExecuteList($ids);
  95. }
  96. /**
  97. * Execute partial indexation by ID list. Template method
  98. *
  99. * @param int[] $ids
  100. * @return void
  101. */
  102. abstract protected function doExecuteList($ids);
  103. /**
  104. * Execute partial indexation by ID
  105. *
  106. * @param int $id
  107. * @throws \Magento\Framework\Exception\LocalizedException
  108. * @return void
  109. */
  110. public function executeRow($id)
  111. {
  112. if (!$id) {
  113. throw new \Magento\Framework\Exception\LocalizedException(
  114. __('We can\'t rebuild the index for an undefined product.')
  115. );
  116. }
  117. $this->doExecuteRow($id);
  118. }
  119. /**
  120. * Execute partial indexation by ID. Template method
  121. *
  122. * @param int $id
  123. * @throws \Magento\Framework\Exception\LocalizedException
  124. * @return void
  125. */
  126. abstract protected function doExecuteRow($id);
  127. /**
  128. * Get cache manager
  129. *
  130. * @return \Magento\Framework\App\CacheInterface|mixed
  131. * @deprecated 100.0.7
  132. */
  133. private function getCacheManager()
  134. {
  135. if ($this->cacheManager === null) {
  136. $this->cacheManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
  137. \Magento\Framework\App\CacheInterface::class
  138. );
  139. }
  140. return $this->cacheManager;
  141. }
  142. /**
  143. * Get cache context
  144. *
  145. * @return \Magento\Framework\Indexer\CacheContext
  146. * @deprecated 100.0.7
  147. */
  148. protected function getCacheContext()
  149. {
  150. if (!($this->cacheContext instanceof CacheContext)) {
  151. return \Magento\Framework\App\ObjectManager::getInstance()->get(CacheContext::class);
  152. } else {
  153. return $this->cacheContext;
  154. }
  155. }
  156. }