|
2 | 2 |
|
3 | 3 | namespace Algolia\AlgoliaSearch\Model\Indexer;
|
4 | 4 |
|
| 5 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 6 | +use Magento\Catalog\Model\Product as ProductModel; |
5 | 7 | use Magento\Catalog\Model\Product\Action;
|
| 8 | +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; |
6 | 9 | use Magento\Framework\Indexer\IndexerRegistry;
|
7 |
| -use Magento\Framework\Model\AbstractModel; |
8 | 10 |
|
9 | 11 | class ProductObserver
|
10 | 12 | {
|
| 13 | + /** @var Product */ |
11 | 14 | private $indexer;
|
12 | 15 |
|
13 |
| - public function __construct(IndexerRegistry $indexerRegistry) |
| 16 | + /** @var ConfigHelper */ |
| 17 | + private $configHelper; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param IndexerRegistry $indexerRegistry |
| 21 | + * @param ConfigHelper $configHelper |
| 22 | + */ |
| 23 | + public function __construct(IndexerRegistry $indexerRegistry, ConfigHelper $configHelper) |
14 | 24 | {
|
15 | 25 | $this->indexer = $indexerRegistry->get('algolia_products');
|
| 26 | + $this->configHelper = $configHelper; |
16 | 27 | }
|
17 | 28 |
|
18 |
| - public function aroundSave( |
19 |
| - \Magento\Catalog\Model\ResourceModel\Product $productResource, |
20 |
| - \Closure $proceed, |
21 |
| - AbstractModel $product |
22 |
| - ) { |
| 29 | + /** |
| 30 | + * Using "before" method here instead of "after", because M2.1 doesn't pass "$product" argument |
| 31 | + * to "after" methods. When M2.1 support will be removed, this method can be rewriten to: |
| 32 | + * afterSave(ProductResource $productResource, ProductResource $result, ProductModel $product) |
| 33 | + * |
| 34 | + * @param ProductResource $productResource |
| 35 | + * @param ProductModel $product |
| 36 | + * |
| 37 | + * @return ProductModel[] |
| 38 | + */ |
| 39 | + public function beforeSave(ProductResource $productResource, ProductModel $product) |
| 40 | + { |
23 | 41 | $productResource->addCommitCallback(function () use ($product) {
|
24 |
| - if (!$this->indexer->isScheduled()) { |
| 42 | + if (!$this->indexer->isScheduled() || $this->configHelper->isQueueActive()) { |
25 | 43 | $this->indexer->reindexRow($product->getId());
|
26 | 44 | }
|
27 | 45 | });
|
28 | 46 |
|
29 |
| - return $proceed($product); |
| 47 | + return [$product]; |
30 | 48 | }
|
31 | 49 |
|
32 |
| - public function aroundDelete( |
33 |
| - \Magento\Catalog\Model\ResourceModel\Product $productResource, |
34 |
| - \Closure $proceed, |
35 |
| - AbstractModel $product |
36 |
| - ) { |
| 50 | + /** |
| 51 | + * Using "before" method here instead of "after", because M2.1 doesn't pass "$product" argument |
| 52 | + * to "after" methods. When M2.1 support will be removed, this method can be rewriten to: |
| 53 | + * public function afterDelete(ProductResource $productResource, ProductResource $result, ProductModel $product) |
| 54 | + * |
| 55 | + * @param ProductResource $productResource |
| 56 | + * @param ProductModel $product |
| 57 | + * |
| 58 | + * @return ProductModel[] |
| 59 | + */ |
| 60 | + public function beforeDelete(ProductResource $productResource, ProductModel $product) |
| 61 | + { |
37 | 62 | $productResource->addCommitCallback(function () use ($product) {
|
38 |
| - if (!$this->indexer->isScheduled()) { |
| 63 | + if (!$this->indexer->isScheduled() || $this->configHelper->isQueueActive()) { |
39 | 64 | $this->indexer->reindexRow($product->getId());
|
40 | 65 | }
|
41 | 66 | });
|
42 | 67 |
|
43 |
| - return $proceed($product); |
| 68 | + return [$product]; |
44 | 69 | }
|
45 | 70 |
|
46 |
| - public function aroundUpdateAttributes( |
47 |
| - Action $subject, |
48 |
| - \Closure $closure, |
49 |
| - array $productIds, |
50 |
| - array $attrData, |
51 |
| - $storeId |
52 |
| - ) { |
53 |
| - $result = $closure($productIds, $attrData, $storeId); |
54 |
| - if (!$this->indexer->isScheduled()) { |
| 71 | + /** |
| 72 | + * @param Action $subject |
| 73 | + * @param Action|null $result |
| 74 | + * @param array $productIds |
| 75 | + * |
| 76 | + * @return Action |
| 77 | + */ |
| 78 | + public function afterUpdateAttributes(Action $subject, Action $result = null, $productIds) |
| 79 | + { |
| 80 | + if (!$this->indexer->isScheduled() || $this->configHelper->isQueueActive()) { |
55 | 81 | $this->indexer->reindexList(array_unique($productIds));
|
56 | 82 | }
|
57 | 83 |
|
58 | 84 | return $result;
|
59 | 85 | }
|
60 | 86 |
|
61 |
| - public function aroundUpdateWebsites( |
62 |
| - Action $subject, |
63 |
| - \Closure $closure, |
64 |
| - array $productIds, |
65 |
| - array $websiteIds, |
66 |
| - $type |
67 |
| - ) { |
68 |
| - $result = $closure($productIds, $websiteIds, $type); |
69 |
| - if (!$this->indexer->isScheduled()) { |
| 87 | + /** |
| 88 | + * @param Action $subject |
| 89 | + * @param Action|null $result |
| 90 | + * @param array $productIds |
| 91 | + * |
| 92 | + * @return mixed |
| 93 | + */ |
| 94 | + public function afterUpdateWebsites(Action $subject, Action $result = null, array $productIds) |
| 95 | + { |
| 96 | + if (!$this->indexer->isScheduled() || $this->configHelper->isQueueActive()) { |
70 | 97 | $this->indexer->reindexList(array_unique($productIds));
|
71 | 98 | }
|
72 | 99 |
|
|
0 commit comments