Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InitializeIndexerState.php
Go to the documentation of this file.
1 <?php
8 
14 use Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory;
16 use Magento\Indexer\Model\Indexer\StateFactory;
20 
26 {
30  private $moduleDataSetup;
31 
35  private $statesFactory;
36 
40  private $stateFactory;
41 
45  private $config;
46 
50  private $encryptor;
51 
55  private $encoder;
56 
61  public function __construct(
62  \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
63  CollectionFactory $statesFactory,
64  StateFactory $stateFactory,
65  ConfigInterface $config,
66  EncryptorInterface $encryptor,
67  EncoderInterface $encoder
68  ) {
69  $this->moduleDataSetup = $moduleDataSetup;
70  $this->statesFactory = $statesFactory;
71  $this->stateFactory = $stateFactory;
72  $this->config = $config;
73  $this->encryptor = $encryptor;
74  $this->encoder = $encoder;
75  }
76 
80  public function apply()
81  {
83  $stateIndexers = [];
84  $states = $this->statesFactory->create();
85  foreach ($states->getItems() as $state) {
87  $stateIndexers[$state->getIndexerId()] = $state;
88  }
89 
90  foreach ($this->config->getIndexers() as $indexerId => $indexerConfig) {
91  $hash = $this->encryptor->hash($this->encoder->encode($indexerConfig), Encryptor::HASH_VERSION_MD5);
92  if (isset($stateIndexers[$indexerId])) {
93  $stateIndexers[$indexerId]->setHashConfig($hash);
94  $stateIndexers[$indexerId]->save();
95  } else {
97  $state = $this->stateFactory->create();
98  $state->loadByIndexer($indexerId);
99  $state->setHashConfig($hash);
100  $state->setStatus(StateInterface::STATUS_INVALID);
101  $state->save();
102  }
103  }
104  }
105 
109  public static function getDependencies()
110  {
111  return [];
112  }
113 
117  public static function getVersion()
118  {
119  return '2.1.0';
120  }
121 
125  public function getAliases()
126  {
127  return [];
128  }
129 }
$config
Definition: fraud_order.php:17
__construct(\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup, CollectionFactory $statesFactory, StateFactory $stateFactory, ConfigInterface $config, EncryptorInterface $encryptor, EncoderInterface $encoder)