77
88use Magento \Catalog \Api \CategoryRepositoryInterface ;
99use Magento \Framework \Exception \NoSuchEntityException ;
10+ use Magento \Store \Model \Store ;
1011
1112class Rows extends \Magento \Catalog \Model \Indexer \Category \Flat \AbstractAction
1213{
@@ -34,11 +35,11 @@ public function __construct(
3435 /**
3536 * Return index table name
3637 *
37- * @param \Magento\Store\Model\ Store $store
38+ * @param Store $store
3839 * @param bool $useTempTable
3940 * @return string
4041 */
41- protected function getTableNameByStore (\ Magento \ Store \ Model \ Store $ store , $ useTempTable )
42+ protected function getTableNameByStore (Store $ store , $ useTempTable )
4243 {
4344 $ tableName = $ this ->getMainStoreTable ($ store ->getId ());
4445 return $ useTempTable ? $ this ->addTemporaryTableSuffix ($ tableName ) : $ tableName ;
@@ -55,50 +56,8 @@ public function reindex(array $entityIds = [], $useTempTable = false)
5556 {
5657 $ stores = $ this ->storeManager ->getStores ();
5758
58- /* @var $store \Magento\Store\Model\Store */
5959 foreach ($ stores as $ store ) {
60- $ tableName = $ this ->getTableNameByStore ($ store , $ useTempTable );
61-
62- if (!$ this ->connection ->isTableExists ($ tableName )) {
63- continue ;
64- }
65-
66- /** @TODO Do something with chunks */
67- $ categoriesIdsChunks = array_chunk ($ entityIds , 500 );
68- foreach ($ categoriesIdsChunks as $ categoriesIdsChunk ) {
69- $ categoriesIdsChunk = $ this ->filterIdsByStore ($ categoriesIdsChunk , $ store );
70-
71- $ attributesData = $ this ->getAttributeValues ($ categoriesIdsChunk , $ store ->getId ());
72- $ data = [];
73- foreach ($ categoriesIdsChunk as $ categoryId ) {
74- if (!isset ($ attributesData [$ categoryId ])) {
75- continue ;
76- }
77-
78- try {
79- $ category = $ this ->categoryRepository ->get ($ categoryId );
80- } catch (NoSuchEntityException $ e ) {
81- continue ;
82- }
83-
84- $ data [] = $ this ->prepareValuesToInsert (
85- array_merge (
86- $ category ->getData (),
87- $ attributesData [$ categoryId ],
88- ['store_id ' => $ store ->getId ()]
89- )
90- );
91- }
92-
93- foreach ($ data as $ row ) {
94- $ updateFields = [];
95- foreach (array_keys ($ row ) as $ key ) {
96- $ updateFields [$ key ] = $ key ;
97- }
98- $ this ->connection ->insertOnDuplicate ($ tableName , $ row , $ updateFields );
99- }
100- }
101- $ this ->deleteNonStoreCategories ($ store , $ useTempTable );
60+ $ this ->reindexStore ($ store , $ entityIds , $ useTempTable );
10261 }
10362
10463 return $ this ;
@@ -107,11 +66,11 @@ public function reindex(array $entityIds = [], $useTempTable = false)
10766 /**
10867 * Delete non stores categories
10968 *
110- * @param \Magento\Store\Model\ Store $store
69+ * @param Store $store
11170 * @param bool $useTempTable
11271 * @return void
11372 */
114- protected function deleteNonStoreCategories (\ Magento \ Store \ Model \ Store $ store , $ useTempTable )
73+ protected function deleteNonStoreCategories (Store $ store , $ useTempTable )
11574 {
11675 $ rootId = \Magento \Catalog \Model \Category::TREE_ROOT_ID ;
11776
@@ -142,7 +101,7 @@ protected function deleteNonStoreCategories(\Magento\Store\Model\Store $store, $
142101 * Filter category ids by store
143102 *
144103 * @param int[] $ids
145- * @param \Magento\Store\Model\ Store $store
104+ * @param Store $store
146105 * @return int[]
147106 */
148107 protected function filterIdsByStore (array $ ids , $ store )
@@ -169,4 +128,97 @@ protected function filterIdsByStore(array $ids, $store)
169128 }
170129 return $ resultIds ;
171130 }
131+
132+ /**
133+ * Reindex data for store
134+ *
135+ * @param Store $store
136+ * @param int[] $entityIds
137+ * @param bool $useTempTable
138+ */
139+ private function reindexStore (Store $ store , array $ entityIds , $ useTempTable )
140+ {
141+ $ tableName = $ this ->getTableNameByStore ($ store , $ useTempTable );
142+ if (!$ this ->connection ->isTableExists ($ tableName )) {
143+ return ;
144+ }
145+
146+ $ categoriesIdsChunks = array_chunk ($ entityIds , 500 );
147+ foreach ($ categoriesIdsChunks as $ categoriesIdsChunk ) {
148+ $ categoriesIdsChunk = $ this ->filterIdsByStore ($ categoriesIdsChunk , $ store );
149+ $ attributesData = $ this ->getAttributeValues ($ categoriesIdsChunk , $ store ->getId ());
150+ $ indexData = $ this ->buildIndexData ($ store , $ categoriesIdsChunk , $ attributesData );
151+ $ this ->updateIndexData ($ tableName , $ indexData );
152+ }
153+
154+ $ this ->deleteNonStoreCategories ($ store , $ useTempTable );
155+ }
156+
157+ /**
158+ * Build data for insert into index
159+ *
160+ * @param Store $store
161+ * @param int[] $categoriesIdsChunk
162+ * @param array[] $attributesData
163+ * @return array
164+ */
165+ private function buildIndexData (Store $ store , $ categoriesIdsChunk , $ attributesData )
166+ {
167+ $ data = [];
168+ foreach ($ categoriesIdsChunk as $ categoryId ) {
169+ try {
170+ $ categoryAttributesData = [];
171+ if (isset ($ attributesData [$ categoryId ]) && is_array ($ attributesData [$ categoryId ])) {
172+ $ categoryAttributesData = $ attributesData [$ categoryId ];
173+ }
174+ $ categoryIndexData = $ this ->buildCategoryIndexData (
175+ $ store ,
176+ $ categoryId ,
177+ $ categoryAttributesData
178+ );
179+ $ data [] = $ categoryIndexData ;
180+ } catch (NoSuchEntityException $ e ) {
181+ // ignore
182+ }
183+ }
184+ return $ data ;
185+ }
186+
187+ /**
188+ * @param Store $store
189+ * @param int $categoryId
190+ * @param array $categoryAttributesData
191+ * @return array
192+ * @throws NoSuchEntityException
193+ */
194+ private function buildCategoryIndexData (Store $ store , $ categoryId , array $ categoryAttributesData )
195+ {
196+ $ category = $ this ->categoryRepository ->get ($ categoryId );
197+ $ categoryAttributesData = [];
198+ $ data = $ this ->prepareValuesToInsert (
199+ array_merge (
200+ $ category ->getData (),
201+ $ categoryAttributesData ,
202+ ['store_id ' => $ store ->getId ()]
203+ )
204+ );
205+ return $ data ;
206+ }
207+
208+ /**
209+ * Insert or update index data
210+ *
211+ * @param string $tableName
212+ * @param $data
213+ */
214+ private function updateIndexData ($ tableName , $ data )
215+ {
216+ foreach ($ data as $ row ) {
217+ $ updateFields = [];
218+ foreach (array_keys ($ row ) as $ key ) {
219+ $ updateFields [$ key ] = $ key ;
220+ }
221+ $ this ->connection ->insertOnDuplicate ($ tableName , $ row , $ updateFields );
222+ }
223+ }
172224}
0 commit comments