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 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 |
||
41 | class BunchSubject extends AbstractSubject |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * The processor to read/write the necessary product data. |
||
46 | * |
||
47 | * @var \TechDivision\Import\Product\Services\ProductProcessorInterface |
||
48 | */ |
||
49 | protected $productProcessor; |
||
50 | |||
51 | /** |
||
52 | * The mapping for the supported backend types (for the product entity) => persist methods. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $backendTypes = array( |
||
57 | 'datetime' => 'persistProductDatetimeAttribute', |
||
58 | 'decimal' => 'persistProductDecimalAttribute', |
||
59 | 'int' => 'persistProductIntAttribute', |
||
60 | 'text' => 'persistProductTextAttribute', |
||
61 | 'varchar' => 'persistProductVarcharAttribute' |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * Mappings for attribute code => CSV column header. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $headerMappings = array( |
||
70 | 'status' => 'product_online', |
||
71 | 'tax_class_id' => 'tax_class_name', |
||
72 | 'price_type' => 'bundle_price_type', |
||
73 | 'sku_type' => 'bundle_sku_type', |
||
74 | 'price_view' => 'bundle_price_view', |
||
75 | 'weight_type' => 'bundle_weight_type', |
||
76 | 'image' => 'base_image', |
||
77 | 'image_label' => 'base_image_label', |
||
78 | 'thumbnail' => 'thumbnail_image', |
||
79 | 'thumbnail_label' => 'thumbnail_image_label' |
||
80 | ); |
||
81 | |||
82 | /** |
||
83 | * Mappings for the table column => CSV column header. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $headerStockMappings = array( |
||
88 | 'qty' => array('qty', 'float'), |
||
89 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
90 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
91 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
92 | 'backorders' => array('allow_backorders', 'int'), |
||
93 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
94 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
95 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
96 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
97 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
98 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
99 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
100 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
101 | 'manage_stock' => array('manage_stock', 'int'), |
||
102 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
103 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
104 | 'qty_increments' => array('qty_increments', 'float'), |
||
105 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
106 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
107 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
108 | ); |
||
109 | |||
110 | /** |
||
111 | * The array with the available visibility keys. |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $availableVisibilities = array( |
||
116 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
117 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
118 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
119 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
120 | ); |
||
121 | |||
122 | /** |
||
123 | * The array containing the data for product type configuration (configurables, bundles, etc). |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | protected $artefacs = array(); |
||
128 | |||
129 | /** |
||
130 | * The mapping for the SKUs to the created entity IDs. |
||
131 | * |
||
132 | * @var array |
||
133 | */ |
||
134 | protected $skuEntityIdMapping = array(); |
||
135 | |||
136 | /** |
||
137 | * The category IDs the product is related with. |
||
138 | * |
||
139 | * @var array |
||
140 | */ |
||
141 | protected $productCategoryIds = array(); |
||
142 | |||
143 | /** |
||
144 | * The UID of the file to be imported. |
||
145 | * |
||
146 | * @var string |
||
147 | */ |
||
148 | protected $uid; |
||
149 | |||
150 | /** |
||
151 | * The available EAV attribute sets. |
||
152 | * |
||
153 | * @var array |
||
154 | */ |
||
155 | protected $attributeSets = array(); |
||
156 | |||
157 | /** |
||
158 | * The available stores. |
||
159 | * |
||
160 | * @var array |
||
161 | */ |
||
162 | protected $stores = array(); |
||
163 | |||
164 | /** |
||
165 | * The available store websites. |
||
166 | * |
||
167 | * @var array |
||
168 | */ |
||
169 | protected $storeWebsites = array(); |
||
170 | |||
171 | /** |
||
172 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
173 | * |
||
174 | * @var array |
||
175 | */ |
||
176 | protected $attributes = array(); |
||
177 | |||
178 | /** |
||
179 | * The available tax classes. |
||
180 | * |
||
181 | * @var array |
||
182 | */ |
||
183 | protected $taxClasses = array(); |
||
184 | |||
185 | /** |
||
186 | * The available categories. |
||
187 | * |
||
188 | * @var array |
||
189 | */ |
||
190 | protected $categories = array(); |
||
191 | |||
192 | /** |
||
193 | * The available root categories. |
||
194 | * |
||
195 | * @var array |
||
196 | */ |
||
197 | protected $rootCategories = array(); |
||
198 | |||
199 | /** |
||
200 | * The attribute set of the product that has to be created. |
||
201 | * |
||
202 | * @var array |
||
203 | */ |
||
204 | protected $attributeSet = array(); |
||
205 | |||
206 | /** |
||
207 | * The ID of the product that has been created recently. |
||
208 | * |
||
209 | * @var string |
||
210 | */ |
||
211 | protected $lastEntityId; |
||
212 | |||
213 | /** |
||
214 | * The SKU of the product that has been created recently. |
||
215 | * |
||
216 | * @var string |
||
217 | */ |
||
218 | protected $lastSku; |
||
219 | |||
220 | /** |
||
221 | * The store view code the create the product/attributes for. |
||
222 | * |
||
223 | * @var string |
||
224 | */ |
||
225 | protected $storeViewCode; |
||
226 | |||
227 | /** |
||
228 | * Set's the product processor instance. |
||
229 | * |
||
230 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | 3 | public function setProductProcessor(ProductProcessorInterface $productProcessor) |
|
235 | { |
||
236 | 3 | $this->productProcessor = $productProcessor; |
|
237 | 3 | } |
|
238 | |||
239 | /** |
||
240 | * Return's the product processor instance. |
||
241 | * |
||
242 | * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance |
||
243 | */ |
||
244 | 3 | public function getProductProcessor() |
|
245 | { |
||
246 | 3 | return $this->productProcessor; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * Set's the SKU of the last imported product. |
||
251 | * |
||
252 | * @param string $lastSku The SKU |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | public function setLastSku($lastSku) |
||
257 | { |
||
258 | $this->lastSku = $lastSku; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Return's the SKU of the last imported product. |
||
263 | * |
||
264 | * @return string|null The SKU |
||
265 | */ |
||
266 | public function getLastSku() |
||
270 | |||
271 | /** |
||
272 | * Set's the ID of the product that has been created recently. |
||
273 | * |
||
274 | * @param string $lastEntityId The entity ID |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | public function setLastEntityId($lastEntityId) |
||
279 | { |
||
280 | $this->lastEntityId = $lastEntityId; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Return's the ID of the product that has been created recently. |
||
285 | * |
||
286 | * @return string The entity Id |
||
287 | */ |
||
288 | public function getLastEntityId() |
||
292 | |||
293 | /** |
||
294 | * Set's the attribute set of the product that has to be created. |
||
295 | * |
||
296 | * @param array $attributeSet The attribute set |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function setAttributeSet(array $attributeSet) |
||
301 | { |
||
302 | $this->attributeSet = $attributeSet; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Return's the attribute set of the product that has to be created. |
||
307 | * |
||
308 | * @return array The attribute set |
||
309 | */ |
||
310 | public function getAttributeSet() |
||
314 | |||
315 | /** |
||
316 | * Set's the store view code the create the product/attributes for. |
||
317 | * |
||
318 | * @param string $storeViewCode The store view code |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | 3 | public function setStoreViewCode($storeViewCode) |
|
326 | |||
327 | /** |
||
328 | * Return's the store view code the create the product/attributes for. |
||
329 | * |
||
330 | * @return string The store view code |
||
331 | */ |
||
332 | public function getStoreViewCode() |
||
336 | |||
337 | /** |
||
338 | * Intializes the previously loaded global data for exactly one bunch. |
||
339 | * |
||
340 | * @return void |
||
341 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
342 | */ |
||
343 | public function setUp() |
||
373 | |||
374 | /** |
||
375 | * Clean up the global data after importing the bunch. |
||
376 | * |
||
377 | * @return void |
||
378 | */ |
||
379 | public function tearDown() |
||
392 | |||
393 | /** |
||
394 | * Export's the artefacts to CSV files. |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | public function exportArtefacts() |
||
430 | |||
431 | /** |
||
432 | * Initialize and return the exporter configuration. |
||
433 | * |
||
434 | * @return \Goodby\CSV\Export\Standard\ExporterConfig The exporter configuration |
||
435 | */ |
||
436 | protected function getExportConfig() |
||
475 | |||
476 | /** |
||
477 | * Cast's the passed value based on the backend type information. |
||
478 | * |
||
479 | * @param string $backendType The backend type to cast to |
||
480 | * @param mixed $value The value to be casted |
||
481 | * |
||
482 | * @return mixed The casted value |
||
483 | */ |
||
484 | public function castValueByBackendType($backendType, $value) |
||
505 | |||
506 | /** |
||
507 | * Return's the mappings for the table column => CSV column header. |
||
508 | * |
||
509 | * @return array The header stock mappings |
||
510 | */ |
||
511 | public function getHeaderStockMappings() |
||
515 | |||
516 | /** |
||
517 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
518 | * |
||
519 | * @return array The mapping for the supported backend types |
||
520 | */ |
||
521 | public function getBackendTypes() |
||
525 | |||
526 | /** |
||
527 | * Return's the attributes for the attribute set of the product that has to be created. |
||
528 | * |
||
529 | * @return array The attributes |
||
530 | * @throws \Exception Is thrown if the attributes for the actual attribute set are not available |
||
531 | */ |
||
532 | public function getAttributes() |
||
546 | |||
547 | /** |
||
548 | * Return's the artefacts for post-processing. |
||
549 | * |
||
550 | * @return array The artefacts |
||
551 | */ |
||
552 | public function getArtefacts() |
||
556 | |||
557 | /** |
||
558 | * Return's the store ID of the actual row. |
||
559 | * |
||
560 | * @return integer The ID of the actual store |
||
561 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
562 | */ |
||
563 | View Code Duplication | public function getRowStoreId() |
|
577 | |||
578 | /** |
||
579 | * Return's the tax class ID for the passed tax class name. |
||
580 | * |
||
581 | * @param string $taxClassName The tax class name to return the ID for |
||
582 | * |
||
583 | * @return integer The tax class ID |
||
584 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
585 | */ |
||
586 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
597 | |||
598 | /** |
||
599 | * Return's the store website for the passed code. |
||
600 | * |
||
601 | * @param string $code The code of the store website to return the ID for |
||
602 | * |
||
603 | * @return integer The store website ID |
||
604 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
605 | */ |
||
606 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
617 | |||
618 | /** |
||
619 | * Return's the visibility key for the passed visibility string. |
||
620 | * |
||
621 | * @param string $visibility The visibility string the return the key for |
||
622 | * |
||
623 | * @return integer The requested visibility key |
||
624 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
625 | */ |
||
626 | public function getVisibilityIdByValue($visibility) |
||
637 | |||
638 | /** |
||
639 | * Return's the attribute set with the passed attribute set name. |
||
640 | * |
||
641 | * @param string $attributeSetName The name of the requested attribute set |
||
642 | * |
||
643 | * @return array The attribute set data |
||
644 | * @throws \Exception Is thrown, if the attribute set with the passed name is not available |
||
645 | */ |
||
646 | public function getAttributeSetByAttributeSetName($attributeSetName) |
||
656 | |||
657 | /** |
||
658 | * Return's the category with the passed path. |
||
659 | * |
||
660 | * @param string $path The path of the category to return |
||
661 | * |
||
662 | * @return array The category |
||
663 | * @throws \Exception Is thrown, if the requested category is not available |
||
664 | */ |
||
665 | public function getCategoryByPath($path) |
||
676 | |||
677 | /** |
||
678 | * Map the passed attribute code, if a header mapping exists and return the |
||
679 | * mapped mapping. |
||
680 | * |
||
681 | * @param string $attributeCode The attribute code to map |
||
682 | * |
||
683 | * @return string The mapped attribute code, or the original one |
||
684 | */ |
||
685 | public function mapAttributeCodeByHeaderMapping($attributeCode) |
||
696 | |||
697 | /** |
||
698 | * Add the passed product type artefacts to the product with the |
||
699 | * last entity ID. |
||
700 | * |
||
701 | * @param string $type The artefact type, e. g. configurable |
||
702 | * @param array $artefacts The product type artefacts |
||
703 | * |
||
704 | * @return void |
||
705 | * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
||
706 | */ |
||
707 | public function addArtefacts($type, array $artefacts) |
||
718 | |||
719 | /** |
||
720 | * Return's the attribute option value with the passed value and store ID. |
||
721 | * |
||
722 | * @param mixed $value The option value |
||
723 | * @param integer $storeId The ID of the store |
||
724 | * |
||
725 | * @return array|boolean The attribute option value instance |
||
726 | */ |
||
727 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
731 | |||
732 | /** |
||
733 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
734 | * |
||
735 | * @param string $entityType The entity type to load the URL rewrites for |
||
736 | * @param integer $entityId The entity ID to laod the rewrites for |
||
737 | * |
||
738 | * @return array The URL rewrites |
||
739 | */ |
||
740 | 1 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
744 | |||
745 | /** |
||
746 | * Add the passed SKU => entity ID mapping. |
||
747 | * |
||
748 | * @param string $sku The SKU |
||
749 | * |
||
750 | * @return void |
||
751 | * @uses \Import\Csv\Actions\ProductImportBunchAction::getLastEntityId() |
||
752 | */ |
||
753 | public function addSkuEntityIdMapping($sku) |
||
757 | |||
758 | /** |
||
759 | * Add the passed category ID to the product's category list. |
||
760 | * |
||
761 | * @param integer $categoryId The category ID to add |
||
762 | * |
||
763 | * @return void |
||
764 | */ |
||
765 | public function addProductCategoryId($categoryId) |
||
769 | |||
770 | /** |
||
771 | * Return's the list with category IDs the product is related with. |
||
772 | * |
||
773 | * @return array The product's category IDs |
||
774 | */ |
||
775 | public function getProductCategoryIds() |
||
779 | |||
780 | /** |
||
781 | * Return's the category with the passed ID. |
||
782 | * |
||
783 | * @param integer $categoryId The ID of the category to return |
||
784 | * |
||
785 | * @return array The category data |
||
786 | * @throws \Exception Is thrown, if the category is not available |
||
787 | */ |
||
788 | public function getCategory($categoryId) |
||
801 | |||
802 | /** |
||
803 | * Return's the root category for the actual view store. |
||
804 | * |
||
805 | * @return array The store's root category |
||
806 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
807 | */ |
||
808 | View Code Duplication | public function getRootCategory() |
|
822 | |||
823 | /** |
||
824 | * Persist's the passed product data and return's the ID. |
||
825 | * |
||
826 | * @param array $product The product data to persist |
||
827 | * |
||
828 | * @return string The ID of the persisted entity |
||
829 | */ |
||
830 | public function persistProduct($product) |
||
834 | |||
835 | /** |
||
836 | * Persist's the passed product varchar attribute. |
||
837 | * |
||
838 | * @param array $attribute The attribute to persist |
||
839 | * |
||
840 | * @return void |
||
841 | */ |
||
842 | public function persistProductVarcharAttribute($attribute) |
||
846 | |||
847 | /** |
||
848 | * Persist's the passed product integer attribute. |
||
849 | * |
||
850 | * @param array $attribute The attribute to persist |
||
851 | * |
||
852 | * @return void |
||
853 | */ |
||
854 | public function persistProductIntAttribute($attribute) |
||
858 | |||
859 | /** |
||
860 | * Persist's the passed product decimal attribute. |
||
861 | * |
||
862 | * @param array $attribute The attribute to persist |
||
863 | * |
||
864 | * @return void |
||
865 | */ |
||
866 | public function persistProductDecimalAttribute($attribute) |
||
870 | |||
871 | /** |
||
872 | * Persist's the passed product datetime attribute. |
||
873 | * |
||
874 | * @param array $attribute The attribute to persist |
||
875 | * |
||
876 | * @return void |
||
877 | */ |
||
878 | public function persistProductDatetimeAttribute($attribute) |
||
882 | |||
883 | /** |
||
884 | * Persist's the passed product text attribute. |
||
885 | * |
||
886 | * @param array $attribute The attribute to persist |
||
887 | * |
||
888 | * @return void |
||
889 | */ |
||
890 | public function persistProductTextAttribute($attribute) |
||
894 | |||
895 | /** |
||
896 | * Persist's the passed product website data and return's the ID. |
||
897 | * |
||
898 | * @param array $productWebsite The product website data to persist |
||
899 | * |
||
900 | * @return void |
||
901 | */ |
||
902 | public function persistProductWebsite($productWebsite) |
||
906 | |||
907 | /** |
||
908 | * Persist's the passed product category data and return's the ID. |
||
909 | * |
||
910 | * @param array $productCategory The product category data to persist |
||
911 | * |
||
912 | * @return void |
||
913 | */ |
||
914 | public function persistProductCategory($productCategory) |
||
918 | |||
919 | /** |
||
920 | * Persist's the passed stock item data and return's the ID. |
||
921 | * |
||
922 | * @param array $stockItem The stock item data to persist |
||
923 | * |
||
924 | * @return void |
||
925 | */ |
||
926 | public function persistStockItem($stockItem) |
||
930 | |||
931 | /** |
||
932 | * Persist's the passed stock status data and return's the ID. |
||
933 | * |
||
934 | * @param array $stockStatus The stock status data to persist |
||
935 | * |
||
936 | * @return void |
||
937 | */ |
||
938 | public function persistStockStatus($stockStatus) |
||
942 | |||
943 | /** |
||
944 | * Persist's the URL write with the passed data. |
||
945 | * |
||
946 | * @param array $row The URL rewrite to persist |
||
947 | * |
||
948 | * @return void |
||
949 | */ |
||
950 | 1 | public function persistUrlRewrite($row) |
|
954 | |||
955 | /** |
||
956 | * Remove's the entity with the passed attributes. |
||
957 | * |
||
958 | * @param array $row The attributes of the entity to remove |
||
959 | * @param string|null $name The name of the prepared statement that has to be executed |
||
960 | * |
||
961 | * @return void |
||
962 | */ |
||
963 | public function removeProduct($row, $name = null) |
||
967 | |||
968 | /** |
||
969 | * Delete's the URL rewrite(s) with the passed attributes. |
||
970 | * |
||
971 | * @param array $row The attributes of the entity to remove |
||
972 | * @param string|null $name The name of the prepared statement that has to be executed |
||
973 | * |
||
974 | * @return void |
||
975 | */ |
||
976 | 1 | public function removeUrlRewrite($row, $name = null) |
|
980 | |||
981 | /** |
||
982 | * Delete's the stock item(s) with the passed attributes. |
||
983 | * |
||
984 | * @param array $row The attributes of the entity to remove |
||
985 | * @param string|null $name The name of the prepared statement that has to be executed |
||
986 | * |
||
987 | * @return void |
||
988 | */ |
||
989 | public function removeStockItem($row, $name = null) |
||
993 | |||
994 | /** |
||
995 | * Delete's the stock status with the passed attributes. |
||
996 | * |
||
997 | * @param array $row The attributes of the entity to remove |
||
998 | * @param string|null $name The name of the prepared statement that has to be executed |
||
999 | * |
||
1000 | * @return void |
||
1001 | */ |
||
1002 | public function removeStockStatus($row, $name = null) |
||
1006 | |||
1007 | /** |
||
1008 | * Delete's the product website relations with the passed attributes. |
||
1009 | * |
||
1010 | * @param array $row The attributes of the entity to remove |
||
1011 | * @param string|null $name The name of the prepared statement that has to be executed |
||
1012 | * |
||
1013 | * @return void |
||
1014 | */ |
||
1015 | public function removeProductWebsite($row, $name = null) |
||
1019 | |||
1020 | /** |
||
1021 | * Delete's the product category relations with the passed attributes. |
||
1022 | * |
||
1023 | * @param array $row The attributes of the entity to remove |
||
1024 | * @param string|null $name The name of the prepared statement that has to be executed |
||
1025 | * |
||
1026 | * @return void |
||
1027 | */ |
||
1028 | public function removeProductCategory($row, $name = null) |
||
1032 | } |
||
1033 |