Complex classes like BunchSubject 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 BunchSubject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class BunchSubject extends AbstractProductSubject |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * The mapping for the supported backend types (for the product entity) => persist methods. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $backendTypes = array( |
||
51 | 'datetime' => 'persistProductDatetimeAttribute', |
||
52 | 'decimal' => 'persistProductDecimalAttribute', |
||
53 | 'int' => 'persistProductIntAttribute', |
||
54 | 'text' => 'persistProductTextAttribute', |
||
55 | 'varchar' => 'persistProductVarcharAttribute' |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Mappings for attribute code => CSV column header. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $headerMappings = array( |
||
64 | 'status' => 'product_online', |
||
65 | 'tax_class_id' => 'tax_class_name', |
||
66 | 'price_type' => 'bundle_price_type', |
||
67 | 'sku_type' => 'bundle_sku_type', |
||
68 | 'price_view' => 'bundle_price_view', |
||
69 | 'weight_type' => 'bundle_weight_type', |
||
70 | 'image' => 'base_image', |
||
71 | 'image_label' => 'base_image_label', |
||
72 | 'thumbnail' => 'thumbnail_image', |
||
73 | 'thumbnail_label' => 'thumbnail_image_label' |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * Mappings for the table column => CSV column header. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $headerStockMappings = array( |
||
82 | 'qty' => array('qty', 'float'), |
||
83 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
84 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
85 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
86 | 'backorders' => array('allow_backorders', 'int'), |
||
87 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
88 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
89 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
90 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
91 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
92 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
93 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
94 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
95 | 'manage_stock' => array('manage_stock', 'int'), |
||
96 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
97 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
98 | 'qty_increments' => array('qty_increments', 'float'), |
||
99 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
100 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
101 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
102 | ); |
||
103 | |||
104 | /** |
||
105 | * The array with the available visibility keys. |
||
106 | * |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $availableVisibilities = array( |
||
110 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
111 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
112 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
113 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * The attribute set of the product that has to be created. |
||
118 | * |
||
119 | * @var array |
||
120 | */ |
||
121 | protected $attributeSet = array(); |
||
122 | |||
123 | /** |
||
124 | * The array containing the data for product type configuration (configurables, bundles, etc). |
||
125 | * |
||
126 | * @var array |
||
127 | */ |
||
128 | protected $artefacs = array(); |
||
129 | |||
130 | /** |
||
131 | * The mapping for the SKUs to the created entity IDs. |
||
132 | * |
||
133 | * @var array |
||
134 | */ |
||
135 | protected $skuEntityIdMapping = array(); |
||
136 | |||
137 | /** |
||
138 | * The category IDs the product is related with. |
||
139 | * |
||
140 | * @var array |
||
141 | */ |
||
142 | protected $productCategoryIds = array(); |
||
143 | |||
144 | /** |
||
145 | * Set's the attribute set of the product that has to be created. |
||
146 | * |
||
147 | * @param array $attributeSet The attribute set |
||
148 | * |
||
149 | * @return void |
||
150 | */ |
||
151 | public function setAttributeSet(array $attributeSet) |
||
155 | |||
156 | /** |
||
157 | * Return's the attribute set of the product that has to be created. |
||
158 | * |
||
159 | * @return array The attribute set |
||
160 | */ |
||
161 | public function getAttributeSet() |
||
165 | |||
166 | /** |
||
167 | * Clean up the global data after importing the bunch. |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | public function tearDown() |
||
189 | |||
190 | /** |
||
191 | * Export's the artefacts to CSV files. |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | public function exportArtefacts() |
||
229 | |||
230 | /** |
||
231 | * Initialize and return the exporter configuration. |
||
232 | * |
||
233 | * @return \Goodby\CSV\Export\Standard\ExporterConfig The exporter configuration |
||
234 | */ |
||
235 | protected function getExportConfig() |
||
274 | |||
275 | /** |
||
276 | * Cast's the passed value based on the backend type information. |
||
277 | * |
||
278 | * @param string $backendType The backend type to cast to |
||
279 | * @param mixed $value The value to be casted |
||
280 | * |
||
281 | * @return mixed The casted value |
||
282 | */ |
||
283 | public function castValueByBackendType($backendType, $value) |
||
304 | |||
305 | /** |
||
306 | * Return's the mappings for the table column => CSV column header. |
||
307 | * |
||
308 | * @return array The header stock mappings |
||
309 | */ |
||
310 | public function getHeaderStockMappings() |
||
314 | |||
315 | /** |
||
316 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
317 | * |
||
318 | * @return array The mapping for the supported backend types |
||
319 | */ |
||
320 | public function getBackendTypes() |
||
324 | |||
325 | /** |
||
326 | * Return's the artefacts for post-processing. |
||
327 | * |
||
328 | * @return array The artefacts |
||
329 | */ |
||
330 | public function getArtefacts() |
||
334 | |||
335 | /** |
||
336 | * Return's the visibility key for the passed visibility string. |
||
337 | * |
||
338 | * @param string $visibility The visibility string the return the key for |
||
339 | * |
||
340 | * @return integer The requested visibility key |
||
341 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
342 | */ |
||
343 | public function getVisibilityIdByValue($visibility) |
||
354 | |||
355 | /** |
||
356 | * Map the passed attribute code, if a header mapping exists and return the |
||
357 | * mapped mapping. |
||
358 | * |
||
359 | * @param string $attributeCode The attribute code to map |
||
360 | * |
||
361 | * @return string The mapped attribute code, or the original one |
||
362 | */ |
||
363 | public function mapAttributeCodeByHeaderMapping($attributeCode) |
||
374 | |||
375 | /** |
||
376 | * Add the passed product type artefacts to the product with the |
||
377 | * last entity ID. |
||
378 | * |
||
379 | * @param string $type The artefact type, e. g. configurable |
||
380 | * @param array $artefacts The product type artefacts |
||
381 | * |
||
382 | * @return void |
||
383 | * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
||
384 | */ |
||
385 | public function addArtefacts($type, array $artefacts) |
||
396 | |||
397 | /** |
||
398 | * Add the passed SKU => entity ID mapping. |
||
399 | * |
||
400 | * @param string $sku The SKU |
||
401 | * |
||
402 | * @return void |
||
403 | * @uses \Import\Csv\Actions\ProductImportBunchAction::getLastEntityId() |
||
404 | */ |
||
405 | public function addSkuEntityIdMapping($sku) |
||
409 | |||
410 | /** |
||
411 | * Add the passed category ID to the product's category list. |
||
412 | * |
||
413 | * @param integer $categoryId The category ID to add |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | public function addProductCategoryId($categoryId) |
||
421 | |||
422 | /** |
||
423 | * Return's the list with category IDs the product is related with. |
||
424 | * |
||
425 | * @return array The product's category IDs |
||
426 | */ |
||
427 | public function getProductCategoryIds() |
||
441 | |||
442 | /** |
||
443 | * Return's the attribute option value with the passed value and store ID. |
||
444 | * |
||
445 | * @param mixed $value The option value |
||
446 | * @param integer $storeId The ID of the store |
||
447 | * |
||
448 | * @return array|boolean The attribute option value instance |
||
449 | */ |
||
450 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
454 | |||
455 | /** |
||
456 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
457 | * |
||
458 | * @param string $entityType The entity type to load the URL rewrites for |
||
459 | * @param integer $entityId The entity ID to laod the rewrites for |
||
460 | * |
||
461 | * @return array The URL rewrites |
||
462 | */ |
||
463 | 1 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
467 | |||
468 | /** |
||
469 | * Persist's the passed product data and return's the ID. |
||
470 | * |
||
471 | * @param array $product The product data to persist |
||
472 | * |
||
473 | * @return string The ID of the persisted entity |
||
474 | */ |
||
475 | public function persistProduct($product) |
||
479 | |||
480 | /** |
||
481 | * Persist's the passed product varchar attribute. |
||
482 | * |
||
483 | * @param array $attribute The attribute to persist |
||
484 | * |
||
485 | * @return void |
||
486 | */ |
||
487 | public function persistProductVarcharAttribute($attribute) |
||
491 | |||
492 | /** |
||
493 | * Persist's the passed product integer attribute. |
||
494 | * |
||
495 | * @param array $attribute The attribute to persist |
||
496 | * |
||
497 | * @return void |
||
498 | */ |
||
499 | public function persistProductIntAttribute($attribute) |
||
503 | |||
504 | /** |
||
505 | * Persist's the passed product decimal attribute. |
||
506 | * |
||
507 | * @param array $attribute The attribute to persist |
||
508 | * |
||
509 | * @return void |
||
510 | */ |
||
511 | public function persistProductDecimalAttribute($attribute) |
||
515 | |||
516 | /** |
||
517 | * Persist's the passed product datetime attribute. |
||
518 | * |
||
519 | * @param array $attribute The attribute to persist |
||
520 | * |
||
521 | * @return void |
||
522 | */ |
||
523 | public function persistProductDatetimeAttribute($attribute) |
||
527 | |||
528 | /** |
||
529 | * Persist's the passed product text attribute. |
||
530 | * |
||
531 | * @param array $attribute The attribute to persist |
||
532 | * |
||
533 | * @return void |
||
534 | */ |
||
535 | public function persistProductTextAttribute($attribute) |
||
539 | |||
540 | /** |
||
541 | * Persist's the passed product website data and return's the ID. |
||
542 | * |
||
543 | * @param array $productWebsite The product website data to persist |
||
544 | * |
||
545 | * @return void |
||
546 | */ |
||
547 | public function persistProductWebsite($productWebsite) |
||
551 | |||
552 | /** |
||
553 | * Persist's the passed product category data and return's the ID. |
||
554 | * |
||
555 | * @param array $productCategory The product category data to persist |
||
556 | * |
||
557 | * @return void |
||
558 | */ |
||
559 | public function persistProductCategory($productCategory) |
||
563 | |||
564 | /** |
||
565 | * Persist's the passed stock item data and return's the ID. |
||
566 | * |
||
567 | * @param array $stockItem The stock item data to persist |
||
568 | * |
||
569 | * @return void |
||
570 | */ |
||
571 | public function persistStockItem($stockItem) |
||
575 | |||
576 | /** |
||
577 | * Persist's the passed stock status data and return's the ID. |
||
578 | * |
||
579 | * @param array $stockStatus The stock status data to persist |
||
580 | * |
||
581 | * @return void |
||
582 | */ |
||
583 | public function persistStockStatus($stockStatus) |
||
587 | |||
588 | /** |
||
589 | * Persist's the URL write with the passed data. |
||
590 | * |
||
591 | * @param array $row The URL rewrite to persist |
||
592 | * |
||
593 | * @return void |
||
594 | */ |
||
595 | 1 | public function persistUrlRewrite($row) |
|
599 | |||
600 | /** |
||
601 | * Remove's the entity with the passed attributes. |
||
602 | * |
||
603 | * @param array $row The attributes of the entity to remove |
||
604 | * @param string|null $name The name of the prepared statement that has to be executed |
||
605 | * |
||
606 | * @return void |
||
607 | */ |
||
608 | public function removeProduct($row, $name = null) |
||
612 | |||
613 | /** |
||
614 | * Delete's the URL rewrite(s) with the passed attributes. |
||
615 | * |
||
616 | * @param array $row The attributes of the entity to remove |
||
617 | * @param string|null $name The name of the prepared statement that has to be executed |
||
618 | * |
||
619 | * @return void |
||
620 | */ |
||
621 | 1 | public function removeUrlRewrite($row, $name = null) |
|
625 | |||
626 | /** |
||
627 | * Delete's the stock item(s) with the passed attributes. |
||
628 | * |
||
629 | * @param array $row The attributes of the entity to remove |
||
630 | * @param string|null $name The name of the prepared statement that has to be executed |
||
631 | * |
||
632 | * @return void |
||
633 | */ |
||
634 | public function removeStockItem($row, $name = null) |
||
638 | |||
639 | /** |
||
640 | * Delete's the stock status with the passed attributes. |
||
641 | * |
||
642 | * @param array $row The attributes of the entity to remove |
||
643 | * @param string|null $name The name of the prepared statement that has to be executed |
||
644 | * |
||
645 | * @return void |
||
646 | */ |
||
647 | public function removeStockStatus($row, $name = null) |
||
651 | |||
652 | /** |
||
653 | * Delete's the product website relations with the passed attributes. |
||
654 | * |
||
655 | * @param array $row The attributes of the entity to remove |
||
656 | * @param string|null $name The name of the prepared statement that has to be executed |
||
657 | * |
||
658 | * @return void |
||
659 | */ |
||
660 | public function removeProductWebsite($row, $name = null) |
||
664 | |||
665 | /** |
||
666 | * Delete's the product category relations with the passed attributes. |
||
667 | * |
||
668 | * @param array $row The attributes of the entity to remove |
||
669 | * @param string|null $name The name of the prepared statement that has to be executed |
||
670 | * |
||
671 | * @return void |
||
672 | */ |
||
673 | public function removeProductCategory($row, $name = null) |
||
677 | } |
||
678 |