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 |
||
38 | class BunchSubject extends AbstractProductSubject |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * The mapping for the supported backend types (for the product entity) => persist methods. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $backendTypes = array( |
||
47 | 'datetime' => array('persistProductDatetimeAttribute', 'loadProductDatetimeAttribute'), |
||
48 | 'decimal' => array('persistProductDecimalAttribute', 'loadProductDecimalAttribute'), |
||
49 | 'int' => array('persistProductIntAttribute', 'loadProductIntAttribute'), |
||
50 | 'text' => array('persistProductTextAttribute', 'loadProductTextAttribute'), |
||
51 | 'varchar' => array('persistProductVarcharAttribute', 'loadProductVarcharAttribute') |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Mappings for attribute code => CSV column header. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $headerMappings = array( |
||
60 | 'status' => 'product_online', |
||
61 | 'tax_class_id' => 'tax_class_name', |
||
62 | 'price_type' => 'bundle_price_type', |
||
63 | 'sku_type' => 'bundle_sku_type', |
||
64 | 'price_view' => 'bundle_price_view', |
||
65 | 'weight_type' => 'bundle_weight_type', |
||
66 | 'image' => 'base_image', |
||
67 | 'image_label' => 'base_image_label', |
||
68 | 'thumbnail' => 'thumbnail_image', |
||
69 | 'thumbnail_label' => 'thumbnail_image_label' |
||
70 | ); |
||
71 | |||
72 | /** |
||
73 | * Mappings for the table column => CSV column header. |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $headerStockMappings = array( |
||
78 | 'qty' => array('qty', 'float'), |
||
79 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
80 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
81 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
82 | 'backorders' => array('allow_backorders', 'int'), |
||
83 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
84 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
85 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
86 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
87 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
88 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
89 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
90 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
91 | 'manage_stock' => array('manage_stock', 'int'), |
||
92 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
93 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
94 | 'qty_increments' => array('qty_increments', 'float'), |
||
95 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
96 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
97 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
98 | ); |
||
99 | |||
100 | /** |
||
101 | * The array with the available visibility keys. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $availableVisibilities = array( |
||
106 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
107 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
108 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
109 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * The attribute set of the product that has to be created. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $attributeSet = array(); |
||
118 | |||
119 | /** |
||
120 | * The array containing the data for product type configuration (configurables, bundles, etc). |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $artefacs = array(); |
||
125 | |||
126 | /** |
||
127 | * The mapping for the SKUs to the created entity IDs. |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | protected $skuEntityIdMapping = array(); |
||
132 | |||
133 | /** |
||
134 | * The category IDs the product is related with. |
||
135 | * |
||
136 | * @var array |
||
137 | */ |
||
138 | protected $productCategoryIds = array(); |
||
139 | |||
140 | /** |
||
141 | * Set's the attribute set of the product that has to be created. |
||
142 | * |
||
143 | * @param array $attributeSet The attribute set |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | 1 | public function setAttributeSet(array $attributeSet) |
|
151 | |||
152 | /** |
||
153 | * Return's the attribute set of the product that has to be created. |
||
154 | * |
||
155 | * @return array The attribute set |
||
156 | */ |
||
157 | public function getAttributeSet() |
||
161 | |||
162 | /** |
||
163 | * Clean up the global data after importing the bunch. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function tearDown() |
||
185 | |||
186 | /** |
||
187 | * Export's the artefacts to CSV files. |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | public function exportArtefacts() |
||
225 | |||
226 | /** |
||
227 | * Initialize and return the exporter configuration. |
||
228 | * |
||
229 | * @return \Goodby\CSV\Export\Standard\ExporterConfig The exporter configuration |
||
230 | */ |
||
231 | protected function getExportConfig() |
||
270 | |||
271 | /** |
||
272 | * Cast's the passed value based on the backend type information. |
||
273 | * |
||
274 | * @param string $backendType The backend type to cast to |
||
275 | * @param mixed $value The value to be casted |
||
276 | * |
||
277 | * @return mixed The casted value |
||
278 | */ |
||
279 | public function castValueByBackendType($backendType, $value) |
||
300 | |||
301 | /** |
||
302 | * Return's the mappings for the table column => CSV column header. |
||
303 | * |
||
304 | * @return array The header stock mappings |
||
305 | */ |
||
306 | public function getHeaderStockMappings() |
||
310 | |||
311 | /** |
||
312 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
313 | * |
||
314 | * @return array The mapping for the supported backend types |
||
315 | */ |
||
316 | public function getBackendTypes() |
||
320 | |||
321 | /** |
||
322 | * Return's the artefacts for post-processing. |
||
323 | * |
||
324 | * @return array The artefacts |
||
325 | */ |
||
326 | public function getArtefacts() |
||
330 | |||
331 | /** |
||
332 | * Return's the visibility key for the passed visibility string. |
||
333 | * |
||
334 | * @param string $visibility The visibility string the return the key for |
||
335 | * |
||
336 | * @return integer The requested visibility key |
||
337 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
338 | */ |
||
339 | public function getVisibilityIdByValue($visibility) |
||
350 | |||
351 | /** |
||
352 | * Map the passed attribute code, if a header mapping exists and return the |
||
353 | * mapped mapping. |
||
354 | * |
||
355 | * @param string $attributeCode The attribute code to map |
||
356 | * |
||
357 | * @return string The mapped attribute code, or the original one |
||
358 | */ |
||
359 | public function mapAttributeCodeByHeaderMapping($attributeCode) |
||
370 | |||
371 | /** |
||
372 | * Add the passed product type artefacts to the product with the |
||
373 | * last entity ID. |
||
374 | * |
||
375 | * @param string $type The artefact type, e. g. configurable |
||
376 | * @param array $artefacts The product type artefacts |
||
377 | * |
||
378 | * @return void |
||
379 | * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
||
380 | */ |
||
381 | public function addArtefacts($type, array $artefacts) |
||
392 | |||
393 | /** |
||
394 | * Add the passed SKU => entity ID mapping. |
||
395 | * |
||
396 | * @param string $sku The SKU |
||
397 | * |
||
398 | * @return void |
||
399 | * @uses \Import\Csv\Actions\ProductImportBunchAction::getLastEntityId() |
||
400 | */ |
||
401 | public function addSkuEntityIdMapping($sku) |
||
405 | |||
406 | /** |
||
407 | * Add the passed category ID to the product's category list. |
||
408 | * |
||
409 | * @param integer $categoryId The category ID to add |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | public function addProductCategoryId($categoryId) |
||
417 | |||
418 | /** |
||
419 | * Return's the list with category IDs the product is related with. |
||
420 | * |
||
421 | * @return array The product's category IDs |
||
422 | */ |
||
423 | public function getProductCategoryIds() |
||
437 | |||
438 | /** |
||
439 | * Return's the attribute option value with the passed value and store ID. |
||
440 | * |
||
441 | * @param mixed $value The option value |
||
442 | * @param integer $storeId The ID of the store |
||
443 | * |
||
444 | * @return array|boolean The attribute option value instance |
||
445 | */ |
||
446 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
450 | |||
451 | /** |
||
452 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
453 | * |
||
454 | * @param string $entityType The entity type to load the URL rewrites for |
||
455 | * @param integer $entityId The entity ID to laod the rewrites for |
||
456 | * |
||
457 | * @return array The URL rewrites |
||
458 | */ |
||
459 | 1 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
463 | |||
464 | /** |
||
465 | * Load's and return's the product with the passed SKU. |
||
466 | * |
||
467 | * @param string $sku The SKU of the product to load |
||
468 | * |
||
469 | * @return array The product |
||
470 | */ |
||
471 | public function loadProduct($sku) |
||
475 | |||
476 | /** |
||
477 | * Load's and return's the product website relation with the passed product and website ID. |
||
478 | * |
||
479 | * @param string $productId The product ID of the relation |
||
480 | * @param string $websiteId The website ID of the relation |
||
481 | * |
||
482 | * @return array The product website |
||
483 | */ |
||
484 | public function loadProductWebsite($productId, $websiteId) |
||
488 | |||
489 | /** |
||
490 | * Return's the category product relation with the passed category/product ID. |
||
491 | * |
||
492 | * @param integer $categoryId The category ID of the category product relation to return |
||
493 | * @param integer $productId The product ID of the category product relation to return |
||
494 | * |
||
495 | * @return array The category product relation |
||
496 | */ |
||
497 | public function loadCategoryProduct($categoryId, $productId) |
||
501 | |||
502 | /** |
||
503 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
504 | * |
||
505 | * @param integer $productId The product ID of the stock status to load |
||
506 | * @param integer $websiteId The website ID of the stock status to load |
||
507 | * @param integer $stockId The stock ID of the stock status to load |
||
508 | * |
||
509 | * @return array The stock status |
||
510 | */ |
||
511 | public function loadStockStatus($productId, $websiteId, $stockId) |
||
515 | |||
516 | /** |
||
517 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
518 | * |
||
519 | * @param integer $productId The product ID of the stock item to load |
||
520 | * @param integer $websiteId The website ID of the stock item to load |
||
521 | * @param integer $stockId The stock ID of the stock item to load |
||
522 | * |
||
523 | * @return array The stock item |
||
524 | */ |
||
525 | public function loadStockItem($productId, $websiteId, $stockId) |
||
529 | |||
530 | /** |
||
531 | * Load's and return's the datetime attribute with the passed entity/attribute/store ID. |
||
532 | * |
||
533 | * @param integer $entityId The entity ID of the attribute |
||
534 | * @param integer $attributeId The attribute ID of the attribute |
||
535 | * @param integer $storeId The store ID of the attribute |
||
536 | * |
||
537 | * @return array|null The datetime attribute |
||
538 | */ |
||
539 | public function loadProductDatetimeAttribute($entityId, $attributeId, $storeId) |
||
543 | |||
544 | /** |
||
545 | * Load's and return's the decimal attribute with the passed entity/attribute/store ID. |
||
546 | * |
||
547 | * @param integer $entityId The entity ID of the attribute |
||
548 | * @param integer $attributeId The attribute ID of the attribute |
||
549 | * @param integer $storeId The store ID of the attribute |
||
550 | * |
||
551 | * @return array|null The decimal attribute |
||
552 | */ |
||
553 | public function loadProductDecimalAttribute($entityId, $attributeId, $storeId) |
||
557 | |||
558 | /** |
||
559 | * Load's and return's the integer attribute with the passed entity/attribute/store ID. |
||
560 | * |
||
561 | * @param integer $entityId The entity ID of the attribute |
||
562 | * @param integer $attributeId The attribute ID of the attribute |
||
563 | * @param integer $storeId The store ID of the attribute |
||
564 | * |
||
565 | * @return array|null The integer attribute |
||
566 | */ |
||
567 | public function loadProductIntAttribute($entityId, $attributeId, $storeId) |
||
571 | |||
572 | /** |
||
573 | * Load's and return's the text attribute with the passed entity/attribute/store ID. |
||
574 | * |
||
575 | * @param integer $entityId The entity ID of the attribute |
||
576 | * @param integer $attributeId The attribute ID of the attribute |
||
577 | * @param integer $storeId The store ID of the attribute |
||
578 | * |
||
579 | * @return array|null The text attribute |
||
580 | */ |
||
581 | public function loadProductTextAttribute($entityId, $attributeId, $storeId) |
||
585 | |||
586 | /** |
||
587 | * Load's and return's the varchar attribute with the passed entity/attribute/store ID. |
||
588 | * |
||
589 | * @param integer $entityId The entity ID of the attribute |
||
590 | * @param integer $attributeId The attribute ID of the attribute |
||
591 | * @param integer $storeId The store ID of the attribute |
||
592 | * |
||
593 | * @return array|null The varchar attribute |
||
594 | */ |
||
595 | public function loadProductVarcharAttribute($entityId, $attributeId, $storeId) |
||
599 | |||
600 | /** |
||
601 | * Persist's the passed product data and return's the ID. |
||
602 | * |
||
603 | * @param array $product The product data to persist |
||
604 | * |
||
605 | * @return string The ID of the persisted entity |
||
606 | */ |
||
607 | public function persistProduct($product) |
||
611 | |||
612 | /** |
||
613 | * Persist's the passed product varchar attribute. |
||
614 | * |
||
615 | * @param array $attribute The attribute to persist |
||
616 | * |
||
617 | * @return void |
||
618 | */ |
||
619 | public function persistProductVarcharAttribute($attribute) |
||
623 | |||
624 | /** |
||
625 | * Persist's the passed product integer attribute. |
||
626 | * |
||
627 | * @param array $attribute The attribute to persist |
||
628 | * |
||
629 | * @return void |
||
630 | */ |
||
631 | public function persistProductIntAttribute($attribute) |
||
635 | |||
636 | /** |
||
637 | * Persist's the passed product decimal attribute. |
||
638 | * |
||
639 | * @param array $attribute The attribute to persist |
||
640 | * |
||
641 | * @return void |
||
642 | */ |
||
643 | public function persistProductDecimalAttribute($attribute) |
||
647 | |||
648 | /** |
||
649 | * Persist's the passed product datetime attribute. |
||
650 | * |
||
651 | * @param array $attribute The attribute to persist |
||
652 | * |
||
653 | * @return void |
||
654 | */ |
||
655 | public function persistProductDatetimeAttribute($attribute) |
||
659 | |||
660 | /** |
||
661 | * Persist's the passed product text attribute. |
||
662 | * |
||
663 | * @param array $attribute The attribute to persist |
||
664 | * |
||
665 | * @return void |
||
666 | */ |
||
667 | public function persistProductTextAttribute($attribute) |
||
671 | |||
672 | /** |
||
673 | * Persist's the passed product website data and return's the ID. |
||
674 | * |
||
675 | * @param array $productWebsite The product website data to persist |
||
676 | * |
||
677 | * @return void |
||
678 | */ |
||
679 | public function persistProductWebsite($productWebsite) |
||
683 | |||
684 | /** |
||
685 | * Persist's the passed category product relation. |
||
686 | * |
||
687 | * @param array $categoryProduct The category product relation to persist |
||
688 | * |
||
689 | * @return void |
||
690 | */ |
||
691 | public function persistCategoryProduct($categoryProduct) |
||
695 | |||
696 | /** |
||
697 | * Persist's the passed stock item data and return's the ID. |
||
698 | * |
||
699 | * @param array $stockItem The stock item data to persist |
||
700 | * |
||
701 | * @return void |
||
702 | */ |
||
703 | public function persistStockItem($stockItem) |
||
707 | |||
708 | /** |
||
709 | * Persist's the passed stock status data and return's the ID. |
||
710 | * |
||
711 | * @param array $stockStatus The stock status data to persist |
||
712 | * |
||
713 | * @return void |
||
714 | */ |
||
715 | public function persistStockStatus($stockStatus) |
||
719 | |||
720 | /** |
||
721 | * Persist's the URL write with the passed data. |
||
722 | * |
||
723 | * @param array $row The URL rewrite to persist |
||
724 | * |
||
725 | * @return void |
||
726 | */ |
||
727 | 1 | public function persistUrlRewrite($row) |
|
731 | |||
732 | /** |
||
733 | * Delete's the entity with the passed attributes. |
||
734 | * |
||
735 | * @param array $row The attributes of the entity to delete |
||
736 | * @param string|null $name The name of the prepared statement that has to be executed |
||
737 | * |
||
738 | * @return void |
||
739 | */ |
||
740 | public function deleteProduct($row, $name = null) |
||
744 | |||
745 | /** |
||
746 | * Delete's the URL rewrite(s) with the passed attributes. |
||
747 | * |
||
748 | * @param array $row The attributes of the entity to delete |
||
749 | * @param string|null $name The name of the prepared statement that has to be executed |
||
750 | * |
||
751 | * @return void |
||
752 | */ |
||
753 | 1 | public function deleteUrlRewrite($row, $name = null) |
|
757 | |||
758 | /** |
||
759 | * Delete's the stock item(s) with the passed attributes. |
||
760 | * |
||
761 | * @param array $row The attributes of the entity to delete |
||
762 | * @param string|null $name The name of the prepared statement that has to be executed |
||
763 | * |
||
764 | * @return void |
||
765 | */ |
||
766 | public function deleteStockItem($row, $name = null) |
||
770 | |||
771 | /** |
||
772 | * Delete's the stock status with the passed attributes. |
||
773 | * |
||
774 | * @param array $row The attributes of the entity to delete |
||
775 | * @param string|null $name The name of the prepared statement that has to be executed |
||
776 | * |
||
777 | * @return void |
||
778 | */ |
||
779 | public function deleteStockStatus($row, $name = null) |
||
783 | |||
784 | /** |
||
785 | * Delete's the product website relations with the passed attributes. |
||
786 | * |
||
787 | * @param array $row The attributes of the entity to delete |
||
788 | * @param string|null $name The name of the prepared statement that has to be executed |
||
789 | * |
||
790 | * @return void |
||
791 | */ |
||
792 | public function deleteProductWebsite($row, $name = null) |
||
796 | |||
797 | /** |
||
798 | * Delete's the category product relations with the passed attributes. |
||
799 | * |
||
800 | * @param array $row The attributes of the entity to delete |
||
801 | * @param string|null $name The name of the prepared statement that has to be executed |
||
802 | * |
||
803 | * @return void |
||
804 | */ |
||
805 | public function deleteCategoryProduct($row, $name = null) |
||
809 | } |
||
810 |