Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractProductSubject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractProductSubject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | abstract class AbstractProductSubject extends AbstractEavSubject implements EntitySubjectInterface, SkuToPkMappingAwareSubjectInterface |
||
45 | { |
||
46 | |||
47 | /** |
||
48 | * The trait with the SKU => PK mapping functionality. |
||
49 | * |
||
50 | * @var \TechDivision\Import\Product\Subjects\SkuToPkMappingTrait |
||
51 | */ |
||
52 | use SkuToPkMappingTrait; |
||
53 | |||
54 | /** |
||
55 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $attributes = array(); |
||
60 | |||
61 | /** |
||
62 | * The available tax classes. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $taxClasses = array(); |
||
67 | |||
68 | /** |
||
69 | * The available categories. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $categories = array(); |
||
74 | |||
75 | /** |
||
76 | * The available link types. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $linkTypes = array(); |
||
81 | |||
82 | /** |
||
83 | * The available link attributes. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $linkAttributes = array(); |
||
88 | |||
89 | /** |
||
90 | * The ID of the product that has been created recently. |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $lastEntityId; |
||
95 | |||
96 | /** |
||
97 | * The SKU of the product that has been created recently. |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $lastSku; |
||
102 | |||
103 | /** |
||
104 | * The Magento 2 configuration. |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $coreConfigData; |
||
109 | |||
110 | /** |
||
111 | * The mapping for the SKUs to the created entity IDs. |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $skuEntityIdMapping = array(); |
||
116 | |||
117 | /** |
||
118 | * The mapping for the SKUs to the store view codes. |
||
119 | * |
||
120 | * @var array |
||
121 | */ |
||
122 | protected $skuStoreViewCodeMapping = array(); |
||
123 | |||
124 | /** |
||
125 | * The array with the available image types and their label columns. |
||
126 | * |
||
127 | * @var array |
||
128 | */ |
||
129 | protected $imageTypes = array(); |
||
130 | |||
131 | /** |
||
132 | * Mappings for CSV column header => attribute code. |
||
133 | * |
||
134 | * @var array |
||
135 | */ |
||
136 | protected $headerMappings = array(); |
||
137 | |||
138 | /** |
||
139 | * The default mappings for the user defined attributes, based on the attributes frontend input type. |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | protected $defaultFrontendInputCallbackMappings = array( |
||
144 | FrontendInputTypes::SELECT => array('import_product.callback.select'), |
||
145 | FrontendInputTypes::MULTISELECT => array('import_product.callback.multiselect'), |
||
146 | FrontendInputTypes::BOOLEAN => array('import_product.callback.boolean') |
||
147 | ); |
||
148 | |||
149 | /** |
||
150 | * Array that contains the relations that has already been processed. |
||
151 | * |
||
152 | * @var array |
||
153 | */ |
||
154 | protected $processedRelations = array(); |
||
155 | |||
156 | /** |
||
157 | * The array that contains the prepared link type mappings. |
||
158 | * |
||
159 | * @var array |
||
160 | */ |
||
161 | protected $linkTypeMappings = array(); |
||
162 | |||
163 | /** |
||
164 | * The array that contains the link type => column name prefix mapping. |
||
165 | * |
||
166 | * @var array |
||
167 | */ |
||
168 | protected $linkTypeCodeToColumnNameMapping = array('super' => 'associated'); |
||
169 | |||
170 | /** |
||
171 | * The array that contains the link type attribute column => callback mapping. |
||
172 | * |
||
173 | * @var array |
||
174 | */ |
||
175 | protected $linkTypeAttributeColumnToCallbackMapping = array( |
||
176 | 'associated_skus' => array('associated_skus', 'explodeKey'), |
||
177 | 'associated_qty' => array('associated_skus', 'explodeValue') |
||
178 | ); |
||
179 | |||
180 | /** |
||
181 | * The array with the columns that has to be cleaned-up. |
||
182 | * |
||
183 | * @var array |
||
184 | */ |
||
185 | 18 | protected $cleanUpColumns = array(); |
|
186 | |||
187 | 18 | /** |
|
188 | * Return's the default callback frontend input mappings for the user defined attributes. |
||
189 | * |
||
190 | * @return array The default frontend input callback mappings |
||
191 | */ |
||
192 | public function getDefaultFrontendInputCallbackMappings() |
||
193 | { |
||
194 | return $this->defaultFrontendInputCallbackMappings; |
||
195 | 18 | } |
|
196 | |||
197 | 18 | /** |
|
198 | * Return's the available link types. |
||
199 | * |
||
200 | * @return array The link types |
||
201 | */ |
||
202 | public function getLinkTypes() |
||
203 | { |
||
204 | return $this->linkTypes; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Set's the SKU of the last imported product. |
||
209 | * |
||
210 | * @param string $lastSku The SKU |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | public function setLastSku($lastSku) |
||
215 | { |
||
216 | $this->lastSku = $lastSku; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Return's the SKU of the last imported product. |
||
221 | * |
||
222 | * @return string|null The SKU |
||
223 | */ |
||
224 | public function getLastSku() |
||
225 | { |
||
226 | return $this->lastSku; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Set's the ID of the product that has been created recently. |
||
231 | * |
||
232 | * @param string $lastEntityId The entity ID |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | public function setLastEntityId($lastEntityId) |
||
237 | { |
||
238 | $this->lastEntityId = $lastEntityId; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Return's the ID of the product that has been created recently. |
||
243 | * |
||
244 | * @return string The entity Id |
||
245 | */ |
||
246 | public function getLastEntityId() |
||
247 | { |
||
248 | return $this->lastEntityId; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Queries whether or not the SKU has already been processed. |
||
253 | * |
||
254 | * @param string $sku The SKU to check been processed |
||
255 | * |
||
256 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
257 | */ |
||
258 | public function hasBeenProcessed($sku) |
||
259 | { |
||
260 | return isset($this->skuEntityIdMapping[$sku]); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Queries whether or not the passed PK and store view code has already been processed. |
||
265 | * |
||
266 | * @param string $pk The PK to check been processed |
||
267 | * @param string $storeViewCode The store view code to check been processed |
||
268 | * |
||
269 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
270 | */ |
||
271 | public function storeViewHasBeenProcessed($pk, $storeViewCode) |
||
272 | { |
||
273 | return isset($this->skuEntityIdMapping[$pk]) && isset($this->skuStoreViewCodeMapping[$pk]) && in_array($storeViewCode, $this->skuStoreViewCodeMapping[$pk]); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Add the passed SKU => entity ID mapping. |
||
278 | * |
||
279 | * @param string $sku The SKU |
||
280 | * @param integer|null $entityId The optional entity ID, the last processed entity ID is used, if not set |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | public function addSkuEntityIdMapping($sku, $entityId = null) |
||
285 | { |
||
286 | $this->skuEntityIdMapping[$sku] = $entityId == null ? $this->getLastEntityId() : $entityId; |
||
|
|||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Add the passed SKU => store view code mapping. |
||
291 | * |
||
292 | * @param string $sku The SKU |
||
293 | * @param string $storeViewCode The store view code |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
298 | { |
||
299 | $this->skuStoreViewCodeMapping[$sku][] = $storeViewCode; |
||
300 | } |
||
301 | |||
302 | 18 | /** |
|
303 | * Intializes the previously loaded global data for exactly one bunch. |
||
304 | * |
||
305 | * @param string $serial The serial of the actual import |
||
306 | 18 | * |
|
307 | * @return void |
||
308 | */ |
||
309 | 18 | public function setUp($serial) |
|
310 | 18 | { |
|
311 | 18 | ||
312 | 18 | // load the status of the actual import |
|
313 | 18 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
314 | 18 | ||
315 | // load the global data we've prepared initially |
||
316 | $this->linkTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::LINK_TYPES]; |
||
317 | 18 | $this->categories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::CATEGORIES]; |
|
318 | $this->taxClasses = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::TAX_CLASSES]; |
||
319 | $this->imageTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::IMAGE_TYPES]; |
||
320 | 18 | $this->storeWebsites = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES]; |
|
321 | 18 | $this->linkAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::LINK_ATTRIBUTES]; |
|
322 | |||
323 | // prepare the link type mappings |
||
324 | $this->linkTypeMappings = $this->prepareLinkTypeMappings(); |
||
325 | |||
326 | // prepare the columns that has to be cleaned-up |
||
327 | $this->cleanUpColumns = $this->prepareCleanUpColumns(); |
||
328 | |||
329 | // invoke the parent method |
||
330 | parent::setUp($serial); |
||
331 | } |
||
332 | |||
333 | |||
334 | /** |
||
335 | * Clean up the global data after importing the bunch. |
||
336 | * |
||
337 | * @param string $serial The serial of the actual import |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public function tearDown($serial) |
||
342 | { |
||
343 | |||
344 | // load the registry processor |
||
345 | $registryProcessor = $this->getRegistryProcessor(); |
||
346 | |||
347 | // update the status |
||
348 | $registryProcessor->mergeAttributesRecursive( |
||
349 | RegistryKeys::STATUS, |
||
350 | array( |
||
351 | RegistryKeys::SKU_ENTITY_ID_MAPPING => $this->skuEntityIdMapping, |
||
352 | RegistryKeys::SKU_STORE_VIEW_CODE_MAPPING => $this->skuStoreViewCodeMapping |
||
353 | ) |
||
354 | ); |
||
355 | |||
356 | // invoke the parent method |
||
357 | parent::tearDown($serial); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Return's the available image types. |
||
362 | * |
||
363 | * @return array The array with the available image types |
||
364 | */ |
||
365 | public function getImageTypes() |
||
369 | |||
370 | /** |
||
371 | * Return's the link type code => colums mapping. |
||
372 | * |
||
373 | * @return array The mapping with the link type codes => colums |
||
374 | */ |
||
375 | public function getLinkTypeMappings() |
||
376 | { |
||
377 | return $this->linkTypeMappings; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Return's the store ID of the actual row, or of the default store |
||
382 | * if no store view code is set in the CSV file. |
||
383 | * |
||
384 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
385 | * |
||
386 | * @return integer The ID of the actual store |
||
387 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
388 | */ |
||
389 | public function getRowStoreId($default = null) |
||
413 | |||
414 | /** |
||
415 | * Return's the store for the passed store code. |
||
416 | * |
||
417 | * @param string $storeCode The store code to return the store for |
||
418 | * |
||
419 | * @return array The requested store |
||
420 | * @throws \Exception Is thrown, if the requested store is not available |
||
421 | */ |
||
422 | public function getStoreByStoreCode($storeCode) |
||
437 | |||
438 | /** |
||
439 | * Return's the tax class ID for the passed tax class name. |
||
440 | * |
||
441 | * @param string $taxClassName The tax class name to return the ID for |
||
442 | * |
||
443 | * @return integer The tax class ID |
||
444 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
445 | */ |
||
446 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
461 | |||
462 | /** |
||
463 | * Return's the store website for the passed code. |
||
464 | * |
||
465 | * @param string $code The code of the store website to return the ID for |
||
466 | * |
||
467 | * @return integer The store website ID |
||
468 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
469 | */ |
||
470 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
485 | |||
486 | /** |
||
487 | * Return's the category with the passed path. |
||
488 | * |
||
489 | * @param string $path The path of the category to return |
||
490 | * @param string $storeViewCode The code of a store view, defaults to admin |
||
491 | * |
||
492 | * @return array The category |
||
493 | * @throws \Exception Is thrown, if the requested category is not available |
||
494 | */ |
||
495 | View Code Duplication | public function getCategoryByPath($path, $storeViewCode = StoreViewCodes::ADMIN) |
|
513 | |||
514 | /** |
||
515 | * Query's whether or not the category with the passed path is available or not. |
||
516 | * |
||
517 | * @param string $path The path of the category to query |
||
518 | * |
||
519 | * @return boolean TRUE if the category is available, else FALSE |
||
520 | */ |
||
521 | public function hasCategoryByPath($path) |
||
525 | |||
526 | /** |
||
527 | * Retrieve categories by given store view code. |
||
528 | * |
||
529 | * @param string $storeViewCode The store view code to retrieve the categories for |
||
530 | * |
||
531 | * @return array The array with the categories for the passed store view code |
||
532 | */ |
||
533 | public function getCategoriesByStoreViewCode($storeViewCode) |
||
537 | |||
538 | /** |
||
539 | * Return's the category with the passed ID. |
||
540 | * |
||
541 | * @param integer $categoryId The ID of the category to return |
||
542 | * @param string $storeViewCode The code of a store view, defaults to "admin" |
||
543 | * |
||
544 | * @return array The category data |
||
545 | * @throws \Exception Is thrown, if the category is not available |
||
546 | */ |
||
547 | View Code Duplication | public function getCategory($categoryId, $storeViewCode = StoreViewCodes::ADMIN) |
|
567 | |||
568 | /** |
||
569 | * Return's the root category for the actual view store. |
||
570 | * |
||
571 | * @return array The store's root category |
||
572 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
573 | */ |
||
574 | public function getRootCategory() |
||
595 | |||
596 | /** |
||
597 | * Returns an array with the codes of the store views related with the passed website code. |
||
598 | * |
||
599 | * @param string $websiteCode The code of the website to return the store view codes for |
||
600 | * |
||
601 | * @return array The array with the matching store view codes |
||
602 | */ |
||
603 | public function getStoreViewCodesByWebsiteCode($websiteCode) |
||
632 | |||
633 | /** |
||
634 | * Merge the columns from the configuration with all image type columns to define which |
||
635 | * columns should be cleaned-up. |
||
636 | * |
||
637 | * @return array The columns that has to be cleaned-up |
||
638 | */ |
||
639 | public function getCleanUpColumns() |
||
643 | |||
644 | /** |
||
645 | * Marks the relation combination processed. |
||
646 | * |
||
647 | * @param string $key The key of the relation |
||
648 | * @param string $value One of the relation values |
||
649 | * @param string $type The relation type to add |
||
650 | * |
||
651 | * @return void |
||
652 | */ |
||
653 | public function addProcessedRelation($key, $value, $type = RelationTypes::PRODUCT_RELATION) |
||
663 | |||
664 | /** |
||
665 | * Query's whether or not the relation with the passed key |
||
666 | * value combination and the given type has been processed. |
||
667 | * |
||
668 | * @param string $key The key of the relation |
||
669 | * @param string $value One of the relation values |
||
670 | * @param string $type The relation type to add |
||
671 | * |
||
672 | * @return boolean TRUE if the combination has been processed, else FALSE |
||
673 | */ |
||
674 | public function hasBeenProcessedRelation($key, $value, $type = RelationTypes::PRODUCT_RELATION) |
||
685 | |||
686 | /** |
||
687 | * Return's the link type ID for the passed link type code. |
||
688 | * |
||
689 | * @param string $linkTypeCode The link type code to return the link type ID for |
||
690 | * |
||
691 | * @return integer The mapped link type ID |
||
692 | * @throws \TechDivision\Import\Product\Exceptions\MapLinkTypeCodeToIdException Is thrown if the link type code is not mapped yet |
||
693 | */ |
||
694 | View Code Duplication | public function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
|
709 | |||
710 | /** |
||
711 | * Return's the link attribute for the passed link type ID and attribute code. |
||
712 | * |
||
713 | * @param integer $linkTypeId The link type |
||
714 | * @param string $attributeCode The attribute code |
||
715 | * |
||
716 | * @return array The link attribute |
||
717 | */ |
||
718 | View Code Duplication | public function getProductLinkAttribute($linkTypeId, $attributeCode) |
|
731 | |||
732 | /** |
||
733 | * Return's the link attribute for the passed link type and attribute code. |
||
734 | * |
||
735 | * @param string $linkTypeCode The link type code |
||
736 | * @param string $attributeCode The attribute code |
||
737 | * |
||
738 | * @return array The link attribute |
||
739 | */ |
||
740 | View Code Duplication | public function getProductLinkAttributeByLinkTypeCodeAndAttributeCode($linkTypeCode, $attributeCode) |
|
756 | |||
757 | /** |
||
758 | * Returns the product link attributes for the passed link type code. |
||
759 | * |
||
760 | * @param string $linkTypeCode The link type code |
||
761 | * |
||
762 | * @return array The product link types |
||
763 | */ |
||
764 | public function getProductLinkAttributes($linkTypeCode) |
||
784 | |||
785 | /** |
||
786 | * Maps the link type code to the apropriate column name. |
||
787 | * |
||
788 | * @param string $linkTypeCode The link type code to map |
||
789 | * |
||
790 | * @return string The mapped column name |
||
791 | */ |
||
792 | public function mapLinkTypeCodeToColumnName($linkTypeCode) |
||
803 | |||
804 | /** |
||
805 | * Return the entity ID for the passed SKU. |
||
806 | * |
||
807 | * @param string $sku The SKU to return the entity ID for |
||
808 | * |
||
809 | * @return integer The mapped entity ID |
||
810 | * @throws \TechDivision\Import\Product\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
||
811 | */ |
||
812 | public function mapSkuToEntityId($sku) |
||
827 | |||
828 | /** |
||
829 | * Return's the link type code => colums mapping. |
||
830 | * |
||
831 | * @return array The mapping with the link type codes => colums |
||
832 | */ |
||
833 | public function prepareLinkTypeMappings() |
||
874 | |||
875 | /** |
||
876 | * Return's the columns that has to be cleaned-up. |
||
877 | * |
||
878 | * @return array The mapping with the columns that has to be cleaned-up |
||
879 | */ |
||
880 | 18 | public function prepareCleanUpColumns() |
|
926 | |||
927 | /** |
||
928 | * Returns the callback method used to extract the value of the passed |
||
929 | * column to create the link type attribute value with. |
||
930 | * |
||
931 | * @param string $columnName The column name to create the callback for |
||
932 | * |
||
933 | * @return callable The callback |
||
934 | */ |
||
935 | public function getLinkTypeColumnCallback($columnName) |
||
949 | |||
950 | /** |
||
951 | * Extracts the keys of the passed value by exploding them |
||
952 | * with the also passed delimiter/value delmiter. |
||
953 | * |
||
954 | * @param string $value The value to extract |
||
955 | * @param string $delimiter The delimiter used to extract the elements |
||
956 | * @param string $valueDelimiter The delimiter used to extract the key from the value |
||
957 | * |
||
958 | * @return array The exploded keys |
||
959 | */ |
||
960 | public function explodeKey($value, $delimiter = ',', $valueDelimiter = '=') |
||
979 | |||
980 | /** |
||
981 | * Extracts the values of the passed value by exploding them |
||
982 | * with the also passed delimiter/value delimiter. |
||
983 | * |
||
984 | * @param string $value The value to extract |
||
985 | * @param string $delimiter The delimiter used to extract the elements |
||
986 | * @param string $valueDelimiter The delimiter used to extract the key from the value |
||
987 | * |
||
988 | * @return array The exploded values |
||
989 | */ |
||
990 | public function explodeValue($value, $delimiter = ',', $valueDelimiter = '=') |
||
1013 | } |
||
1014 |