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 |
||
| 42 | abstract class AbstractProductSubject extends AbstractEavSubject |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The processor to read/write the necessary product data. |
||
| 47 | * |
||
| 48 | * @var \TechDivision\Import\Product\Services\ProductProcessorInterface |
||
| 49 | */ |
||
| 50 | protected $productProcessor; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The available stores. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $stores = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The available store websites. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $storeWebsites = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $attributes = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The available tax classes. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $taxClasses = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The available categories. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $categories = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The available link types. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $linkTypes = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The ID of the product that has been created recently. |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $lastEntityId; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The SKU of the product that has been created recently. |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | protected $lastSku; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The Magento 2 configuration. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | protected $coreConfigData; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The mapping for the SKUs to the created entity IDs. |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | protected $skuEntityIdMapping = array(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The mapping for the SKUs to the store view codes. |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $skuStoreViewCodeMapping = array(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Mappings for attribute code => CSV column header. |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | protected $headerMappings = array( |
||
| 135 | 'product_online' => 'status', |
||
| 136 | 'tax_class_name' => 'tax_class_id', |
||
| 137 | 'bundle_price_type' => 'price_type', |
||
| 138 | 'bundle_sku_type' => 'sku_type', |
||
| 139 | 'bundle_price_view' => 'price_view', |
||
| 140 | 'bundle_weight_type' => 'weight_type', |
||
| 141 | 'base_image' => 'image', |
||
| 142 | 'base_image_label' => 'image_label', |
||
| 143 | 'thumbnail_image' => 'thumbnail', |
||
| 144 | 'thumbnail_image_label'=> 'thumbnail_label', |
||
| 145 | 'bundle_shipment_type' => 'shipment_type', |
||
| 146 | 'related_skus' => 'relation_skus', |
||
| 147 | 'related_position' => 'relation_position', |
||
| 148 | 'crosssell_skus' => 'cross_sell_skus', |
||
| 149 | 'crosssell_position' => 'cross_sell_position', |
||
| 150 | 'upsell_skus' => 'up_sell_skus', |
||
| 151 | 'upsell_position' => 'up_sell_position' |
||
| 152 | ); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Initialize the subject instance. |
||
| 156 | * |
||
| 157 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger instance |
||
| 158 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration instance |
||
| 159 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 160 | * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data |
||
| 161 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 162 | */ |
||
| 163 | 3 | public function __construct( |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Return's the available link types. |
||
| 180 | * |
||
| 181 | * @return array The link types |
||
| 182 | */ |
||
| 183 | public function getLinkTypes() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set's the product processor instance. |
||
| 190 | * |
||
| 191 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 192 | * |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | 3 | public function setProductProcessor(ProductProcessorInterface $productProcessor) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Return's the product processor instance. |
||
| 202 | * |
||
| 203 | * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance |
||
| 204 | */ |
||
| 205 | 3 | public function getProductProcessor() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Set's the SKU of the last imported product. |
||
| 212 | * |
||
| 213 | * @param string $lastSku The SKU |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function setLastSku($lastSku) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Return's the SKU of the last imported product. |
||
| 224 | * |
||
| 225 | * @return string|null The SKU |
||
| 226 | */ |
||
| 227 | public function getLastSku() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set's the ID of the product that has been created recently. |
||
| 234 | * |
||
| 235 | * @param string $lastEntityId The entity ID |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | 1 | public function setLastEntityId($lastEntityId) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Return's the ID of the product that has been created recently. |
||
| 246 | * |
||
| 247 | * @return string The entity Id |
||
| 248 | */ |
||
| 249 | public function getLastEntityId() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Queries whether or not the SKU has already been processed. |
||
| 256 | * |
||
| 257 | * @param string $sku The SKU to check been processed |
||
| 258 | * |
||
| 259 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
| 260 | */ |
||
| 261 | public function hasBeenProcessed($sku) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Add the passed SKU => entity ID mapping. |
||
| 268 | * |
||
| 269 | * @param string $sku The SKU |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public function addSkuEntityIdMapping($sku) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Add the passed SKU => store view code mapping. |
||
| 280 | * |
||
| 281 | * @param string $sku The SKU |
||
| 282 | * @param string $storeViewCode The store view code |
||
| 283 | * |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
| 296 | */ |
||
| 297 | public function setUp() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Clean up the global data after importing the bunch. |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | public function tearDown() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return's the header mappings for the actual entity. |
||
| 340 | * |
||
| 341 | * @return array The header mappings |
||
| 342 | */ |
||
| 343 | public function getHeaderMappings() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
| 350 | * |
||
| 351 | * @param integer $isUserDefined The flag itself |
||
| 352 | * |
||
| 353 | * @return array The array with the EAV attributes matching the passed flag |
||
| 354 | */ |
||
| 355 | public function getEavAttributeByIsUserDefined($isUserDefined = 1) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return's the attribute option value with the passed value and store ID. |
||
| 362 | * |
||
| 363 | * @param mixed $value The option value |
||
| 364 | * @param integer $storeId The ID of the store |
||
| 365 | * |
||
| 366 | * @return array|boolean The attribute option value instance |
||
| 367 | */ |
||
| 368 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Return's the store ID of the actual row, or of the default store |
||
| 375 | * if no store view code is set in the CSV file. |
||
| 376 | * |
||
| 377 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 378 | * |
||
| 379 | * @return integer The ID of the actual store |
||
| 380 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 381 | */ |
||
| 382 | public function getRowStoreId($default = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return's the tax class ID for the passed tax class name. |
||
| 412 | * |
||
| 413 | * @param string $taxClassName The tax class name to return the ID for |
||
| 414 | * |
||
| 415 | * @return integer The tax class ID |
||
| 416 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
| 417 | */ |
||
| 418 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Return's the store website for the passed code. |
||
| 439 | * |
||
| 440 | * @param string $code The code of the store website to return the ID for |
||
| 441 | * |
||
| 442 | * @return integer The store website ID |
||
| 443 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
| 444 | */ |
||
| 445 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Return's the category with the passed path. |
||
| 466 | * |
||
| 467 | * @param string $path The path of the category to return |
||
| 468 | * |
||
| 469 | * @return array The category |
||
| 470 | * @throws \Exception Is thrown, if the requested category is not available |
||
| 471 | */ |
||
| 472 | View Code Duplication | public function getCategoryByPath($path) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Return's the category with the passed ID. |
||
| 493 | * |
||
| 494 | * @param integer $categoryId The ID of the category to return |
||
| 495 | * |
||
| 496 | * @return array The category data |
||
| 497 | * @throws \Exception Is thrown, if the category is not available |
||
| 498 | */ |
||
| 499 | public function getCategory($categoryId) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Return's the root category for the actual view store. |
||
| 522 | * |
||
| 523 | * @return array The store's root category |
||
| 524 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 525 | */ |
||
| 526 | public function getRootCategory() |
||
| 543 | } |
||
| 544 |