|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Bundle\Observers\BundleSelectionObserver |
|
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-bundle |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Bundle\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
|
24
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
|
25
|
|
|
use TechDivision\Import\Observers\StateDetectorInterface; |
|
26
|
|
|
use TechDivision\Import\Observers\AttributeLoaderInterface; |
|
27
|
|
|
use TechDivision\Import\Observers\DynamicAttributeObserverInterface; |
|
28
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
|
29
|
|
|
use TechDivision\Import\Product\Bundle\Utils\ColumnKeys; |
|
30
|
|
|
use TechDivision\Import\Product\Bundle\Utils\MemberNames; |
|
31
|
|
|
use TechDivision\Import\Product\Bundle\Utils\EntityTypeCodes; |
|
32
|
|
|
use TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Oberserver that provides functionality for the bundle selection replace operation. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Tim Wagner <[email protected]> |
|
38
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
39
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
40
|
|
|
* @link https://github.com/techdivision/import-product-bundle |
|
41
|
|
|
* @link http://www.techdivision.com |
|
42
|
|
|
*/ |
|
43
|
|
|
class BundleSelectionObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The product bundle processor instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $productBundleProcessor; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The attribute loader instance. |
|
55
|
|
|
* |
|
56
|
|
|
* @var \TechDivision\Import\Observers\AttributeLoaderInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $attributeLoader; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Initialize the "dymanmic" columns. |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $columns = array( |
|
66
|
|
|
MemberNames::POSITION => array(ColumnKeys::BUNDLE_VALUE_SELECTION_POSITION, BackendTypeKeys::BACKEND_TYPE_INT), |
|
67
|
|
|
MemberNames::IS_DEFAULT => array(ColumnKeys::BUNDLE_VALUE_DEFAULT, BackendTypeKeys::BACKEND_TYPE_INT), |
|
68
|
|
|
MemberNames::SELECTION_PRICE_VALUE => array(ColumnKeys::BUNDLE_VALUE_PRICE, BackendTypeKeys::BACKEND_TYPE_FLOAT), |
|
69
|
|
|
MemberNames::SELECTION_CAN_CHANGE_QTY => array(ColumnKeys::BUNDLE_VALUE_CAN_CHANGE_QTY, BackendTypeKeys::BACKEND_TYPE_INT) |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Array with virtual column name mappings (this is a temporary |
|
74
|
|
|
* solution till techdivision/import#179 as been implemented). |
|
75
|
|
|
* |
|
76
|
|
|
* @var array |
|
77
|
|
|
* @todo https://github.com/techdivision/import/issues/179 |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $virtualMapping = array( |
|
80
|
|
|
MemberNames::POSITION => ColumnKeys::BUNDLE_VALUE_SELECTION_POSITION, |
|
81
|
|
|
MemberNames::IS_DEFAULT => ColumnKeys::BUNDLE_VALUE_DEFAULT, |
|
82
|
|
|
MemberNames::SELECTION_PRICE_VALUE => ColumnKeys::BUNDLE_VALUE_PRICE, |
|
83
|
|
|
MemberNames::SELECTION_CAN_CHANGE_QTY => ColumnKeys::BUNDLE_VALUE_CAN_CHANGE_QTY |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Initialize the observer with the passed product bundle processor instance. |
|
88
|
|
|
* |
|
89
|
|
|
* @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance |
|
90
|
|
|
* @param \TechDivision\Import\Observers\AttributeLoaderInterface|null $attributeLoader The attribute loader instance |
|
91
|
|
|
* @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
94
|
|
|
ProductBundleProcessorInterface $productBundleProcessor, |
|
95
|
|
|
AttributeLoaderInterface $attributeLoader = null, |
|
96
|
|
|
StateDetectorInterface $stateDetector = null |
|
97
|
|
|
) { |
|
98
|
|
|
|
|
99
|
|
|
// initialize the product bundle processor and the attribute loader instance |
|
100
|
|
|
$this->productBundleProcessor = $productBundleProcessor; |
|
101
|
|
|
$this->attributeLoader = $attributeLoader; |
|
102
|
|
|
|
|
103
|
|
|
// pass the state detector to the parent method |
|
104
|
|
|
parent::__construct($stateDetector); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return's the product bundle processor instance. |
|
109
|
|
|
* |
|
110
|
|
|
* @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getProductBundleProcessor() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->productBundleProcessor; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Query whether or not a value for the column with the passed name exists. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $name The column name to query for a valid value |
|
121
|
|
|
* |
|
122
|
|
|
* @return boolean TRUE if the value is set, else FALSE |
|
123
|
|
|
* @todo https://github.com/techdivision/import/issues/179 |
|
124
|
|
|
*/ |
|
125
|
|
|
public function hasValue($name) |
|
126
|
|
|
{ |
|
127
|
|
|
return parent::hasValue(isset($this->virtualMapping[$name]) ? $this->virtualMapping[$name] : $name); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Process the observer's business logic. |
|
132
|
|
|
* |
|
133
|
|
|
* @return array The processed row |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function process() |
|
136
|
|
|
{ |
|
137
|
|
|
|
|
138
|
|
|
// prepare the store view code |
|
139
|
|
|
$this->prepareStoreViewCode($this->getRow()); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
// return immediately if we're have no store view code set |
|
142
|
|
|
if (StoreViewCodes::ADMIN !== $this->getStoreViewCode(StoreViewCodes::ADMIN)) { |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// prepare, initialize and persist the product bundle selection data |
|
147
|
|
|
$productBundleSelection = $this->initializeBundleSelection($this->prepareDynamicAttributes()); |
|
148
|
|
|
$selectionId = $this->persistProductBundleSelection($productBundleSelection); |
|
149
|
|
|
|
|
150
|
|
|
// add the mapping for the child SKU => selection ID |
|
151
|
|
|
$this->addChildSkuSelectionIdMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU), $selectionId); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Appends the dynamic attributes to the static ones and returns them. |
|
156
|
|
|
* |
|
157
|
|
|
* @return array The array with all available attributes |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function prepareDynamicAttributes() : array |
|
160
|
|
|
{ |
|
161
|
|
|
return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
|
166
|
|
|
* |
|
167
|
|
|
* @return array The prepared attributes |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function prepareAttributes() |
|
170
|
|
|
{ |
|
171
|
|
|
|
|
172
|
|
|
// load the actual option ID |
|
173
|
|
|
$optionId = $this->getLastOptionId(); |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
|
|
// load and map the parent SKU |
|
177
|
|
|
$parentId = $this->mapSku($this->getValue(ColumnKeys::BUNDLE_PARENT_SKU)); |
|
178
|
|
|
} catch (\Exception $e) { |
|
179
|
|
|
throw $this->wrapException(array(ColumnKeys::BUNDLE_PARENT_SKU), $e); |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
try { |
|
183
|
|
|
// try to load the child ID |
|
184
|
|
|
$childId = $this->mapSkuToEntityId($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU)); |
|
185
|
|
|
} catch (\Exception $e) { |
|
186
|
|
|
throw $this->wrapException(array(ColumnKeys::BUNDLE_VALUE_SKU), $e); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
try { |
|
190
|
|
|
// try to load the selection price type |
|
191
|
|
|
$selectionPriceType = $this->mapPriceType($this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE)); |
|
192
|
|
|
} catch (\Exception $e) { |
|
193
|
|
|
throw $this->wrapException(array(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE), $e); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// load the default values |
|
197
|
|
|
$selectionQty = $this->getValue(ColumnKeys::BUNDLE_VALUE_DEFAULT_QTY); |
|
198
|
|
|
|
|
199
|
|
|
// prepare the product bundle selection data |
|
200
|
|
|
return $this->initializeEntity( |
|
201
|
|
|
$this->loadRawEntity( |
|
202
|
|
|
array( |
|
203
|
|
|
MemberNames::OPTION_ID => $optionId, |
|
204
|
|
|
MemberNames::PARENT_PRODUCT_ID => $parentId, |
|
205
|
|
|
MemberNames::PRODUCT_ID => $childId, |
|
206
|
|
|
MemberNames::SELECTION_QTY => $selectionQty, |
|
207
|
|
|
MemberNames::SELECTION_PRICE_TYPE => $selectionPriceType |
|
208
|
|
|
) |
|
209
|
|
|
) |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Load's and return's a raw entity without primary key but the mandatory members only and nulled values. |
|
215
|
|
|
* |
|
216
|
|
|
* @param array $data An array with data that will be used to initialize the raw entity with |
|
217
|
|
|
* |
|
218
|
|
|
* @return array The initialized entity |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function loadRawEntity(array $data = array()) |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->getProductBundleProcessor()->loadRawEntity(EntityTypeCodes::CATALOG_PRODUCT_BUNDLE_SELECTION, $data); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Initialize the bundle selection with the passed attributes and returns an instance. |
|
227
|
|
|
* |
|
228
|
|
|
* @param array $attr The bundle selection attributes |
|
229
|
|
|
* |
|
230
|
|
|
* @return array The initialized bundle selection |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function initializeBundleSelection(array $attr) |
|
233
|
|
|
{ |
|
234
|
|
|
return $attr; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Return's the last created option ID. |
|
239
|
|
|
* |
|
240
|
|
|
* @return integer $optionId The last created option ID |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function getLastOptionId() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->getSubject()->getLastOptionId(); |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Save's the mapping of the child SKU and the selection ID. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $childSku The child SKU of the selection |
|
251
|
|
|
* @param integer $selectionId The selection ID to save |
|
252
|
|
|
* |
|
253
|
|
|
* @return void |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function addChildSkuSelectionIdMapping($childSku, $selectionId) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->getSubject()->addChildSkuSelectionIdMapping($childSku, $selectionId); |
|
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Returns the acutal value of the position counter and raise's it by one. |
|
262
|
|
|
* |
|
263
|
|
|
* @return integer The actual value of the position counter |
|
264
|
|
|
* @deprecated Since 22.0.0 |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function raisePositionCounter() |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->getSubject()->raisePositionCounter(); |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Return's the mapping for the passed price type. |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $priceType The price type to map |
|
275
|
|
|
* |
|
276
|
|
|
* @return integer The mapped price type |
|
277
|
|
|
* @throws \Exception Is thrown, if the passed price type can't be mapped |
|
278
|
|
|
*/ |
|
279
|
|
|
protected function mapPriceType($priceType) |
|
280
|
|
|
{ |
|
281
|
|
|
return $this->getSubject()->mapPriceType($priceType); |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Return the entity ID for the passed SKU. |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $sku The SKU to return the entity ID for |
|
288
|
|
|
* |
|
289
|
|
|
* @return integer The mapped entity ID |
|
290
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
|
291
|
|
|
*/ |
|
292
|
|
|
protected function mapSku($sku) |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
|
|
|
|
|
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Return the entity ID for the passed SKU. |
|
299
|
|
|
* |
|
300
|
|
|
* @param string $sku The SKU to return the entity ID for |
|
301
|
|
|
* |
|
302
|
|
|
* @return integer The mapped entity ID |
|
303
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
|
304
|
|
|
*/ |
|
305
|
|
|
protected function mapSkuToEntityId($sku) |
|
306
|
|
|
{ |
|
307
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
|
|
|
|
|
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Persist's the passed product bundle selection data and return's the ID. |
|
312
|
|
|
* |
|
313
|
|
|
* @param array $productBundleSelection The product bundle selection data to persist |
|
314
|
|
|
* |
|
315
|
|
|
* @return string The ID of the persisted entity |
|
316
|
|
|
*/ |
|
317
|
|
|
protected function persistProductBundleSelection($productBundleSelection) |
|
318
|
|
|
{ |
|
319
|
|
|
return $this->getProductBundleProcessor()->persistProductBundleSelection($productBundleSelection); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.