1 | <?php |
||
37 | class VariantSuperAttributeObserver extends AbstractProductImportObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The ID of the actual store to use. |
||
42 | * |
||
43 | * @var integer |
||
44 | */ |
||
45 | protected $storeId; |
||
46 | |||
47 | /** |
||
48 | * The EAV attribute to handle. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $eavAttribute; |
||
53 | |||
54 | /** |
||
55 | * The tempoarary stored product super attribute ID. |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | protected $productSuperAttributeId; |
||
60 | |||
61 | /** |
||
62 | * Process the observer's business logic. |
||
63 | * |
||
64 | * @return array The processed row |
||
65 | */ |
||
66 | protected function process() |
||
67 | { |
||
68 | |||
69 | // load parent/child IDs |
||
70 | $parentId = $this->mapParentSku($parentSku = $this->getValue(ColumnKeys::VARIANT_PARENT_SKU)); |
||
71 | |||
72 | // query whether or not, the parent ID have changed |
||
73 | if ($this->isParentId($parentId)) { |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | // prepare the store view code |
||
78 | $this->prepareStoreViewCode($this->getRow()); |
||
|
|||
79 | |||
80 | // preserve the parent ID |
||
81 | $this->setParentId($parentId); |
||
82 | |||
83 | // extract the option value and attribute code from the row |
||
84 | $attributeCode = $this->getValue(ColumnKeys::VARIANT_ATTRIBUTE_CODE); |
||
85 | |||
86 | // load the store ID |
||
87 | $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN)); |
||
88 | $this->storeId = $store[MemberNames::STORE_ID]; |
||
89 | |||
90 | // load the EAV attribute with the found attribute code |
||
91 | $this->eavAttribute = $this->getEavAttributeByAttributeCode($attributeCode); |
||
92 | |||
93 | try { |
||
94 | // initialize and save the super attribute |
||
95 | $productSuperAttribute = $this->initializeProductSuperAttribute($this->prepareProducSuperAttributeAttributes()); |
||
96 | $this->productSuperAttributeId = $this->persistProductSuperAttribute($productSuperAttribute); |
||
97 | |||
98 | // initialize and save the super attribute label |
||
99 | $productSuperAttributeLabel = $this->initializeProductSuperAttributeLabel($this->prepareProductSuperAttributeLabelAttributes()); |
||
100 | $this->persistProductSuperAttributeLabel($productSuperAttributeLabel); |
||
101 | |||
102 | } catch (\Exception $e) { |
||
103 | // prepare a more detailed error messsage |
||
104 | $message = sprintf( |
||
105 | 'Super attribute for SKU %s and attribute %s can\'t be created in file %s on line %d', |
||
106 | $parentSku, |
||
107 | $attributeCode, |
||
108 | $this->getFilename(), |
||
109 | $this->getLineNumber() |
||
110 | ); |
||
111 | |||
112 | // query whether or not, debug mode is enabled |
||
113 | if ($this->isDebugMode()) { |
||
114 | // log a warning and return immediately |
||
115 | $this->getSystemLogger()->warning($message); |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | // if we're NOT in debug mode, re-throw a more detailed exception |
||
120 | throw new \Exception($message, null, $e); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Prepare the product super attribute attributes that has to be persisted. |
||
126 | * |
||
127 | * @return array The prepared product attribute attributes |
||
128 | */ |
||
129 | protected function prepareProducSuperAttributeAttributes() |
||
130 | { |
||
131 | |||
132 | // load the parent ID |
||
133 | $parentId = $this->getParentId(); |
||
134 | |||
135 | // load the attribute ID |
||
136 | $attributeId = $this->eavAttribute[MemberNames::ATTRIBUTE_ID]; |
||
137 | |||
138 | // initialize the attributes and return them |
||
139 | return $this->initializeEntity( |
||
140 | array( |
||
141 | MemberNames::PRODUCT_ID => $parentId, |
||
142 | MemberNames::ATTRIBUTE_ID => $attributeId, |
||
143 | MemberNames::POSITION => 0 |
||
144 | ) |
||
145 | ); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Prepare the product super attribute label attributes that has to be persisted. |
||
150 | * |
||
151 | * @return array The prepared product super attribute label attributes |
||
152 | */ |
||
153 | protected function prepareProductSuperAttributeLabelAttributes() |
||
154 | { |
||
155 | |||
156 | // extract the parent/child ID as well as option value and variation label from the row |
||
157 | $variationLabel = $this->getValue(ColumnKeys::VARIANT_VARIATION_LABEL); |
||
158 | |||
159 | // query whether or not we've to create super attribute labels |
||
160 | if (empty($variationLabel)) { |
||
161 | $variationLabel = $this->eavAttribute[MemberNames::FRONTENT_LABEL]; |
||
162 | } |
||
163 | |||
164 | // initialize the attributes and return them |
||
165 | return $this->initializeEntity( |
||
166 | array( |
||
167 | MemberNames::PRODUCT_SUPER_ATTRIBUTE_ID => $this->productSuperAttributeId, |
||
168 | MemberNames::STORE_ID => $this->storeId, |
||
169 | MemberNames::USE_DEFAULT => 0, |
||
170 | MemberNames::VALUE => $variationLabel |
||
171 | ) |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Initialize the product super attribute with the passed attributes and returns an instance. |
||
177 | * |
||
178 | * @param array $attr The product super attribute attributes |
||
179 | * |
||
180 | * @return array The initialized product super attribute |
||
181 | */ |
||
182 | protected function initializeProductSuperAttribute(array $attr) |
||
183 | { |
||
184 | return $attr; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Initialize the product super attribute label with the passed attributes and returns an instance. |
||
189 | * |
||
190 | * @param array $attr The product super attribute label attributes |
||
191 | * |
||
192 | * @return array The initialized product super attribute label |
||
193 | */ |
||
194 | protected function initializeProductSuperAttributeLabel(array $attr) |
||
195 | { |
||
196 | return $attr; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Map's the passed SKU of the parent product to it's PK. |
||
201 | * |
||
202 | * @param string $parentSku The SKU of the parent product |
||
203 | * |
||
204 | * @return integer The primary key used to create relations |
||
205 | */ |
||
206 | protected function mapParentSku($parentSku) |
||
207 | { |
||
208 | return $this->mapSkuToEntityId($parentSku); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Return the entity ID for the passed SKU. |
||
213 | * |
||
214 | * @param string $sku The SKU to return the entity ID for |
||
215 | * |
||
216 | * @return integer The mapped entity ID |
||
217 | * @throws \Exception Is thrown if the SKU is not mapped yet |
||
218 | */ |
||
219 | protected function mapSkuToEntityId($sku) |
||
220 | { |
||
221 | return $this->getSubject()->mapSkuToEntityId($sku); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Return's TRUE if the passed ID is the parent one. |
||
226 | * |
||
227 | * @param integer $parentId The parent ID to check |
||
228 | * |
||
229 | * @return boolean TRUE if the passed ID is the parent one |
||
230 | */ |
||
231 | protected function isParentId($parentId) |
||
232 | { |
||
233 | return $this->getParentId() === $parentId; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Set's the ID of the parent product to relate the variant with. |
||
238 | * |
||
239 | * @param integer $parentId The ID of the parent product |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | protected function setParentId($parentId) |
||
244 | { |
||
245 | $this->getSubject()->setParentId($parentId); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return's the ID of the parent product to relate the variant with. |
||
250 | * |
||
251 | * @return integer The ID of the parent product |
||
252 | */ |
||
253 | protected function getParentId() |
||
254 | { |
||
255 | return $this->getSubject()->getParentId(); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Return's the store for the passed store code. |
||
260 | * |
||
261 | * @param string $storeCode The store code to return the store for |
||
262 | * |
||
263 | * @return array The requested store |
||
264 | * @throws \Exception Is thrown, if the requested store is not available |
||
265 | */ |
||
266 | protected function getStoreByStoreCode($storeCode) |
||
267 | { |
||
268 | return $this->getSubject()->getStoreByStoreCode($storeCode); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Return's an array with the available stores. |
||
273 | * |
||
274 | * @return array The available stores |
||
275 | */ |
||
276 | protected function getStores() |
||
277 | { |
||
278 | return $this->getSubject()->getStores(); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Return's the first EAV attribute for the passed attribute code. |
||
283 | * |
||
284 | * @param string $attributeCode The attribute code |
||
285 | * |
||
286 | * @return array The array with the EAV attribute |
||
287 | */ |
||
288 | protected function getEavAttributeByAttributeCode($attributeCode) |
||
289 | { |
||
290 | return $this->getSubject()->getEavAttributeByAttributeCode($attributeCode); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Persist's the passed product super attribute data and return's the ID. |
||
295 | * |
||
296 | * @param array $productSuperAttribute The product super attribute data to persist |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | protected function persistProductSuperAttribute($productSuperAttribute) |
||
304 | |||
305 | /** |
||
306 | * Persist's the passed product super attribute label data and return's the ID. |
||
307 | * |
||
308 | * @param array $productSuperAttributeLabel The product super attribute label data to persist |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | protected function persistProductSuperAttributeLabel($productSuperAttributeLabel) |
||
316 | } |
||
317 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.