/
Rows.php
237 lines (211 loc) · 7.5 KB
/
Rows.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Indexer\Product\Category\Action;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Config;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Query\Generator as QueryGenerator;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Framework\Indexer\CacheContext;
use Magento\Store\Model\StoreManagerInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractAction
{
/**
* Limitation by products
*
* @var int[]
*/
protected $limitationByProducts;
/**
* @var CacheContext
*/
private $cacheContext;
/**
* @var EventManagerInterface|null
*/
private $eventManager;
/**
* @param ResourceConnection $resource
* @param StoreManagerInterface $storeManager
* @param Config $config
* @param QueryGenerator|null $queryGenerator
* @param MetadataPool|null $metadataPool
* @param CacheContext|null $cacheContext
* @param EventManagerInterface|null $eventManager
*/
public function __construct(
ResourceConnection $resource,
StoreManagerInterface $storeManager,
Config $config,
QueryGenerator $queryGenerator = null,
MetadataPool $metadataPool = null,
CacheContext $cacheContext = null,
EventManagerInterface $eventManager = null
) {
parent::__construct($resource, $storeManager, $config, $queryGenerator, $metadataPool);
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
$this->eventManager = $eventManager ?: ObjectManager::getInstance()->get(EventManagerInterface::class);
}
/**
* Refresh entities index
*
* @param int[] $entityIds
* @param bool $useTempTable
* @return $this
* @throws \Exception if metadataPool doesn't contain metadata for ProductInterface
* @throws \DomainException
*/
public function execute(array $entityIds = [], $useTempTable = false)
{
$idsToBeReIndexed = $this->getProductIdsWithParents($entityIds);
$this->limitationByProducts = $idsToBeReIndexed;
$this->useTempTable = $useTempTable;
$affectedCategories = $this->getCategoryIdsFromIndex($idsToBeReIndexed);
$this->removeEntries();
$this->reindex();
$affectedCategories = array_merge($affectedCategories, $this->getCategoryIdsFromIndex($idsToBeReIndexed));
$this->registerProducts($idsToBeReIndexed);
$this->registerCategories($affectedCategories);
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
return $this;
}
/**
* Get IDs of parent products by their child IDs.
*
* Returns identifiers of parent product from the catalog_product_relation.
* Please note that returned ids don't contain ids of passed child products.
*
* @param int[] $childProductIds
* @return int[]
* @throws \Exception if metadataPool doesn't contain metadata for ProductInterface
* @throws \DomainException
*/
private function getProductIdsWithParents(array $childProductIds)
{
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
$fieldForParent = $metadata->getLinkField();
$select = $this->connection
->select()
->from(['relation' => $this->getTable('catalog_product_relation')], [])
->distinct(true)
->where('child_id IN (?)', $childProductIds)
->join(
['cpe' => $this->getTable('catalog_product_entity')],
'relation.parent_id = cpe.' . $fieldForParent,
['cpe.entity_id']
);
$parentProductIds = $this->connection->fetchCol($select);
return array_unique(array_merge($childProductIds, $parentProductIds));
}
/**
* Register affected products
*
* @param array $entityIds
* @return void
*/
private function registerProducts($entityIds)
{
$this->cacheContext->registerEntities(Product::CACHE_TAG, $entityIds);
}
/**
* Register categories assigned to products
*
* @param array $categoryIds
* @return void
*/
private function registerCategories(array $categoryIds)
{
if ($categoryIds) {
$this->cacheContext->registerEntities(Category::CACHE_TAG, $categoryIds);
}
}
/**
* Remove index entries before reindexation
*
* @return void
*/
protected function removeEntries()
{
$this->connection->delete(
$this->getMainTable(),
['product_id IN (?)' => $this->limitationByProducts]
);
}
/**
* Retrieve select for reindex products of non anchor categories
*
* @param \Magento\Store\Model\Store $store
* @return \Magento\Framework\DB\Select
*/
protected function getNonAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
{
$select = parent::getNonAnchorCategoriesSelect($store);
return $select->where('ccp.product_id IN (?) OR relation.child_id IN (?)', $this->limitationByProducts);
}
/**
* Retrieve select for reindex products of non anchor categories
*
* @param \Magento\Store\Model\Store $store
* @return \Magento\Framework\DB\Select
*/
protected function getAnchorCategoriesSelect(\Magento\Store\Model\Store $store)
{
$select = parent::getAnchorCategoriesSelect($store);
return $select->where('ccp.product_id IN (?)', $this->limitationByProducts);
}
/**
* Get select for all products
*
* @param \Magento\Store\Model\Store $store
* @return \Magento\Framework\DB\Select
*/
protected function getAllProducts(\Magento\Store\Model\Store $store)
{
$select = parent::getAllProducts($store);
return $select->where('cp.entity_id IN (?)', $this->limitationByProducts);
}
/**
* Check whether select ranging is needed
*
* @return bool
*/
protected function isRangingNeeded()
{
return false;
}
/**
* Returns a list of category ids which are assigned to product ids in the index
*
* This may be required to get list of categories that are affected by the indexation process.
* Also, returns all parents of assigned categories as they're may be affected as well.
*
* @param array $productIds
* @return array
*/
private function getCategoryIdsFromIndex(array $productIds)
{
$categoryIds = $this->connection->fetchCol(
$this->connection->select()
->from($this->getMainTable(), ['category_id'])
->where('product_id IN (?)', $productIds)
->distinct()
);
$parentCategories = $categoryIds;
foreach ($categoryIds as $categoryId) {
$parentIds = explode('/', $this->getPathFromCategoryId($categoryId));
$parentCategories = array_merge($parentCategories, $parentIds);
}
$categoryIds = array_unique($parentCategories);
return $categoryIds;
}
}