6
6
7
7
namespace Magento \Framework \Mview ;
8
8
9
+ use InvalidArgumentException ;
10
+ use Magento \Framework \DataObject ;
9
11
use Magento \Framework \Mview \View \ChangelogTableNotExistsException ;
10
12
use Magento \Framework \Mview \View \SubscriptionFactory ;
13
+ use Exception ;
14
+ use Magento \Framework \Mview \View \SubscriptionInterface ;
11
15
12
16
/**
13
17
* Mview
14
18
*
15
19
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16
20
*/
17
- class View extends \ Magento \ Framework \ DataObject implements ViewInterface
21
+ class View extends DataObject implements ViewInterface
18
22
{
19
23
/**
20
24
* Default batch size for partial reindex
@@ -52,7 +56,7 @@ class View extends \Magento\Framework\DataObject implements ViewInterface
52
56
protected $ subscriptionFactory ;
53
57
54
58
/**
55
- * @var \Magento\Framework\Mview\ View\StateInterface
59
+ * @var View\StateInterface
56
60
*/
57
61
protected $ state ;
58
62
@@ -113,7 +117,7 @@ public function setId($id)
113
117
/**
114
118
* Id field name setter
115
119
*
116
- * @param string $name
120
+ * @param string $name
117
121
* @return $this
118
122
*/
119
123
public function setIdFieldName ($ name )
@@ -167,13 +171,13 @@ public function getSubscriptions()
167
171
*
168
172
* @param string $viewId
169
173
* @return ViewInterface
170
- * @throws \ InvalidArgumentException
174
+ * @throws InvalidArgumentException
171
175
*/
172
176
public function load ($ viewId )
173
177
{
174
178
$ view = $ this ->config ->getView ($ viewId );
175
- if (empty ($ view ) || empty ($ view ['view_id ' ]) || $ view ['view_id ' ] != $ viewId ) {
176
- throw new \ InvalidArgumentException ("{$ viewId } view does not exist. " );
179
+ if (empty ($ view ) || empty ($ view ['view_id ' ]) || $ view ['view_id ' ] !== $ viewId ) {
180
+ throw new InvalidArgumentException ("{$ viewId } view does not exist. " );
177
181
}
178
182
179
183
$ this ->setId ($ viewId );
@@ -185,37 +189,21 @@ public function load($viewId)
185
189
/**
186
190
* Create subscriptions
187
191
*
188
- * @throws \Exception
189
192
* @return ViewInterface
193
+ * @throws Exception
190
194
*/
191
195
public function subscribe ()
192
196
{
193
- if ($ this ->getState ()->getMode () != View \StateInterface::MODE_ENABLED ) {
194
- try {
195
- // Create changelog table
196
- $ this ->getChangelog ()->create ();
197
-
198
- // Create subscriptions
199
- foreach ($ this ->getSubscriptions () as $ subscriptionConfig ) {
200
- /** @var \Magento\Framework\Mview\View\SubscriptionInterface $subscription */
201
- $ subscriptionInstance = $ this ->subscriptionFactory ->create (
202
- [
203
- 'view ' => $ this ,
204
- 'tableName ' => $ subscriptionConfig ['name ' ],
205
- 'columnName ' => $ subscriptionConfig ['column ' ],
206
- 'subscriptionModel ' => !empty ($ subscriptionConfig ['subscription_model ' ])
207
- ? $ subscriptionConfig ['subscription_model ' ]
208
- : SubscriptionFactory::INSTANCE_NAME ,
209
- ]
210
- );
211
- $ subscriptionInstance ->create ();
212
- }
197
+ if ($ this ->getState ()->getMode () !== View \StateInterface::MODE_ENABLED ) {
198
+ // Create changelog table
199
+ $ this ->getChangelog ()->create ();
213
200
214
- // Update view state
215
- $ this ->getState ()->setMode (View \StateInterface::MODE_ENABLED )->save ();
216
- } catch (\Exception $ e ) {
217
- throw $ e ;
201
+ foreach ($ this ->getSubscriptions () as $ subscriptionConfig ) {
202
+ $ this ->initSubscriptionInstance ($ subscriptionConfig )->create ();
218
203
}
204
+
205
+ // Update view state
206
+ $ this ->getState ()->setMode (View \StateInterface::MODE_ENABLED )->save ();
219
207
}
220
208
221
209
return $ this ;
@@ -224,34 +212,19 @@ public function subscribe()
224
212
/**
225
213
* Remove subscriptions
226
214
*
227
- * @throws \Exception
228
215
* @return ViewInterface
216
+ * @throws Exception
229
217
*/
230
218
public function unsubscribe ()
231
219
{
232
220
if ($ this ->getState ()->getMode () != View \StateInterface::MODE_DISABLED ) {
233
- try {
234
- // Remove subscriptions
235
- foreach ($ this ->getSubscriptions () as $ subscriptionConfig ) {
236
- /** @var \Magento\Framework\Mview\View\SubscriptionInterface $subscription */
237
- $ subscriptionInstance = $ this ->subscriptionFactory ->create (
238
- [
239
- 'view ' => $ this ,
240
- 'tableName ' => $ subscriptionConfig ['name ' ],
241
- 'columnName ' => $ subscriptionConfig ['column ' ],
242
- 'subscriptionModel ' => !empty ($ subscriptionConfig ['subscriptionModel ' ])
243
- ? $ subscriptionConfig ['subscriptionModel ' ]
244
- : SubscriptionFactory::INSTANCE_NAME ,
245
- ]
246
- );
247
- $ subscriptionInstance ->remove ();
248
- }
249
-
250
- // Update view state
251
- $ this ->getState ()->setMode (View \StateInterface::MODE_DISABLED )->save ();
252
- } catch (\Exception $ e ) {
253
- throw $ e ;
221
+ // Remove subscriptions
222
+ foreach ($ this ->getSubscriptions () as $ subscriptionConfig ) {
223
+ $ this ->initSubscriptionInstance ($ subscriptionConfig )->remove ();
254
224
}
225
+
226
+ // Update view state
227
+ $ this ->getState ()->setMode (View \StateInterface::MODE_DISABLED )->save ();
255
228
}
256
229
257
230
return $ this ;
@@ -261,64 +234,68 @@ public function unsubscribe()
261
234
* Materialize view by IDs in changelog
262
235
*
263
236
* @return void
264
- * @throws \ Exception
237
+ * @throws Exception
265
238
*/
266
239
public function update ()
267
240
{
268
- if ($ this ->getState ()->getStatus () == View \StateInterface::STATUS_IDLE ) {
269
- try {
270
- $ currentVersionId = $ this ->getChangelog ()->getVersion ();
271
- } catch (ChangelogTableNotExistsException $ e ) {
272
- return ;
273
- }
274
- $ lastVersionId = (int ) $ this ->getState ()->getVersionId ();
275
- $ action = $ this ->actionFactory ->get ($ this ->getActionClass ());
276
-
277
- try {
278
- $ this ->getState ()->setStatus (View \StateInterface::STATUS_WORKING )->save ();
279
-
280
- $ versionBatchSize = self ::$ maxVersionQueryBatch ;
281
- $ batchSize = isset ($ this ->changelogBatchSize [$ this ->getChangelog ()->getViewId ()])
282
- ? $ this ->changelogBatchSize [$ this ->getChangelog ()->getViewId ()]
283
- : self ::DEFAULT_BATCH_SIZE ;
284
-
285
- for ($ vsFrom = $ lastVersionId ; $ vsFrom < $ currentVersionId ; $ vsFrom += $ versionBatchSize ) {
286
- // Don't go past the current version for atomicy.
287
- $ versionTo = min ($ currentVersionId , $ vsFrom + $ versionBatchSize );
288
- $ ids = $ this ->getChangelog ()->getList ($ vsFrom , $ versionTo );
289
-
290
- // We run the actual indexer in batches.
291
- // Chunked AFTER loading to avoid duplicates in separate chunks.
292
- $ chunks = array_chunk ($ ids , $ batchSize );
293
- foreach ($ chunks as $ ids ) {
294
- $ action ->execute ($ ids );
295
- }
296
- }
241
+ if ($ this ->getState ()->getStatus () !== View \StateInterface::STATUS_IDLE ) {
242
+ return ;
243
+ }
244
+
245
+ try {
246
+ $ currentVersionId = $ this ->getChangelog ()->getVersion ();
247
+ } catch (ChangelogTableNotExistsException $ e ) {
248
+ return ;
249
+ }
250
+
251
+ $ lastVersionId = (int )$ this ->getState ()->getVersionId ();
252
+ $ action = $ this ->actionFactory ->get ($ this ->getActionClass ());
297
253
298
- $ this ->getState ()->loadByView ($ this ->getId ());
299
- $ statusToRestore = $ this ->getState ()->getStatus () == View \StateInterface::STATUS_SUSPENDED
300
- ? View \StateInterface::STATUS_SUSPENDED
301
- : View \StateInterface::STATUS_IDLE ;
302
- $ this ->getState ()->setVersionId ($ currentVersionId )->setStatus ($ statusToRestore )->save ();
303
- } catch (\Exception $ exception ) {
304
- $ this ->getState ()->loadByView ($ this ->getId ());
305
- $ statusToRestore = $ this ->getState ()->getStatus () == View \StateInterface::STATUS_SUSPENDED
306
- ? View \StateInterface::STATUS_SUSPENDED
307
- : View \StateInterface::STATUS_IDLE ;
308
- $ this ->getState ()->setStatus ($ statusToRestore )->save ();
309
- throw $ exception ;
254
+ try {
255
+ $ this ->getState ()->setStatus (View \StateInterface::STATUS_WORKING )->save ();
256
+
257
+ $ versionBatchSize = self ::$ maxVersionQueryBatch ;
258
+ $ batchSize = isset ($ this ->changelogBatchSize [$ this ->getChangelog ()->getViewId ()])
259
+ ? $ this ->changelogBatchSize [$ this ->getChangelog ()->getViewId ()]
260
+ : self ::DEFAULT_BATCH_SIZE ;
261
+
262
+ for ($ vsFrom = $ lastVersionId ; $ vsFrom < $ currentVersionId ; $ vsFrom += $ versionBatchSize ) {
263
+ // Don't go past the current version for atomicy.
264
+ $ versionTo = min ($ currentVersionId , $ vsFrom + $ versionBatchSize );
265
+ $ ids = $ this ->getChangelog ()->getList ($ vsFrom , $ versionTo );
266
+
267
+ // We run the actual indexer in batches.
268
+ // Chunked AFTER loading to avoid duplicates in separate chunks.
269
+ $ chunks = array_chunk ($ ids , $ batchSize );
270
+ foreach ($ chunks as $ ids ) {
271
+ $ action ->execute ($ ids );
272
+ }
310
273
}
274
+
275
+ $ this ->getState ()->loadByView ($ this ->getId ());
276
+ $ statusToRestore = $ this ->getState ()->getStatus () === View \StateInterface::STATUS_SUSPENDED
277
+ ? View \StateInterface::STATUS_SUSPENDED
278
+ : View \StateInterface::STATUS_IDLE ;
279
+ $ this ->getState ()->setVersionId ($ currentVersionId )->setStatus ($ statusToRestore )->save ();
280
+ } catch (Exception $ exception ) {
281
+ $ this ->getState ()->loadByView ($ this ->getId ());
282
+ $ statusToRestore = $ this ->getState ()->getStatus () === View \StateInterface::STATUS_SUSPENDED
283
+ ? View \StateInterface::STATUS_SUSPENDED
284
+ : View \StateInterface::STATUS_IDLE ;
285
+ $ this ->getState ()->setStatus ($ statusToRestore )->save ();
286
+ throw $ exception ;
311
287
}
312
288
}
313
289
314
290
/**
315
291
* Suspend view updates and set version ID to changelog's end
316
292
*
317
293
* @return void
294
+ * @throws Exception
318
295
*/
319
296
public function suspend ()
320
297
{
321
- if ($ this ->getState ()->getMode () == View \StateInterface::MODE_ENABLED ) {
298
+ if ($ this ->getState ()->getMode () === View \StateInterface::MODE_ENABLED ) {
322
299
$ state = $ this ->getState ();
323
300
$ state ->setVersionId ($ this ->getChangelog ()->getVersion ());
324
301
$ state ->setStatus (View \StateInterface::STATUS_SUSPENDED );
@@ -330,11 +307,12 @@ public function suspend()
330
307
* Resume view updates
331
308
*
332
309
* @return void
310
+ * @throws Exception
333
311
*/
334
312
public function resume ()
335
313
{
336
314
$ state = $ this ->getState ();
337
- if ($ state ->getStatus () == View \StateInterface::STATUS_SUSPENDED ) {
315
+ if ($ state ->getStatus () === View \StateInterface::STATUS_SUSPENDED ) {
338
316
$ state ->setStatus (View \StateInterface::STATUS_IDLE );
339
317
$ state ->save ();
340
318
}
@@ -347,7 +325,7 @@ public function resume()
347
325
*/
348
326
public function clearChangelog ()
349
327
{
350
- if ($ this ->getState ()->getMode () == View \StateInterface::MODE_ENABLED ) {
328
+ if ($ this ->getState ()->getMode () === View \StateInterface::MODE_ENABLED ) {
351
329
$ this ->getChangelog ()->clear ($ this ->getState ()->getVersionId ());
352
330
}
353
331
}
@@ -384,7 +362,7 @@ public function setState(View\StateInterface $state)
384
362
*/
385
363
public function isEnabled ()
386
364
{
387
- return $ this ->getState ()->getMode () == View \StateInterface::MODE_ENABLED ;
365
+ return $ this ->getState ()->getMode () === View \StateInterface::MODE_ENABLED ;
388
366
}
389
367
390
368
/**
@@ -394,7 +372,7 @@ public function isEnabled()
394
372
*/
395
373
public function isIdle ()
396
374
{
397
- return $ this ->getState ()->getStatus () == \ Magento \ Framework \ Mview \ View \StateInterface::STATUS_IDLE ;
375
+ return $ this ->getState ()->getStatus () === View \StateInterface::STATUS_IDLE ;
398
376
}
399
377
400
378
/**
@@ -404,7 +382,7 @@ public function isIdle()
404
382
*/
405
383
public function isWorking ()
406
384
{
407
- return $ this ->getState ()->getStatus () == \ Magento \ Framework \ Mview \ View \StateInterface::STATUS_WORKING ;
385
+ return $ this ->getState ()->getStatus () === View \StateInterface::STATUS_WORKING ;
408
386
}
409
387
410
388
/**
@@ -414,7 +392,7 @@ public function isWorking()
414
392
*/
415
393
public function isSuspended ()
416
394
{
417
- return $ this ->getState ()->getStatus () == \ Magento \ Framework \ Mview \ View \StateInterface::STATUS_SUSPENDED ;
395
+ return $ this ->getState ()->getStatus () === View \StateInterface::STATUS_SUSPENDED ;
418
396
}
419
397
420
398
/**
@@ -439,4 +417,24 @@ public function getChangelog()
439
417
}
440
418
return $ this ->changelog ;
441
419
}
420
+
421
+ /**
422
+ * Initializes Subscription instance
423
+ *
424
+ * @param array $subscriptionConfig
425
+ * @return SubscriptionInterface
426
+ */
427
+ private function initSubscriptionInstance (array $ subscriptionConfig ): SubscriptionInterface
428
+ {
429
+ return $ this ->subscriptionFactory ->create (
430
+ [
431
+ 'view ' => $ this ,
432
+ 'tableName ' => $ subscriptionConfig ['name ' ],
433
+ 'columnName ' => $ subscriptionConfig ['column ' ],
434
+ 'subscriptionModel ' => !empty ($ subscriptionConfig ['subscription_model ' ])
435
+ ? $ subscriptionConfig ['subscription_model ' ]
436
+ : SubscriptionFactory::INSTANCE_NAME ,
437
+ ]
438
+ );
439
+ }
442
440
}
0 commit comments