|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2024 TechDivision GmbH <[email protected]> - TechDivision GmbH |
|
4
|
|
|
* All rights reserved |
|
5
|
|
|
* |
|
6
|
|
|
* This product includes proprietary software developed at TechDivision GmbH, Germany |
|
7
|
|
|
* For more information see https://www.techdivision.com |
|
8
|
|
|
* |
|
9
|
|
|
* To obtain a valid license for using this software, please contact us at |
|
10
|
|
|
* [email protected] |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace TechDivision\Import\Product\Grouped\Observers; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use TechDivision\Import\Observers\StateDetectorInterface; |
|
18
|
|
|
use TechDivision\Import\Product\Grouped\Services\ProductGroupedProcessorInterface; |
|
19
|
|
|
use TechDivision\Import\Product\Grouped\Utils\ColumnKeys; |
|
20
|
|
|
use TechDivision\Import\Product\Grouped\Utils\ConfigurationKeys; |
|
21
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
|
22
|
|
|
use TechDivision\Import\Product\Grouped\Utils\ProductTypes; |
|
23
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @copyright Copyright (c) 2024 TechDivision GmbH <[email protected]> - TechDivision GmbH |
|
27
|
|
|
* @link http://www.techdivision.com |
|
28
|
|
|
* @author MET <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class CleanUpGroupedProductRelationObserver extends AbstractProductImportObserver |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ProductGroupedProcessorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected ProductGroupedProcessorInterface $productGroupedProcessor; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize the observer with the passed product variant processor instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @param ProductGroupedProcessorInterface $productGroupedProcessor The product variant processor instance |
|
41
|
|
|
* @param StateDetectorInterface|null $stateDetector The state detector instance to use |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
ProductGroupedProcessorInterface $productGroupedProcessor, |
|
45
|
|
|
StateDetectorInterface $stateDetector = null |
|
46
|
|
|
) { |
|
47
|
|
|
// pass the state detector to the parent constructor |
|
48
|
|
|
parent::__construct($stateDetector); |
|
49
|
|
|
|
|
50
|
|
|
// initialize the product variant processor instance |
|
51
|
|
|
$this->productGroupedProcessor = $productGroupedProcessor; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return's the product variant processor instance |
|
56
|
|
|
* |
|
57
|
|
|
* @return ProductGroupedProcessorInterface The product variant processor instance |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getProductGroupedProcessor(): ProductGroupedProcessorInterface |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->productGroupedProcessor; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Process the observer's business logic. |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
* @throws Exception |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function process() |
|
71
|
|
|
{ |
|
72
|
|
|
// query whether or not we've found a configurable product |
|
73
|
|
|
if ($this->getValue(ColumnKeys::PRODUCT_TYPE) !== ProductTypes::GROUPED) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// query whether or not the media gallery has to be cleaned up |
|
78
|
|
|
$subject = $this->getSubject(); |
|
79
|
|
|
$subjectConfiguration = $subject->getConfiguration(); |
|
80
|
|
|
|
|
81
|
|
|
if ($subjectConfiguration->hasParam(ConfigurationKeys::CLEAN_UP_GROUPED) |
|
82
|
|
|
&& $subjectConfiguration->getParam(ConfigurationKeys::CLEAN_UP_GROUPED)) { |
|
83
|
|
|
$this->cleanUpGrouped(); |
|
84
|
|
|
|
|
85
|
|
|
$subject->getSystemLogger()->info( |
|
86
|
|
|
$subject->appendExceptionSuffix( |
|
87
|
|
|
sprintf( |
|
88
|
|
|
'Successfully clean up variants for product with SKU "%s"', |
|
89
|
|
|
$this->getValue(ColumnKeys::SKU) |
|
90
|
|
|
) |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Search for variants in the artefacts and check for differences in the database. Remove entries in DB that do not |
|
98
|
|
|
* exist in artefact. |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
* @throws Exception Is thrown if all the variant children und attributes cannot be deleted |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function cleanUpGrouped() |
|
104
|
|
|
{ |
|
105
|
|
|
// load the available artefacts from the subject |
|
106
|
|
|
$subject = $this->getSubject(); |
|
107
|
|
|
$artefacts = $subject->getArtefacts(); |
|
108
|
|
|
|
|
109
|
|
|
// return, if we do NOT have any variant artefacts |
|
110
|
|
|
if (!isset($artefacts[ProductGroupedObserver::ARTEFACT_TYPE])) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// load the entity ID of the parent product |
|
115
|
|
|
$parentIdForArtefacts = $this->getLastEntityId(); |
|
116
|
|
|
|
|
117
|
|
|
// return, if we do NOT have any artefacts for the actual entity ID |
|
118
|
|
|
if (!isset($artefacts[ProductGroupedObserver::ARTEFACT_TYPE][$parentIdForArtefacts])) { |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// initialize the array with the SKUs of |
|
123
|
|
|
// the child IDs and the attribute codes |
|
124
|
|
|
$actualGrouped = []; |
|
125
|
|
|
$actualAttributes = []; |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
// load the variant artefacts for the actual entity ID |
|
128
|
|
|
$allGrouped = $artefacts[ProductGroupedObserver::ARTEFACT_TYPE][$parentIdForArtefacts]; |
|
129
|
|
|
|
|
130
|
|
|
// iterate over the artefacts with the variant data |
|
131
|
|
|
foreach ($allGrouped as $variantData) { |
|
132
|
|
|
// add the child SKU to the array |
|
133
|
|
|
$actualGrouped[] = $variantData[ColumnKeys::GROUPED_CHILD_SKU]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// load the row/entity ID of the parent product |
|
137
|
|
|
$parentId = $this->getLastPrimaryKey(); |
|
138
|
|
|
|
|
139
|
|
|
try { |
|
140
|
|
|
$this->cleanUpGroupedRelation($parentId, $actualGrouped); |
|
141
|
|
|
} catch (Exception $e) { |
|
142
|
|
|
// log a warning if debug mode has been enabled |
|
143
|
|
|
if ($subject->isDebugMode()) { |
|
144
|
|
|
$subject->getSystemLogger()->critical($subject->appendExceptionSuffix($e->getMessage())); |
|
145
|
|
|
} else { |
|
146
|
|
|
throw $e; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Delete not exists import relations from database. |
|
153
|
|
|
* |
|
154
|
|
|
* @param int $parentProductId The ID of the parent product |
|
155
|
|
|
* @param array $childData The array of variants |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
* @throws Exception |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function cleanUpGroupedRelation(int $parentProductId, array $childData) |
|
161
|
|
|
{ |
|
162
|
|
|
// we don't want to delete everything |
|
163
|
|
|
if (empty($childData)) { |
|
164
|
|
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// load the SKU of the parent product |
|
168
|
|
|
$parentSku = $this->getValue(ColumnKeys::SKU); |
|
169
|
|
|
|
|
170
|
|
|
// remove the old variants from the database‚ |
|
171
|
|
|
$this->getProductGroupedProcessor()->deleteProductRelation( |
|
172
|
|
|
[ |
|
173
|
|
|
MemberNames::PARENT_ID => $parentProductId, |
|
174
|
|
|
MemberNames::SKU => $childData, |
|
175
|
|
|
] |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
// log a debug message that the image has been removed |
|
179
|
|
|
$subject = $this->getSubject(); |
|
180
|
|
|
$subject->getSystemLogger()->info( |
|
181
|
|
|
$subject->appendExceptionSuffix( |
|
182
|
|
|
sprintf( |
|
183
|
|
|
'Successfully clean up relations for product with SKU "%s"', |
|
184
|
|
|
$parentSku |
|
185
|
|
|
) |
|
186
|
|
|
) |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Return's the PK to create the product => variant relation. |
|
192
|
|
|
* |
|
193
|
|
|
* @return int The PK to create the relation with |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function getLastPrimaryKey(): int |
|
196
|
|
|
{ |
|
197
|
|
|
return (int)$this->getLastEntityId(); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|