66use Aligent \FredhopperIndexer \Model \DataHandler ;
77use Aligent \FredhopperIndexer \Model \ResourceModel \Changelog as ChangelogResource ;
88use Magento \Framework \App \ResourceConnection ;
9+ use Magento \Framework \Exception \LocalizedException ;
910
1011class InsertRecords
1112{
1213 /**
1314 * @param ResourceConnection $resourceConnection
1415 * @param ChangelogResource $changelogResource
16+ * @param TempTable $tempTable
1517 */
1618 public function __construct (
1719 private readonly ResourceConnection $ resourceConnection ,
18- private readonly ChangelogResource $ changelogResource
20+ private readonly ChangelogResource $ changelogResource ,
21+ private readonly TempTable $ tempTable
1922 ) {
2023 }
2124
@@ -24,30 +27,36 @@ public function __construct(
2427 *
2528 * @return void
2629 * @throws \Zend_Db_Select_Exception
30+ * @throws LocalizedException
2731 */
2832 public function execute (): void
2933 {
34+ $ tempTableName = $ this ->tempTable ->getTempTableName ();
3035 $ addedProductIds = $ this ->getAddedOrDeletedProductsByType (
3136 true ,
32- DataHandler::TYPE_PRODUCT
37+ DataHandler::TYPE_PRODUCT ,
38+ $ tempTableName
3339 );
3440 $ addedVariantIds = $ this ->getAddedOrDeletedProductsByType (
3541 true ,
36- DataHandler::TYPE_VARIANT
42+ DataHandler::TYPE_VARIANT ,
43+ $ tempTableName
3744 );
3845 $ this ->changelogResource ->insertAdditionOperations ($ addedProductIds , $ addedVariantIds );
3946
40- $ updatedProductIds = $ this ->getUpdatedProductsByType (DataHandler::TYPE_PRODUCT );
41- $ updatedVariantIds = $ this ->getUpdatedProductsByType (DataHandler::TYPE_VARIANT );
47+ $ updatedProductIds = $ this ->getUpdatedProductsByType (DataHandler::TYPE_PRODUCT , $ tempTableName );
48+ $ updatedVariantIds = $ this ->getUpdatedProductsByType (DataHandler::TYPE_VARIANT , $ tempTableName );
4249 $ this ->changelogResource ->insertUpdateOperations ($ updatedProductIds , $ updatedVariantIds );
4350
4451 $ deletedProductIds = $ this ->getAddedOrDeletedProductsByType (
4552 false ,
46- DataHandler::TYPE_PRODUCT
53+ DataHandler::TYPE_PRODUCT ,
54+ $ tempTableName
4755 );
4856 $ deletedVariantIds = $ this ->getAddedOrDeletedProductsByType (
4957 false ,
50- DataHandler::TYPE_VARIANT
58+ DataHandler::TYPE_VARIANT ,
59+ $ tempTableName
5160 );
5261 $ this ->changelogResource ->insertDeleteOperations ($ deletedProductIds , $ deletedVariantIds );
5362 }
@@ -57,21 +66,23 @@ public function execute(): void
5766 *
5867 * @param bool $isAddition
5968 * @param string $productType
69+ * @param string $tempTableName
6070 * @return array
6171 */
6272 private function getAddedOrDeletedProductsByType (
6373 bool $ isAddition ,
64- string $ productType
74+ string $ productType ,
75+ string $ tempTableName
6576 ): array {
6677 $ connection = $ this ->resourceConnection ->getConnection ();
6778 $ select = $ connection ->select ();
6879
6980 $ select ->from (
70- ['main_table ' => ($ isAddition ? DataHandler::INDEX_TABLE_NAME : TempTable:: TEMP_TABLE_NAME )],
81+ ['main_table ' => ($ isAddition ? DataHandler::INDEX_TABLE_NAME : $ tempTableName )],
7182 ['product_id ' ]
7283 );
7384 $ select ->joinLeft (
74- ['temp_table ' => ($ isAddition ? TempTable:: TEMP_TABLE_NAME : DataHandler::INDEX_TABLE_NAME )],
85+ ['temp_table ' => ($ isAddition ? $ tempTableName : DataHandler::INDEX_TABLE_NAME )],
7586 'temp_table.product_id = main_table.product_id AND ' .
7687 'temp_table.product_type = main_table.product_type ' ,
7788 []
@@ -87,16 +98,17 @@ private function getAddedOrDeletedProductsByType(
8798 * Determine which products have been updated between the main and temporary table
8899 *
89100 * @param string $productType
101+ * @param string $tempTableName
90102 * @return array
91103 * @throws \Zend_Db_Select_Exception
92104 */
93- private function getUpdatedProductsByType (string $ productType ): array
105+ private function getUpdatedProductsByType (string $ productType, string $ tempTableName ): array
94106 {
95107 // get all product ids and variant ids that exist in both tables
96108 // we do not want to consider products that are being added or deleted completely
97109 $ connection = $ this ->resourceConnection ->getConnection ();
98110 $ existingProductsSelect = $ connection ->select ();
99- $ existingProductsSelect ->from (['temp_table ' => TempTable:: TEMP_TABLE_NAME ], ['product_id ' ]);
111+ $ existingProductsSelect ->from (['temp_table ' => $ tempTableName ], ['product_id ' ]);
100112 $ existingProductsSelect ->joinInner (
101113 ['main_table ' => DataHandler::INDEX_TABLE_NAME ],
102114 'main_table.product_id = temp_table.product_id AND ' .
@@ -114,7 +126,7 @@ private function getUpdatedProductsByType(string $productType): array
114126 ['product_id ' ]
115127 );
116128 $ existingProductsTempMissingSelect ->joinLeft (
117- ['temp_table ' => TempTable:: TEMP_TABLE_NAME ],
129+ ['temp_table ' => $ tempTableName ],
118130 'main_table.product_id = temp_table.product_id AND ' .
119131 'main_table.product_type = temp_table.product_type AND ' .
120132 'main_table.store_id = temp_table.store_id ' ,
@@ -127,7 +139,7 @@ private function getUpdatedProductsByType(string $productType): array
127139 // records that are in the old table, but not in the new table
128140 $ existingProductsMainMissingSelect = $ connection ->select ();
129141 $ existingProductsMainMissingSelect ->from (
130- ['temp_table ' => TempTable:: TEMP_TABLE_NAME ],
142+ ['temp_table ' => $ tempTableName ],
131143 ['product_id ' ]
132144 );
133145 $ existingProductsMainMissingSelect ->joinLeft (
@@ -148,7 +160,7 @@ private function getUpdatedProductsByType(string $productType): array
148160 ['product_id ' ]
149161 );
150162 $ existingProductsDifferenceSelect ->joinInner (
151- ['temp_table ' => TempTable:: TEMP_TABLE_NAME ],
163+ ['temp_table ' => $ tempTableName ],
152164 'main_table.product_id = temp_table.product_id AND ' .
153165 'main_table.product_type = temp_table.product_type AND ' .
154166 'main_table.store_id = temp_table.store_id AND ' .
0 commit comments