InitializeIndexerState.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Setup\Patch\Data;
  7. use Magento\Framework\Encryption\Encryptor;
  8. use Magento\Framework\Encryption\EncryptorInterface;
  9. use Magento\Framework\Indexer\StateInterface;
  10. use Magento\Framework\Json\EncoderInterface;
  11. use Magento\Framework\Indexer\ConfigInterface;
  12. use Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory;
  13. use Magento\Indexer\Model\Indexer\State;
  14. use Magento\Indexer\Model\Indexer\StateFactory;
  15. use Magento\Framework\App\ResourceConnection;
  16. use Magento\Framework\Setup\Patch\DataPatchInterface;
  17. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  18. /**
  19. * Class InitializeIndexerState
  20. * @package Magento\Indexer\Setup\Patch
  21. */
  22. class InitializeIndexerState implements DataPatchInterface, PatchVersionInterface
  23. {
  24. /**
  25. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  26. */
  27. private $moduleDataSetup;
  28. /**
  29. * @var CollectionFactory
  30. */
  31. private $statesFactory;
  32. /**
  33. * @var StateFactory
  34. */
  35. private $stateFactory;
  36. /**
  37. * @var ConfigInterface
  38. */
  39. private $config;
  40. /**
  41. * @var EncryptorInterface
  42. */
  43. private $encryptor;
  44. /**
  45. * @var EncoderInterface
  46. */
  47. private $encoder;
  48. /**
  49. * PatchInitial constructor.
  50. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  51. */
  52. public function __construct(
  53. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  54. CollectionFactory $statesFactory,
  55. StateFactory $stateFactory,
  56. ConfigInterface $config,
  57. EncryptorInterface $encryptor,
  58. EncoderInterface $encoder
  59. ) {
  60. $this->moduleDataSetup = $moduleDataSetup;
  61. $this->statesFactory = $statesFactory;
  62. $this->stateFactory = $stateFactory;
  63. $this->config = $config;
  64. $this->encryptor = $encryptor;
  65. $this->encoder = $encoder;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function apply()
  71. {
  72. /** @var State[] $stateIndexers */
  73. $stateIndexers = [];
  74. $states = $this->statesFactory->create();
  75. foreach ($states->getItems() as $state) {
  76. /** @var State $state */
  77. $stateIndexers[$state->getIndexerId()] = $state;
  78. }
  79. foreach ($this->config->getIndexers() as $indexerId => $indexerConfig) {
  80. $hash = $this->encryptor->hash($this->encoder->encode($indexerConfig), Encryptor::HASH_VERSION_MD5);
  81. if (isset($stateIndexers[$indexerId])) {
  82. $stateIndexers[$indexerId]->setHashConfig($hash);
  83. $stateIndexers[$indexerId]->save();
  84. } else {
  85. /** @var State $state */
  86. $state = $this->stateFactory->create();
  87. $state->loadByIndexer($indexerId);
  88. $state->setHashConfig($hash);
  89. $state->setStatus(StateInterface::STATUS_INVALID);
  90. $state->save();
  91. }
  92. }
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public static function getDependencies()
  98. {
  99. return [];
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public static function getVersion()
  105. {
  106. return '2.1.0';
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getAliases()
  112. {
  113. return [];
  114. }
  115. }