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\Product\Observers\AbstractProductImportObserver; |
25
|
|
|
use TechDivision\Import\Product\Bundle\Utils\ColumnKeys; |
26
|
|
|
use TechDivision\Import\Product\Bundle\Utils\MemberNames; |
27
|
|
|
use TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Oberserver that provides functionality for the bundle selection replace operation. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link https://github.com/techdivision/import-product-bundle |
36
|
|
|
* @link http://www.techdivision.com |
37
|
|
|
*/ |
38
|
|
|
class BundleSelectionObserver extends AbstractProductImportObserver |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The product bundle processor instance. |
43
|
|
|
* |
44
|
|
|
* @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface |
45
|
|
|
*/ |
46
|
|
|
protected $productBundleProcessor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initialize the observer with the passed product bundle processor instance. |
50
|
|
|
* |
51
|
|
|
* @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance |
52
|
|
|
*/ |
53
|
|
|
public function __construct(ProductBundleProcessorInterface $productBundleProcessor) |
54
|
|
|
{ |
55
|
|
|
$this->productBundleProcessor = $productBundleProcessor; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return's the product bundle processor instance. |
60
|
|
|
* |
61
|
|
|
* @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance |
62
|
|
|
*/ |
63
|
|
|
protected function getProductBundleProcessor() |
64
|
|
|
{ |
65
|
|
|
return $this->productBundleProcessor; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Process the observer's business logic. |
70
|
|
|
* |
71
|
|
|
* @return array The processed row |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
protected function process() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
// prepare the store view code |
77
|
|
|
$this->prepareStoreViewCode($this->getRow()); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// return immediately if we're have no store view code set |
80
|
|
|
if (StoreViewCodes::ADMIN !== $this->getStoreViewCode(StoreViewCodes::ADMIN)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// prepare, initialize and persist the product bundle selection data |
85
|
|
|
$productBundleSelection = $this->initializeBundleSelection($this->prepareAttributes()); |
86
|
|
|
$selectionId = $this->persistProductBundleSelection($productBundleSelection); |
87
|
|
|
|
88
|
|
|
// add the mapping for the child SKU => selection ID |
89
|
|
|
$this->addChildSkuSelectionIdMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU), $selectionId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
94
|
|
|
* |
95
|
|
|
* @return array The prepared attributes |
96
|
|
|
*/ |
97
|
|
|
protected function prepareAttributes() |
98
|
|
|
{ |
99
|
|
|
|
100
|
|
|
// load the product bundle option SKU |
101
|
|
|
$parentSku = $this->getValue(ColumnKeys::BUNDLE_PARENT_SKU); |
102
|
|
|
|
103
|
|
|
// load parent/option ID |
104
|
|
|
$parentId = $this->mapSkuToEntityId($parentSku); |
105
|
|
|
|
106
|
|
|
// load the actual option ID |
107
|
|
|
$optionId = $this->getLastOptionId(); |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
// try to load the child ID |
111
|
|
|
$childId = $this->mapSkuToEntityId($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU)); |
112
|
|
|
} catch (\Exception $e) { |
113
|
|
|
throw $this->wrapException(array(ColumnKeys::BUNDLE_VALUE_SKU), $e); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
// try to load the selection price type |
118
|
|
|
$selectionPriceType = $this->mapPriceType($this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE)); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
throw $this->wrapException(array(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE), $e); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// load the default values |
124
|
|
|
$selectionCanChangeQty = 1; |
125
|
|
|
$selectionPriceValue = $this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE); |
126
|
|
|
$selectionQty = $this->getValue(ColumnKeys::BUNDLE_VALUE_DEFAULT_QTY); |
127
|
|
|
$isDefault = $this->getValue(ColumnKeys::BUNDLE_VALUE_DEFAULT); |
128
|
|
|
|
129
|
|
|
// laod the position counter |
130
|
|
|
$position = $this->raisePositionCounter(); |
131
|
|
|
|
132
|
|
|
// prepare the product bundle selection data |
133
|
|
|
return $this->initializeEntity( |
134
|
|
|
array( |
135
|
|
|
MemberNames::OPTION_ID => $optionId, |
136
|
|
|
MemberNames::PARENT_PRODUCT_ID => $parentId, |
137
|
|
|
MemberNames::PRODUCT_ID => $childId, |
138
|
|
|
MemberNames::POSITION => $position, |
139
|
|
|
MemberNames::IS_DEFAULT => $isDefault, |
140
|
|
|
MemberNames::SELECTION_PRICE_TYPE => $selectionPriceType, |
141
|
|
|
MemberNames::SELECTION_PRICE_VALUE => $selectionPriceValue, |
142
|
|
|
MemberNames::SELECTION_QTY => $selectionQty, |
143
|
|
|
MemberNames::SELECTION_CAN_CHANGE_QTY => $selectionCanChangeQty |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Initialize the bundle selection with the passed attributes and returns an instance. |
150
|
|
|
* |
151
|
|
|
* @param array $attr The bundle selection attributes |
152
|
|
|
* |
153
|
|
|
* @return array The initialized bundle selection |
154
|
|
|
*/ |
155
|
|
|
protected function initializeBundleSelection(array $attr) |
156
|
|
|
{ |
157
|
|
|
return $attr; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return's the last created option ID. |
162
|
|
|
* |
163
|
|
|
* @return integer $optionId The last created option ID |
164
|
|
|
*/ |
165
|
|
|
protected function getLastOptionId() |
166
|
|
|
{ |
167
|
|
|
return $this->getSubject()->getLastOptionId(); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Save's the mapping of the child SKU and the selection ID. |
172
|
|
|
* |
173
|
|
|
* @param string $childSku The child SKU of the selection |
174
|
|
|
* @param integer $selectionId The selection ID to save |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function addChildSkuSelectionIdMapping($childSku, $selectionId) |
179
|
|
|
{ |
180
|
|
|
$this->getSubject()->addChildSkuSelectionIdMapping($childSku, $selectionId); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns the acutal value of the position counter and raise's it by one. |
185
|
|
|
* |
186
|
|
|
* @return integer The actual value of the position counter |
187
|
|
|
*/ |
188
|
|
|
protected function raisePositionCounter() |
189
|
|
|
{ |
190
|
|
|
return $this->getSubject()->raisePositionCounter(); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return's the option ID for the passed name. |
195
|
|
|
* |
196
|
|
|
* @param string $name The name to return the option ID for |
197
|
|
|
* |
198
|
|
|
* @return integer The option ID for the passed name |
199
|
|
|
* @throws \Exception Is thrown, if no option ID for the passed name is available |
200
|
|
|
*/ |
201
|
|
|
protected function getOptionIdForName($name) |
202
|
|
|
{ |
203
|
|
|
return $this->getSubject()->getOptionIdForName($name); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Return's the mapping for the passed price type. |
208
|
|
|
* |
209
|
|
|
* @param string $priceType The price type to map |
210
|
|
|
* |
211
|
|
|
* @return integer The mapped price type |
212
|
|
|
* @throws \Exception Is thrown, if the passed price type can't be mapped |
213
|
|
|
*/ |
214
|
|
|
protected function mapPriceType($priceType) |
215
|
|
|
{ |
216
|
|
|
return $this->getSubject()->mapPriceType($priceType); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Return the entity ID for the passed SKU. |
221
|
|
|
* |
222
|
|
|
* @param string $sku The SKU to return the entity ID for |
223
|
|
|
* |
224
|
|
|
* @return integer The mapped entity ID |
225
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
226
|
|
|
*/ |
227
|
|
|
protected function mapSkuToEntityId($sku) |
228
|
|
|
{ |
229
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Persist's the passed product bundle selection data and return's the ID. |
234
|
|
|
* |
235
|
|
|
* @param array $productBundleSelection The product bundle selection data to persist |
236
|
|
|
* |
237
|
|
|
* @return string The ID of the persisted entity |
238
|
|
|
*/ |
239
|
|
|
protected function persistProductBundleSelection($productBundleSelection) |
240
|
|
|
{ |
241
|
|
|
return $this->getProductBundleProcessor()->persistProductBundleSelection($productBundleSelection); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
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.