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:
1 | <?php |
||
38 | abstract class AbstractProductSubject extends AbstractSubject |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * The processor to read/write the necessary product data. |
||
43 | * |
||
44 | * @var \TechDivision\Import\Product\Services\ProductProcessorInterface |
||
45 | */ |
||
46 | protected $productProcessor; |
||
47 | |||
48 | /** |
||
49 | * The available EAV attribute sets. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $attributeSets = array(); |
||
54 | |||
55 | /** |
||
56 | * The available stores. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $stores = array(); |
||
61 | |||
62 | /** |
||
63 | * The available store websites. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $storeWebsites = array(); |
||
68 | |||
69 | /** |
||
70 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $attributes = array(); |
||
75 | |||
76 | /** |
||
77 | * The available tax classes. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $taxClasses = array(); |
||
82 | |||
83 | /** |
||
84 | * The available categories. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $categories = array(); |
||
89 | |||
90 | /** |
||
91 | * The available root categories. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $rootCategories = array(); |
||
96 | |||
97 | /** |
||
98 | * The ID of the product that has been created recently. |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $lastEntityId; |
||
103 | |||
104 | /** |
||
105 | * The SKU of the product that has been created recently. |
||
106 | * |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $lastSku; |
||
110 | |||
111 | /** |
||
112 | * The store view code the create the product/attributes for. |
||
113 | * |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $storeViewCode; |
||
117 | |||
118 | /** |
||
119 | * The default store. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $defaultStore; |
||
124 | |||
125 | /** |
||
126 | * The Magento 2 configuration. |
||
127 | * |
||
128 | * @var array |
||
129 | */ |
||
130 | protected $coreConfigData; |
||
131 | |||
132 | /** |
||
133 | * The mapping for the SKUs to the created entity IDs. |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | protected $skuEntityIdMapping = array(); |
||
138 | |||
139 | /** |
||
140 | * The mapping for the SKUs to the store view codes. |
||
141 | * |
||
142 | * @var array |
||
143 | */ |
||
144 | protected $skuStoreViewCodeMapping = array(); |
||
145 | |||
146 | /** |
||
147 | * Set's the product processor instance. |
||
148 | * |
||
149 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | 3 | public function setProductProcessor(ProductProcessorInterface $productProcessor) |
|
157 | |||
158 | /** |
||
159 | * Return's the product processor instance. |
||
160 | * |
||
161 | * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance |
||
162 | */ |
||
163 | 3 | public function getProductProcessor() |
|
167 | |||
168 | /** |
||
169 | * Set's the SKU of the last imported product. |
||
170 | * |
||
171 | * @param string $lastSku The SKU |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function setLastSku($lastSku) |
||
179 | |||
180 | /** |
||
181 | * Return's the SKU of the last imported product. |
||
182 | * |
||
183 | * @return string|null The SKU |
||
184 | */ |
||
185 | public function getLastSku() |
||
189 | |||
190 | /** |
||
191 | * Set's the ID of the product that has been created recently. |
||
192 | * |
||
193 | * @param string $lastEntityId The entity ID |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | 1 | public function setLastEntityId($lastEntityId) |
|
201 | |||
202 | /** |
||
203 | * Return's the ID of the product that has been created recently. |
||
204 | * |
||
205 | * @return string The entity Id |
||
206 | */ |
||
207 | public function getLastEntityId() |
||
211 | |||
212 | /** |
||
213 | * Queries whether or not the SKU has already been processed. |
||
214 | * |
||
215 | * @param string $sku The SKU to check been processed |
||
216 | * |
||
217 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
218 | */ |
||
219 | public function hasBeenProcessed($sku) |
||
223 | |||
224 | /** |
||
225 | * Add the passed SKU => entity ID mapping. |
||
226 | * |
||
227 | * @param string $sku The SKU |
||
228 | * |
||
229 | * @return void |
||
230 | * @uses \Import\Csv\Actions\ProductImportBunchAction::getLastEntityId() |
||
231 | */ |
||
232 | public function addSkuEntityIdMapping($sku) |
||
236 | |||
237 | /** |
||
238 | * Add the passed SKU => store view code mapping. |
||
239 | * |
||
240 | * @param string $sku The SKU |
||
241 | * @param string $storeViewCode The store view code |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
249 | |||
250 | /** |
||
251 | * Set's the store view code the create the product/attributes for. |
||
252 | * |
||
253 | * @param string $storeViewCode The store view code |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | 3 | public function setStoreViewCode($storeViewCode) |
|
261 | |||
262 | /** |
||
263 | * Return's the store view code the create the product/attributes for. |
||
264 | * |
||
265 | * @param string|null $default The default value to return, if the store view code has not been set |
||
266 | * |
||
267 | * @return string The store view code |
||
268 | */ |
||
269 | public function getStoreViewCode($default = null) |
||
283 | |||
284 | /** |
||
285 | * Return's the default store. |
||
286 | * |
||
287 | * @return array The default store |
||
288 | */ |
||
289 | public function getDefaultStore() |
||
293 | |||
294 | /** |
||
295 | * Intializes the previously loaded global data for exactly one bunch. |
||
296 | * |
||
297 | * @return void |
||
298 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
299 | */ |
||
300 | public function setUp() |
||
320 | |||
321 | /** |
||
322 | * Clean up the global data after importing the bunch. |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | public function tearDown() |
||
351 | |||
352 | /** |
||
353 | * Return's the attributes for the attribute set of the product that has to be created. |
||
354 | * |
||
355 | * @return array The attributes |
||
356 | * @throws \Exception Is thrown if the attributes for the actual attribute set are not available |
||
357 | */ |
||
358 | public function getAttributes() |
||
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) |
||
416 | |||
417 | /** |
||
418 | * Return's the tax class ID for the passed tax class name. |
||
419 | * |
||
420 | * @param string $taxClassName The tax class name to return the ID for |
||
421 | * |
||
422 | * @return integer The tax class ID |
||
423 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
424 | */ |
||
425 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
443 | |||
444 | /** |
||
445 | * Return's the store website for the passed code. |
||
446 | * |
||
447 | * @param string $code The code of the store website to return the ID for |
||
448 | * |
||
449 | * @return integer The store website ID |
||
450 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
451 | */ |
||
452 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
470 | |||
471 | /** |
||
472 | * Return's the attribute set with the passed attribute set name. |
||
473 | * |
||
474 | * @param string $attributeSetName The name of the requested attribute set |
||
475 | * |
||
476 | * @return array The attribute set data |
||
477 | * @throws \Exception Is thrown, if the attribute set with the passed name is not available |
||
478 | */ |
||
479 | View Code Duplication | public function getAttributeSetByAttributeSetName($attributeSetName) |
|
497 | |||
498 | /** |
||
499 | * Return's the category with the passed path. |
||
500 | * |
||
501 | * @param string $path The path of the category to return |
||
502 | * |
||
503 | * @return array The category |
||
504 | * @throws \Exception Is thrown, if the requested category is not available |
||
505 | */ |
||
506 | View Code Duplication | public function getCategoryByPath($path) |
|
524 | |||
525 | /** |
||
526 | * Return's the category with the passed ID. |
||
527 | * |
||
528 | * @param integer $categoryId The ID of the category to return |
||
529 | * |
||
530 | * @return array The category data |
||
531 | * @throws \Exception Is thrown, if the category is not available |
||
532 | */ |
||
533 | public function getCategory($categoryId) |
||
553 | |||
554 | /** |
||
555 | * Return's the root category for the actual view store. |
||
556 | * |
||
557 | * @return array The store's root category |
||
558 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
559 | */ |
||
560 | public function getRootCategory() |
||
577 | } |
||
578 |