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 | * The default mappings for the user defined attributes, based on the attributes frontend input type. |
||
| 156 | * |
||
| 157 | * @var array |
||
| 158 | */ |
||
| 159 | protected $defaultFrontendInputCallbackMappings = array( |
||
| 160 | FrontendInputTypes::SELECT => 'TechDivision\\Import\\Product\\Callbacks\\SelectCallback', |
||
| 161 | FrontendInputTypes::MULTISELECT => 'TechDivision\\Import\\Product\\Callbacks\\MultiselectCallback', |
||
| 162 | FrontendInputTypes::BOOLEAN => 'TechDivision\\Import\\Product\\Callbacks\\BooleanCallback' |
||
| 163 | ); |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Initialize the subject instance. |
||
| 167 | * |
||
| 168 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration instance |
||
| 169 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 170 | * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data |
||
| 171 | * @param array $systemLoggers The array with the system logger instances |
||
| 172 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 173 | */ |
||
| 174 | 3 | public function __construct( |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Return's the default callback frontend input mappings for the user defined attributes. |
||
| 191 | * |
||
| 192 | * @return array The default frontend input callback mappings |
||
| 193 | */ |
||
| 194 | public function getDefaultFrontendInputCallbackMappings() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return's the available link types. |
||
| 201 | * |
||
| 202 | * @return array The link types |
||
| 203 | */ |
||
| 204 | public function getLinkTypes() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set's the product processor instance. |
||
| 211 | * |
||
| 212 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | 3 | public function setProductProcessor(ProductProcessorInterface $productProcessor) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Return's the product processor instance. |
||
| 223 | * |
||
| 224 | * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance |
||
| 225 | */ |
||
| 226 | 3 | public function getProductProcessor() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Set's the SKU of the last imported product. |
||
| 233 | * |
||
| 234 | * @param string $lastSku The SKU |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function setLastSku($lastSku) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return's the SKU of the last imported product. |
||
| 245 | * |
||
| 246 | * @return string|null The SKU |
||
| 247 | */ |
||
| 248 | public function getLastSku() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set's the ID of the product that has been created recently. |
||
| 255 | * |
||
| 256 | * @param string $lastEntityId The entity ID |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | 1 | public function setLastEntityId($lastEntityId) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Return's the ID of the product that has been created recently. |
||
| 267 | * |
||
| 268 | * @return string The entity Id |
||
| 269 | */ |
||
| 270 | public function getLastEntityId() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Queries whether or not the SKU has already been processed. |
||
| 277 | * |
||
| 278 | * @param string $sku The SKU to check been processed |
||
| 279 | * |
||
| 280 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
| 281 | */ |
||
| 282 | public function hasBeenProcessed($sku) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Add the passed SKU => entity ID mapping. |
||
| 289 | * |
||
| 290 | * @param string $sku The SKU |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | public function addSkuEntityIdMapping($sku) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Add the passed SKU => store view code mapping. |
||
| 301 | * |
||
| 302 | * @param string $sku The SKU |
||
| 303 | * @param string $storeViewCode The store view code |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
| 317 | */ |
||
| 318 | public function setUp() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Clean up the global data after importing the bunch. |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public function tearDown() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return's the header mappings for the actual entity. |
||
| 361 | * |
||
| 362 | * @return array The header mappings |
||
| 363 | */ |
||
| 364 | public function getHeaderMappings() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
| 371 | * |
||
| 372 | * @param integer $isUserDefined The flag itself |
||
| 373 | * |
||
| 374 | * @return array The array with the EAV attributes matching the passed flag |
||
| 375 | */ |
||
| 376 | public function getEavAttributeByIsUserDefined($isUserDefined = 1) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Return's the attribute option value with the passed value and store ID. |
||
| 383 | * |
||
| 384 | * @param mixed $value The option value |
||
| 385 | * @param integer $storeId The ID of the store |
||
| 386 | * |
||
| 387 | * @return array|boolean The attribute option value instance |
||
| 388 | */ |
||
| 389 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return's the store ID of the actual row, or of the default store |
||
| 396 | * if no store view code is set in the CSV file. |
||
| 397 | * |
||
| 398 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 399 | * |
||
| 400 | * @return integer The ID of the actual store |
||
| 401 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 402 | */ |
||
| 403 | public function getRowStoreId($default = null) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return's the tax class ID for the passed tax class name. |
||
| 430 | * |
||
| 431 | * @param string $taxClassName The tax class name to return the ID for |
||
| 432 | * |
||
| 433 | * @return integer The tax class ID |
||
| 434 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
| 435 | */ |
||
| 436 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Return's the store website for the passed code. |
||
| 454 | * |
||
| 455 | * @param string $code The code of the store website to return the ID for |
||
| 456 | * |
||
| 457 | * @return integer The store website ID |
||
| 458 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
| 459 | */ |
||
| 460 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Return's the category with the passed path. |
||
| 478 | * |
||
| 479 | * @param string $path The path of the category to return |
||
| 480 | * |
||
| 481 | * @return array The category |
||
| 482 | * @throws \Exception Is thrown, if the requested category is not available |
||
| 483 | */ |
||
| 484 | public function getCategoryByPath($path) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Return's the category with the passed ID. |
||
| 502 | * |
||
| 503 | * @param integer $categoryId The ID of the category to return |
||
| 504 | * |
||
| 505 | * @return array The category data |
||
| 506 | * @throws \Exception Is thrown, if the category is not available |
||
| 507 | */ |
||
| 508 | public function getCategory($categoryId) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Return's the root category for the actual view store. |
||
| 528 | * |
||
| 529 | * @return array The store's root category |
||
| 530 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 531 | */ |
||
| 532 | public function getRootCategory() |
||
| 553 | } |
||
| 554 |