17
17
use Magento \Store \Model \StoreManagerInterface ;
18
18
use Magento \Framework \DB \Adapter \AdapterInterface ;
19
19
use Magento \Framework \Indexer \IndexerRegistry ;
20
+ use Magento \Catalog \Model \Indexer \Product \Category as ProductCategoryIndexer ;
20
21
use Magento \Catalog \Model \Indexer \Category \Product as CategoryProductIndexer ;
22
+ use Magento \Catalog \Model \Indexer \Category \Product \TableMaintainer ;
23
+ use Magento \Indexer \Model \WorkingStateProvider ;
21
24
22
25
/**
23
26
* Category rows indexer.
@@ -48,30 +51,42 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
48
51
*/
49
52
private $ indexerRegistry ;
50
53
54
+ /**
55
+ * @var WorkingStateProvider
56
+ */
57
+ private $ workingStateProvider ;
58
+
51
59
/**
52
60
* @param ResourceConnection $resource
53
61
* @param StoreManagerInterface $storeManager
54
62
* @param Config $config
55
63
* @param QueryGenerator|null $queryGenerator
56
64
* @param MetadataPool|null $metadataPool
65
+ * @param TableMaintainer|null $tableMaintainer
57
66
* @param CacheContext|null $cacheContext
58
67
* @param EventManagerInterface|null $eventManager
59
68
* @param IndexerRegistry|null $indexerRegistry
69
+ * @param WorkingStateProvider|null $workingStateProvider
70
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList) Preserve compatibility with the parent class
60
71
*/
61
72
public function __construct (
62
73
ResourceConnection $ resource ,
63
74
StoreManagerInterface $ storeManager ,
64
75
Config $ config ,
65
76
QueryGenerator $ queryGenerator = null ,
66
77
MetadataPool $ metadataPool = null ,
78
+ ?TableMaintainer $ tableMaintainer = null ,
67
79
CacheContext $ cacheContext = null ,
68
80
EventManagerInterface $ eventManager = null ,
69
- IndexerRegistry $ indexerRegistry = null
81
+ IndexerRegistry $ indexerRegistry = null ,
82
+ ?WorkingStateProvider $ workingStateProvider = null
70
83
) {
71
- parent ::__construct ($ resource , $ storeManager , $ config , $ queryGenerator , $ metadataPool );
84
+ parent ::__construct ($ resource , $ storeManager , $ config , $ queryGenerator , $ metadataPool, $ tableMaintainer );
72
85
$ this ->cacheContext = $ cacheContext ?: ObjectManager::getInstance ()->get (CacheContext::class);
73
86
$ this ->eventManager = $ eventManager ?: ObjectManager::getInstance ()->get (EventManagerInterface::class);
74
87
$ this ->indexerRegistry = $ indexerRegistry ?: ObjectManager::getInstance ()->get (IndexerRegistry::class);
88
+ $ this ->workingStateProvider = $ workingStateProvider ?:
89
+ ObjectManager::getInstance ()->get (WorkingStateProvider::class);
75
90
}
76
91
77
92
/**
@@ -82,6 +97,7 @@ public function __construct(
82
97
* @return $this
83
98
* @throws \Exception if metadataPool doesn't contain metadata for ProductInterface
84
99
* @throws \DomainException
100
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
85
101
*/
86
102
public function execute (array $ entityIds = [], $ useTempTable = false )
87
103
{
@@ -90,46 +106,68 @@ public function execute(array $entityIds = [], $useTempTable = false)
90
106
$ this ->limitationByProducts = $ idsToBeReIndexed ;
91
107
$ this ->useTempTable = $ useTempTable ;
92
108
$ indexer = $ this ->indexerRegistry ->get (CategoryProductIndexer::INDEXER_ID );
93
- $ workingState = $ indexer -> isWorking ();
109
+ $ workingState = $ this -> isWorkingState ();
94
110
95
- $ affectedCategories = $ this ->getCategoryIdsFromIndex ($ idsToBeReIndexed );
111
+ if (!$ indexer ->isScheduled ()
112
+ || ($ indexer ->isScheduled () && !$ useTempTable )
113
+ || ($ indexer ->isScheduled () && $ useTempTable && !$ workingState )) {
96
114
97
- if ($ useTempTable && !$ workingState && $ indexer ->isScheduled ()) {
98
- foreach ($ this ->storeManager ->getStores () as $ store ) {
99
- $ this ->connection ->truncateTable ($ this ->getIndexTable ($ store ->getId ()));
115
+ $ affectedCategories = $ this ->getCategoryIdsFromIndex ($ idsToBeReIndexed );
116
+
117
+ if ($ useTempTable && !$ workingState && $ indexer ->isScheduled ()) {
118
+ foreach ($ this ->storeManager ->getStores () as $ store ) {
119
+ $ this ->connection ->truncateTable ($ this ->getIndexTable ($ store ->getId ()));
120
+ }
121
+ } else {
122
+ $ this ->removeEntries ();
100
123
}
101
- } else {
102
- $ this ->removeEntries ();
103
- }
104
- $ this ->reindex ();
105
- if ($ useTempTable && !$ workingState && $ indexer ->isScheduled ()) {
106
- foreach ($ this ->storeManager ->getStores () as $ store ) {
107
- $ this ->connection ->delete (
108
- $ this ->tableMaintainer ->getMainTable ($ store ->getId ()),
109
- ['product_id IN (?) ' => $ this ->limitationByProducts ]
110
- );
111
- $ select = $ this ->connection ->select ()
112
- ->from ($ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ()));
113
- $ this ->connection ->query (
114
- $ this ->connection ->insertFromSelect (
115
- $ select ,
124
+ $ this ->reindex ();
125
+
126
+ // get actual state
127
+ $ workingState = $ this ->isWorkingState ();
128
+
129
+ if ($ useTempTable && !$ workingState && $ indexer ->isScheduled ()) {
130
+ foreach ($ this ->storeManager ->getStores () as $ store ) {
131
+ $ this ->connection ->delete (
116
132
$ this ->tableMaintainer ->getMainTable ($ store ->getId ()),
117
- [],
118
- AdapterInterface::INSERT_ON_DUPLICATE
119
- )
120
- );
133
+ ['product_id IN (?) ' => $ this ->limitationByProducts ]
134
+ );
135
+ $ select = $ this ->connection ->select ()
136
+ ->from ($ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ()));
137
+ $ this ->connection ->query (
138
+ $ this ->connection ->insertFromSelect (
139
+ $ select ,
140
+ $ this ->tableMaintainer ->getMainTable ($ store ->getId ()),
141
+ [],
142
+ AdapterInterface::INSERT_ON_DUPLICATE
143
+ )
144
+ );
145
+ }
121
146
}
122
- }
123
147
124
- $ affectedCategories = array_merge ($ affectedCategories , $ this ->getCategoryIdsFromIndex ($ idsToBeReIndexed ));
148
+ $ affectedCategories = array_merge ($ affectedCategories , $ this ->getCategoryIdsFromIndex ($ idsToBeReIndexed ));
125
149
126
- $ this ->registerProducts ($ idsToBeReIndexed );
127
- $ this ->registerCategories ($ affectedCategories );
128
- $ this ->eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ this ->cacheContext ]);
150
+ $ this ->registerProducts ($ idsToBeReIndexed );
151
+ $ this ->registerCategories ($ affectedCategories );
152
+ $ this ->eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ this ->cacheContext ]);
153
+ }
129
154
130
155
return $ this ;
131
156
}
132
157
158
+ /**
159
+ * Get state for current and shared indexer
160
+ *
161
+ * @return bool
162
+ */
163
+ private function isWorkingState () : bool
164
+ {
165
+ $ indexer = $ this ->indexerRegistry ->get (CategoryProductIndexer::INDEXER_ID );
166
+ $ sharedIndexer = $ this ->indexerRegistry ->get (ProductCategoryIndexer::INDEXER_ID );
167
+ return $ this ->workingStateProvider ->isWorking ($ indexer ->getId ())
168
+ || $ this ->workingStateProvider ->isWorking ($ sharedIndexer ->getId ());
169
+ }
170
+
133
171
/**
134
172
* Get IDs of parent products by their child IDs.
135
173
*
0 commit comments