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 |
||
| 41 | abstract class AbstractProductSubject extends AbstractEavSubject |
||
| 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 available stores. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $stores = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The available store websites. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $storeWebsites = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $attributes = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The available tax classes. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $taxClasses = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The available categories. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $categories = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The ID of the product that has been created recently. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $lastEntityId; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The SKU of the product that has been created recently. |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $lastSku; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The Magento 2 configuration. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $coreConfigData; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The mapping for the SKUs to the created entity IDs. |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | protected $skuEntityIdMapping = array(); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The mapping for the SKUs to the store view codes. |
||
| 116 | * |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | protected $skuStoreViewCodeMapping = array(); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Mappings for attribute code => CSV column header. |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $headerMappings = array( |
||
| 127 | 'product_online' => 'status', |
||
| 128 | 'tax_class_name' => 'tax_class_id', |
||
| 129 | 'bundle_price_type' => 'price_type', |
||
| 130 | 'bundle_sku_type' => 'sku_type', |
||
| 131 | 'bundle_price_view' => 'price_view', |
||
| 132 | 'bundle_weight_type' => 'weight_type', |
||
| 133 | 'base_image' => 'image', |
||
| 134 | 'base_image_label' => 'image_label', |
||
| 135 | 'thumbnail_image' => 'thumbnail', |
||
| 136 | 'thumbnail_image_label'=> 'thumbnail_label', |
||
| 137 | 'bundle_shipment_type' => 'shipment_type' |
||
| 138 | ); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Initialize the subject instance. |
||
| 142 | * |
||
| 143 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger instance |
||
| 144 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration instance |
||
| 145 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 146 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 147 | */ |
||
| 148 | 3 | public function __construct( |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Set's the product processor instance. |
||
| 164 | * |
||
| 165 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | 3 | public function setProductProcessor(ProductProcessorInterface $productProcessor) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Return's the product processor instance. |
||
| 176 | * |
||
| 177 | * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance |
||
| 178 | */ |
||
| 179 | 3 | public function getProductProcessor() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Set's the SKU of the last imported product. |
||
| 186 | * |
||
| 187 | * @param string $lastSku The SKU |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function setLastSku($lastSku) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return's the SKU of the last imported product. |
||
| 198 | * |
||
| 199 | * @return string|null The SKU |
||
| 200 | */ |
||
| 201 | public function getLastSku() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set's the ID of the product that has been created recently. |
||
| 208 | * |
||
| 209 | * @param string $lastEntityId The entity ID |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | 1 | public function setLastEntityId($lastEntityId) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Return's the ID of the product that has been created recently. |
||
| 220 | * |
||
| 221 | * @return string The entity Id |
||
| 222 | */ |
||
| 223 | public function getLastEntityId() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Queries whether or not the SKU has already been processed. |
||
| 230 | * |
||
| 231 | * @param string $sku The SKU to check been processed |
||
| 232 | * |
||
| 233 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
| 234 | */ |
||
| 235 | public function hasBeenProcessed($sku) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add the passed SKU => entity ID mapping. |
||
| 242 | * |
||
| 243 | * @param string $sku The SKU |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function addSkuEntityIdMapping($sku) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Add the passed SKU => store view code mapping. |
||
| 254 | * |
||
| 255 | * @param string $sku The SKU |
||
| 256 | * @param string $storeViewCode The store view code |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 267 | * |
||
| 268 | * @return void |
||
| 269 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
| 270 | */ |
||
| 271 | public function setUp() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Clean up the global data after importing the bunch. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function tearDown() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Return's the header mappings for the actual entity. |
||
| 321 | * |
||
| 322 | * @return array The header mappings |
||
| 323 | */ |
||
| 324 | public function getHeaderMappings() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
| 331 | * |
||
| 332 | * @param integer $isUserDefined The flag itself |
||
| 333 | * |
||
| 334 | * @return array The array with the EAV attributes matching the passed flag |
||
| 335 | */ |
||
| 336 | public function getEavAttributeByIsUserDefined($isUserDefined = 1) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Return's the attribute option value with the passed value and store ID. |
||
| 343 | * |
||
| 344 | * @param mixed $value The option value |
||
| 345 | * @param integer $storeId The ID of the store |
||
| 346 | * |
||
| 347 | * @return array|boolean The attribute option value instance |
||
| 348 | */ |
||
| 349 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return's the store ID of the actual row, or of the default store |
||
| 356 | * if no store view code is set in the CSV file. |
||
| 357 | * |
||
| 358 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 359 | * |
||
| 360 | * @return integer The ID of the actual store |
||
| 361 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 362 | */ |
||
| 363 | public function getRowStoreId($default = null) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Return's the tax class ID for the passed tax class name. |
||
| 393 | * |
||
| 394 | * @param string $taxClassName The tax class name to return the ID for |
||
| 395 | * |
||
| 396 | * @return integer The tax class ID |
||
| 397 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
| 398 | */ |
||
| 399 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Return's the store website for the passed code. |
||
| 420 | * |
||
| 421 | * @param string $code The code of the store website to return the ID for |
||
| 422 | * |
||
| 423 | * @return integer The store website ID |
||
| 424 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
| 425 | */ |
||
| 426 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Return's the category with the passed path. |
||
| 447 | * |
||
| 448 | * @param string $path The path of the category to return |
||
| 449 | * |
||
| 450 | * @return array The category |
||
| 451 | * @throws \Exception Is thrown, if the requested category is not available |
||
| 452 | */ |
||
| 453 | View Code Duplication | public function getCategoryByPath($path) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Return's the category with the passed ID. |
||
| 474 | * |
||
| 475 | * @param integer $categoryId The ID of the category to return |
||
| 476 | * |
||
| 477 | * @return array The category data |
||
| 478 | * @throws \Exception Is thrown, if the category is not available |
||
| 479 | */ |
||
| 480 | public function getCategory($categoryId) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Return's the root category for the actual view store. |
||
| 503 | * |
||
| 504 | * @return array The store's root category |
||
| 505 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 506 | */ |
||
| 507 | public function getRootCategory() |
||
| 524 | } |
||
| 525 |