14
14
use Magento \Framework \Config \Data \ConfigData ;
15
15
use Magento \Framework \Config \File \ConfigFilePool ;
16
16
use Magento \Framework \Encryption \EncryptorInterface ;
17
+ use Magento \Framework \Encryption \Encryptor ;
17
18
use Magento \Framework \Exception \FileSystemException ;
18
19
use Magento \Framework \Exception \LocalizedException ;
19
20
use Magento \Framework \Filesystem ;
20
21
use Magento \Framework \Filesystem \Directory \WriteInterface ;
21
22
use Magento \Framework \Math \Random ;
22
23
use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
23
24
use Magento \Framework \Model \ResourceModel \Db \Context ;
25
+ use Magento \Framework \Indexer \ConfigInterface ;
26
+ use Magento \Framework \Json \EncoderInterface ;
27
+ use Magento \Indexer \Model \ResourceModel \Indexer \State \CollectionFactory ;
24
28
25
29
/**
26
30
* Encryption key changer resource model
@@ -71,13 +75,37 @@ class Change extends AbstractDb
71
75
*/
72
76
protected $ random ;
73
77
78
+ /**
79
+ * Indexer Config
80
+ *
81
+ * @var IndexerConfig
82
+ */
83
+ protected $ indexerConfig ;
84
+
85
+ /**
86
+ * Json Encoder
87
+ *
88
+ * @var Encoder
89
+ */
90
+ protected $ encoder ;
91
+
92
+ /**
93
+ * Indexer State Collection Factory
94
+ *
95
+ * @var IndexerStateCollection
96
+ */
97
+ protected $ indexerStateCollection ;
98
+
74
99
/**
75
100
* @param Context $context
76
101
* @param Filesystem $filesystem
77
102
* @param Structure $structure
78
103
* @param EncryptorInterface $encryptor
79
104
* @param Writer $writer
80
105
* @param Random $random
106
+ * @param ConfigInterface $indexerConfig
107
+ * @param EncoderInterface $encoder
108
+ * @param CollectionFactory $indexerStateCollection
81
109
* @param string $connectionName
82
110
*/
83
111
public function __construct (
@@ -87,6 +115,9 @@ public function __construct(
87
115
EncryptorInterface $ encryptor ,
88
116
Writer $ writer ,
89
117
Random $ random ,
118
+ ConfigInterface $ indexerConfig ,
119
+ EncoderInterface $ encoder ,
120
+ CollectionFactory $ indexerStateCollection ,
90
121
$ connectionName = null
91
122
) {
92
123
$ this ->encryptor = clone $ encryptor ;
@@ -95,6 +126,9 @@ public function __construct(
95
126
$ this ->structure = $ structure ;
96
127
$ this ->writer = $ writer ;
97
128
$ this ->random = $ random ;
129
+ $ this ->indexerConfig = $ indexerConfig ;
130
+ $ this ->encoder = $ encoder ;
131
+ $ this ->indexerStateCollection = $ indexerStateCollection ;
98
132
}
99
133
100
134
/**
@@ -139,6 +173,7 @@ public function changeEncryptionKey($key = null)
139
173
try {
140
174
$ this ->_reEncryptSystemConfigurationValues ();
141
175
$ this ->_reEncryptCreditCardNumbers ();
176
+ $ this ->_updateIndexersHash ();
142
177
$ this ->writer ->saveConfig ($ configData );
143
178
$ this ->commit ();
144
179
return $ key ;
@@ -207,4 +242,31 @@ protected function _reEncryptCreditCardNumbers()
207
242
);
208
243
}
209
244
}
245
+
246
+ /**
247
+ * Retrieve indexer state and update the hash with new encryption key
248
+ *
249
+ * @return void
250
+ */
251
+ protected function _updateIndexersHash (){
252
+
253
+ $ stateIndexers = [];
254
+ $ stateCollection = $ this ->indexerStateCollection ->create ();
255
+ foreach ($ stateCollection ->getItems () as $ state ) {
256
+ /** @var \Magento\Indexer\Model\Indexer\State $state */
257
+ $ stateIndexers [$ state ->getIndexerId ()] = $ state ;
258
+ }
259
+
260
+ foreach ($ this ->indexerConfig ->getIndexers () as $ indexerId => $ indexerConfig ) {
261
+ $ newHashConfig = $ this ->encryptor ->hash (
262
+ $ this ->encoder ->encode ($ indexerConfig ),
263
+ Encryptor::HASH_VERSION_MD5
264
+ );
265
+
266
+ if (isset ($ stateIndexers [$ indexerId ])) {
267
+ $ stateIndexers [$ indexerId ]->setHashConfig ($ newHashConfig );
268
+ $ stateIndexers [$ indexerId ]->save ();
269
+ }
270
+ }
271
+ }
210
272
}
0 commit comments