1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Variant\Observers\VariantObserver |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-product-variant |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Variant\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Variant\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\Variant\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
26
|
|
|
use TechDivision\Import\Product\Variant\Services\ProductVariantProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Oberserver that provides functionality for the product variant replace operation. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import-product-variant |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class VariantObserver extends AbstractProductImportObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The product relation's parent ID. |
42
|
|
|
* |
43
|
|
|
* @var integer |
44
|
|
|
*/ |
45
|
|
|
protected $parentId; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The product relation's child ID. |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
protected $childId; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The product variant processor instance. |
56
|
|
|
* |
57
|
|
|
* @var \TechDivision\Import\Product\Variant\Services\ProductVariantProcessorInterface |
58
|
|
|
*/ |
59
|
|
|
protected $productVariantProcessor; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initialize the observer with the passed product variant processor instance. |
63
|
|
|
* |
64
|
|
|
* @param \TechDivision\Import\Product\Variant\Services\ProductVariantProcessorInterface $productVariantProcessor The product variant processor instance |
65
|
|
|
*/ |
66
|
|
|
public function __construct(ProductVariantProcessorInterface $productVariantProcessor) |
67
|
|
|
{ |
68
|
|
|
$this->productVariantProcessor = $productVariantProcessor; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return's the product variant processor instance. |
73
|
|
|
* |
74
|
|
|
* @return \TechDivision\Import\Product\Variant\Services\ProductVariantProcessorInterface The product variant processor instance |
75
|
|
|
*/ |
76
|
|
|
protected function getProductVariantProcessor() |
77
|
|
|
{ |
78
|
|
|
return $this->productVariantProcessor; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Process the observer's business logic. |
83
|
|
|
* |
84
|
|
|
* @return array The processed row |
85
|
|
|
*/ |
86
|
|
|
protected function process() |
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
// try to load and map the parent ID |
91
|
|
|
$this->parentId = $this->mapParentSku($parentSku = $this->getValue(ColumnKeys::VARIANT_PARENT_SKU)); |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
throw $this->wrapException(array(ColumnKeys::VARIANT_PARENT_SKU), $e); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
// try to load and map the child ID |
99
|
|
|
$this->childId = $this->mapChildSku($childSku = $this->getValue(ColumnKeys::VARIANT_CHILD_SKU)); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
throw $this->wrapException(array(ColumnKeys::VARIANT_CHILD_SKU), $e); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
// prepare and persist the product relation |
106
|
|
|
if ($productRelation = $this->initializeProductRelation($this->prepareProductRelationAttributes())) { |
107
|
|
|
$this->persistProductRelation($productRelation); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// prepare and persist the product super link |
111
|
|
|
if ($productSuperLink = $this->initializeProductSuperLink($this->prepareProductSuperLinkAttributes())) { |
112
|
|
|
$this->persistProductSuperLink($productSuperLink); |
113
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
} catch (\Exception $e) { |
116
|
|
|
// prepare a more detailed error message |
117
|
|
|
$message = $this->appendExceptionSuffix( |
118
|
|
|
sprintf( |
119
|
|
|
'Product relation with SKUs %s => %s can\'t be created', |
120
|
|
|
$parentSku, |
121
|
|
|
$childSku |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
// if we're NOT in debug mode, re-throw a more detailed exception |
126
|
|
|
$wrappedException = $this->wrapException( |
127
|
|
|
array(ColumnKeys::VARIANT_PARENT_SKU, ColumnKeys::VARIANT_CHILD_SKU), |
|
|
|
|
128
|
|
|
new \Exception($message, null, $e) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
// query whether or not, debug mode is enabled |
132
|
|
|
if ($this->isDebugMode()) { |
133
|
|
|
// log a warning and return immediately |
134
|
|
|
$this->getSystemLogger()->warning($wrappedException->getMessage()); |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// else, throw the exception |
139
|
|
|
throw $wrappedException; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Prepare the product relation attributes that has to be persisted. |
145
|
|
|
* |
146
|
|
|
* @return array The prepared product relation attributes |
147
|
|
|
*/ |
148
|
|
|
protected function prepareProductRelationAttributes() |
149
|
|
|
{ |
150
|
|
|
|
151
|
|
|
// initialize and return the entity |
152
|
|
|
return $this->initializeEntity( |
153
|
|
|
array( |
154
|
|
|
MemberNames::PARENT_ID => $this->parentId, |
155
|
|
|
MemberNames::CHILD_ID => $this->childId |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Prepare the product super link attributes that has to be persisted. |
162
|
|
|
* |
163
|
|
|
* @return array The prepared product super link attributes |
164
|
|
|
*/ |
165
|
|
|
protected function prepareProductSuperLinkAttributes() |
166
|
|
|
{ |
167
|
|
|
|
168
|
|
|
// initialize and return the entity |
169
|
|
|
return $this->initializeEntity( |
170
|
|
|
array( |
171
|
|
|
MemberNames::PRODUCT_ID => $this->childId, |
172
|
|
|
MemberNames::PARENT_ID => $this->parentId |
173
|
|
|
) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Initialize the product relation with the passed attributes and returns an instance. |
179
|
|
|
* |
180
|
|
|
* @param array $attr The product relation attributes |
181
|
|
|
* |
182
|
|
|
* @return array|null The initialized product relation, or null if the relation already exsist |
183
|
|
|
*/ |
184
|
|
|
protected function initializeProductRelation(array $attr) |
185
|
|
|
{ |
186
|
|
|
return $attr; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Initialize the product super link with the passed attributes and returns an instance. |
191
|
|
|
* |
192
|
|
|
* @param array $attr The product super link attributes |
193
|
|
|
* |
194
|
|
|
* @return array|null The initialized product super link, or null if the super link already exsist |
195
|
|
|
*/ |
196
|
|
|
protected function initializeProductSuperLink(array $attr) |
197
|
|
|
{ |
198
|
|
|
return $attr; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Map's the passed SKU of the parent product to it's PK. |
203
|
|
|
* |
204
|
|
|
* @param string $parentSku The SKU of the parent product |
205
|
|
|
* |
206
|
|
|
* @return integer The primary key used to create relations |
207
|
|
|
*/ |
208
|
|
|
protected function mapParentSku($parentSku) |
209
|
|
|
{ |
210
|
|
|
return $this->mapSkuToEntityId($parentSku); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Map's the passed SKU of the child product to it's PK. |
215
|
|
|
* |
216
|
|
|
* @param string $childSku The SKU of the child product |
217
|
|
|
* |
218
|
|
|
* @return integer The primary key used to create relations |
219
|
|
|
*/ |
220
|
|
|
protected function mapChildSku($childSku) |
221
|
|
|
{ |
222
|
|
|
return $this->mapSkuToEntityId($childSku); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return the entity ID for the passed SKU. |
227
|
|
|
* |
228
|
|
|
* @param string $sku The SKU to return the entity ID for |
229
|
|
|
* |
230
|
|
|
* @return integer The mapped entity ID |
231
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
232
|
|
|
*/ |
233
|
|
|
protected function mapSkuToEntityId($sku) |
234
|
|
|
{ |
235
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Persist's the passed product relation data and return's the ID. |
240
|
|
|
* |
241
|
|
|
* @param array $productRelation The product relation data to persist |
242
|
|
|
* |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
protected function persistProductRelation($productRelation) |
246
|
|
|
{ |
247
|
|
|
return $this->getProductVariantProcessor()->persistProductRelation($productRelation); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Persist's the passed product super link data and return's the ID. |
252
|
|
|
* |
253
|
|
|
* @param array $productSuperLink The product super link data to persist |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
protected function persistProductSuperLink($productSuperLink) |
258
|
|
|
{ |
259
|
|
|
return $this->getProductVariantProcessor()->persistProductSuperLink($productSuperLink); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: