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 AbstractProductSubject 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 AbstractProductSubject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class AbstractProductSubject extends AbstractEavSubject implements EntitySubjectInterface, SkuToPkMappingAwareSubjectInterface |
||
| 45 | { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The trait with the SKU => PK mapping functionality. |
||
| 49 | * |
||
| 50 | * @var \TechDivision\Import\Product\Subjects\SkuToPkMappingTrait |
||
| 51 | */ |
||
| 52 | use SkuToPkMappingTrait; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $attributes = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The available tax classes. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $taxClasses = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The available categories. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $categories = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The available link types. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $linkTypes = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The available link attributes. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $linkAttributes = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The ID of the product that has been created recently. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $lastEntityId; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The SKU of the product that has been created recently. |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $lastSku; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The Magento 2 configuration. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | protected $coreConfigData; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The mapping for the SKUs to the created entity IDs. |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected $skuEntityIdMapping = array(); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The mapping for the SKUs to the store view codes. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | protected $skuStoreViewCodeMapping = array(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The array with the available image types and their label columns. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $imageTypes = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Mappings for CSV column header => attribute code. |
||
| 133 | * |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | protected $headerMappings = array( |
||
| 137 | 'product_online' => 'status', |
||
| 138 | 'tax_class_name' => 'tax_class_id', |
||
| 139 | 'bundle_price_type' => 'price_type', |
||
| 140 | 'bundle_sku_type' => 'sku_type', |
||
| 141 | 'bundle_price_view' => 'price_view', |
||
| 142 | 'bundle_weight_type' => 'weight_type', |
||
| 143 | 'bundle_shipment_type' => 'shipment_type', |
||
| 144 | 'related_skus' => 'relation_skus', |
||
| 145 | 'related_position' => 'relation_position', |
||
| 146 | 'crosssell_skus' => 'cross_sell_skus', |
||
| 147 | 'crosssell_position' => 'cross_sell_position', |
||
| 148 | 'upsell_skus' => 'up_sell_skus', |
||
| 149 | 'upsell_position' => 'up_sell_position', |
||
| 150 | 'msrp_price' => 'msrp', |
||
| 151 | 'base_image' => 'image', |
||
| 152 | 'base_image_label' => 'image_label', |
||
| 153 | 'thumbnail_image' => 'thumbnail', |
||
| 154 | 'thumbnail_image_label'=> 'thumbnail_label' |
||
| 155 | ); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * The default mappings for the user defined attributes, based on the attributes frontend input type. |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | protected $defaultFrontendInputCallbackMappings = array( |
||
| 163 | FrontendInputTypes::SELECT => array('import_product.callback.select'), |
||
| 164 | FrontendInputTypes::MULTISELECT => array('import_product.callback.multiselect'), |
||
| 165 | FrontendInputTypes::BOOLEAN => array('import_product.callback.boolean') |
||
| 166 | ); |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Array that contains the relations that has already been processed. |
||
| 170 | * |
||
| 171 | * @var array |
||
| 172 | */ |
||
| 173 | protected $processedRelations = array(); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * The array that contains the prepared link type mappings. |
||
| 177 | * |
||
| 178 | * @var array |
||
| 179 | */ |
||
| 180 | protected $linkTypeMappings = array(); |
||
| 181 | |||
| 182 | /** |
||
| 183 | * The array that contains the link type => column name prefix mapping. |
||
| 184 | * |
||
| 185 | * @var array |
||
| 186 | */ |
||
| 187 | protected $linkTypeCodeToColumnNameMapping = array('super' => 'associated'); |
||
| 188 | |||
| 189 | /** |
||
| 190 | * The array that contains the link type attribute column => callback mapping. |
||
| 191 | * |
||
| 192 | * @var array |
||
| 193 | */ |
||
| 194 | protected $linkTypeAttributeColumnToCallbackMapping = array( |
||
| 195 | 'associated_skus' => array('associated_skus', 'explodeKey'), |
||
| 196 | 'associated_qty' => array('associated_skus', 'explodeValue') |
||
| 197 | ); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return's the default callback frontend input mappings for the user defined attributes. |
||
| 201 | * |
||
| 202 | * @return array The default frontend input callback mappings |
||
| 203 | */ |
||
| 204 | 18 | public function getDefaultFrontendInputCallbackMappings() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Return's the available link types. |
||
| 211 | * |
||
| 212 | * @return array The link types |
||
| 213 | */ |
||
| 214 | 18 | public function getLinkTypes() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Set's the SKU of the last imported product. |
||
| 221 | * |
||
| 222 | * @param string $lastSku The SKU |
||
| 223 | * |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function setLastSku($lastSku) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return's the SKU of the last imported product. |
||
| 233 | * |
||
| 234 | * @return string|null The SKU |
||
| 235 | */ |
||
| 236 | public function getLastSku() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set's the ID of the product that has been created recently. |
||
| 243 | * |
||
| 244 | * @param string $lastEntityId The entity ID |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function setLastEntityId($lastEntityId) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Return's the ID of the product that has been created recently. |
||
| 255 | * |
||
| 256 | * @return string The entity Id |
||
| 257 | */ |
||
| 258 | public function getLastEntityId() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Queries whether or not the SKU has already been processed. |
||
| 265 | * |
||
| 266 | * @param string $sku The SKU to check been processed |
||
| 267 | * |
||
| 268 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
| 269 | */ |
||
| 270 | public function hasBeenProcessed($sku) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Queries whether or not the passed PK and store view code has already been processed. |
||
| 277 | * |
||
| 278 | * @param string $pk The PK to check been processed |
||
| 279 | * @param string $storeViewCode The store view code to check been processed |
||
| 280 | * |
||
| 281 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
| 282 | */ |
||
| 283 | public function storeViewHasBeenProcessed($pk, $storeViewCode) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Add the passed SKU => entity ID mapping. |
||
| 290 | * |
||
| 291 | * @param string $sku The SKU |
||
| 292 | * @param integer|null $entityId The optional entity ID, the last processed entity ID is used, if not set |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | public function addSkuEntityIdMapping($sku, $entityId = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Add the passed SKU => store view code mapping. |
||
| 303 | * |
||
| 304 | * @param string $sku The SKU |
||
| 305 | * @param string $storeViewCode The store view code |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 316 | * |
||
| 317 | * @param string $serial The serial of the actual import |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | 18 | public function setUp($serial) |
|
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Clean up the global data after importing the bunch. |
||
| 345 | * |
||
| 346 | * @param string $serial The serial of the actual import |
||
| 347 | * |
||
| 348 | * @return void |
||
| 349 | */ |
||
| 350 | public function tearDown($serial) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return's the available image types. |
||
| 371 | * |
||
| 372 | * @return array The array with the available image types |
||
| 373 | */ |
||
| 374 | public function getImageTypes() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Return's the link type code => colums mapping. |
||
| 381 | * |
||
| 382 | * @return array The mapping with the link type codes => colums |
||
| 383 | */ |
||
| 384 | public function getLinkTypeMappings() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return's the store ID of the actual row, or of the default store |
||
| 391 | * if no store view code is set in the CSV file. |
||
| 392 | * |
||
| 393 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 394 | * |
||
| 395 | * @return integer The ID of the actual store |
||
| 396 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 397 | */ |
||
| 398 | public function getRowStoreId($default = null) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return's the store for the passed store code. |
||
| 425 | * |
||
| 426 | * @param string $storeCode The store code to return the store for |
||
| 427 | * |
||
| 428 | * @return array The requested store |
||
| 429 | * @throws \Exception Is thrown, if the requested store is not available |
||
| 430 | */ |
||
| 431 | public function getStoreByStoreCode($storeCode) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Return's the tax class ID for the passed tax class name. |
||
| 449 | * |
||
| 450 | * @param string $taxClassName The tax class name to return the ID for |
||
| 451 | * |
||
| 452 | * @return integer The tax class ID |
||
| 453 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
| 454 | */ |
||
| 455 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Return's the store website for the passed code. |
||
| 473 | * |
||
| 474 | * @param string $code The code of the store website to return the ID for |
||
| 475 | * |
||
| 476 | * @return integer The store website ID |
||
| 477 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
| 478 | */ |
||
| 479 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Return's the category with the passed path. |
||
| 497 | * |
||
| 498 | * @param string $path The path of the category to return |
||
| 499 | * @param string $storeViewCode The code of a store view, defaults to admin |
||
| 500 | * |
||
| 501 | * @return array The category |
||
| 502 | * @throws \Exception Is thrown, if the requested category is not available |
||
| 503 | */ |
||
| 504 | View Code Duplication | public function getCategoryByPath($path, $storeViewCode = StoreViewCodes::ADMIN) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Retrieve categories by given store view code. |
||
| 525 | * |
||
| 526 | * @param string $storeViewCode The store view code to retrieve the categories for |
||
| 527 | * |
||
| 528 | * @return array The array with the categories for the passed store view code |
||
| 529 | */ |
||
| 530 | public function getCategoriesByStoreViewCode($storeViewCode) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Return's the category with the passed ID. |
||
| 537 | * |
||
| 538 | * @param integer $categoryId The ID of the category to return |
||
| 539 | * @param string $storeViewCode The code of a store view, defaults to "admin" |
||
| 540 | * |
||
| 541 | * @return array The category data |
||
| 542 | * @throws \Exception Is thrown, if the category is not available |
||
| 543 | */ |
||
| 544 | View Code Duplication | public function getCategory($categoryId, $storeViewCode = StoreViewCodes::ADMIN) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Return's the root category for the actual view store. |
||
| 567 | * |
||
| 568 | * @return array The store's root category |
||
| 569 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 570 | */ |
||
| 571 | public function getRootCategory() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Returns an array with the codes of the store views related with the passed website code. |
||
| 595 | * |
||
| 596 | * @param string $websiteCode The code of the website to return the store view codes for |
||
| 597 | * |
||
| 598 | * @return array The array with the matching store view codes |
||
| 599 | */ |
||
| 600 | public function getStoreViewCodesByWebsiteCode($websiteCode) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Merge the columns from the configuration with all image type columns to define which |
||
| 632 | * columns should be cleaned-up. |
||
| 633 | * |
||
| 634 | * @return array The columns that has to be cleaned-up |
||
| 635 | */ |
||
| 636 | public function getCleanUpColumns() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Marks the relation combination processed. |
||
| 661 | * |
||
| 662 | * @param string $key The key of the relation |
||
| 663 | * @param string $value One of the relation values |
||
| 664 | * @param string $type The relation type to add |
||
| 665 | * |
||
| 666 | * @return void |
||
| 667 | */ |
||
| 668 | public function addProcessedRelation($key, $value, $type = RelationTypes::PRODUCT_RELATION) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Query's whether or not the relation with the passed key |
||
| 681 | * value combination and the given type has been processed. |
||
| 682 | * |
||
| 683 | * @param string $key The key of the relation |
||
| 684 | * @param string $value One of the relation values |
||
| 685 | * @param string $type The relation type to add |
||
| 686 | * |
||
| 687 | * @return boolean TRUE if the combination has been processed, else FALSE |
||
| 688 | */ |
||
| 689 | public function hasBeenProcessedRelation($key, $value, $type = RelationTypes::PRODUCT_RELATION) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Return's the link type ID for the passed link type code. |
||
| 703 | * |
||
| 704 | * @param string $linkTypeCode The link type code to return the link type ID for |
||
| 705 | * |
||
| 706 | * @return integer The mapped link type ID |
||
| 707 | * @throws \TechDivision\Import\Product\Exceptions\MapLinkTypeCodeToIdException Is thrown if the link type code is not mapped yet |
||
| 708 | */ |
||
| 709 | View Code Duplication | public function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Return's the link attribute for the passed link type ID and attribute code. |
||
| 727 | * |
||
| 728 | * @param integer $linkTypeId The link type |
||
| 729 | * @param string $attributeCode The attribute code |
||
| 730 | * |
||
| 731 | * @return array The link attribute |
||
| 732 | */ |
||
| 733 | View Code Duplication | public function getProductLinkAttribute($linkTypeId, $attributeCode) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Return's the link attribute for the passed link type and attribute code. |
||
| 749 | * |
||
| 750 | * @param string $linkTypeCode The link type code |
||
| 751 | * @param string $attributeCode The attribute code |
||
| 752 | * |
||
| 753 | * @return array The link attribute |
||
| 754 | */ |
||
| 755 | View Code Duplication | public function getProductLinkAttributeByLinkTypeCodeAndAttributeCode($linkTypeCode, $attributeCode) |
|
| 771 | |||
| 772 | /** |
||
| 773 | * Returns the product link attributes for the passed link type code. |
||
| 774 | * |
||
| 775 | * @param string $linkTypeCode The link type code |
||
| 776 | * |
||
| 777 | * @return array The product link types |
||
| 778 | */ |
||
| 779 | public function getProductLinkAttributes($linkTypeCode) |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Maps the link type code to the apropriate column name. |
||
| 802 | * |
||
| 803 | * @param string $linkTypeCode The link type code to map |
||
| 804 | * |
||
| 805 | * @return string The mapped column name |
||
| 806 | */ |
||
| 807 | public function mapLinkTypeCodeToColumnName($linkTypeCode) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Return the entity ID for the passed SKU. |
||
| 821 | * |
||
| 822 | * @param string $sku The SKU to return the entity ID for |
||
| 823 | * |
||
| 824 | * @return integer The mapped entity ID |
||
| 825 | * @throws \TechDivision\Import\Product\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
||
| 826 | */ |
||
| 827 | public function mapSkuToEntityId($sku) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Return's the link type code => colums mapping. |
||
| 845 | * |
||
| 846 | * @return array The mapping with the link type codes => colums |
||
| 847 | */ |
||
| 848 | 18 | public function prepareLinkTypeMappings() |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Returns the callback method used to extract the value of the passed |
||
| 892 | * column to create the link type attribute value with. |
||
| 893 | * |
||
| 894 | * @param string $columnName The column name to create the callback for |
||
| 895 | * |
||
| 896 | * @return callable The callback |
||
| 897 | */ |
||
| 898 | public function getLinkTypeColumnCallback($columnName) |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Extracts the keys of the passed value by exploding them |
||
| 915 | * with the also passed delimiter/value delmiter. |
||
| 916 | * |
||
| 917 | * @param string $value The value to extract |
||
| 918 | * @param string $delimiter The delimiter used to extract the elements |
||
| 919 | * @param string $valueDelimiter The delimiter used to extract the key from the value |
||
| 920 | * |
||
| 921 | * @return array The exploded keys |
||
| 922 | */ |
||
| 923 | View Code Duplication | public function explodeKey($value, $delimiter = ',', $valueDelimiter = '=') |
|
| 937 | |||
| 938 | /** |
||
| 939 | * Extracts the values of the passed value by exploding them |
||
| 940 | * with the also passed delimiter/value delimiter. |
||
| 941 | * |
||
| 942 | * @param string $value The value to extract |
||
| 943 | * @param string $delimiter The delimiter used to extract the elements |
||
| 944 | * @param string $valueDelimiter The delimiter used to extract the key from the value |
||
| 945 | * |
||
| 946 | * @return array The exploded values |
||
| 947 | */ |
||
| 948 | View Code Duplication | public function explodeValue($value, $delimiter = ',', $valueDelimiter = '=') |
|
| 962 | } |
||
| 963 |