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 | |||
| 138 | /** |
||
| 139 | * The default mappings for the user defined attributes, based on the attributes frontend input type. |
||
| 140 | * |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | protected $defaultFrontendInputCallbackMappings = array( |
||
| 144 | FrontendInputTypes::SELECT => array('import_product.callback.select'), |
||
| 145 | FrontendInputTypes::MULTISELECT => array('import_product.callback.multiselect'), |
||
| 146 | FrontendInputTypes::BOOLEAN => array('import_product.callback.boolean') |
||
| 147 | ); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Array that contains the relations that has already been processed. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $processedRelations = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The array that contains the prepared link type mappings. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | protected $linkTypeMappings = array(); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The array that contains the link type => column name prefix mapping. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected $linkTypeCodeToColumnNameMapping = array('super' => 'associated'); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The array that contains the link type attribute column => callback mapping. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | protected $linkTypeAttributeColumnToCallbackMapping = array( |
||
| 176 | 'associated_skus' => array('associated_skus', 'explodeKey'), |
||
| 177 | 'associated_qty' => array('associated_skus', 'explodeValue') |
||
| 178 | ); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return's the default callback frontend input mappings for the user defined attributes. |
||
| 182 | * |
||
| 183 | * @return array The default frontend input callback mappings |
||
| 184 | */ |
||
| 185 | public function getDefaultFrontendInputCallbackMappings() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Return's the available link types. |
||
| 192 | * |
||
| 193 | * @return array The link types |
||
| 194 | */ |
||
| 195 | public function getLinkTypes() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set's the SKU of the last imported product. |
||
| 202 | * |
||
| 203 | * @param string $lastSku The SKU |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function setLastSku($lastSku) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return's the SKU of the last imported product. |
||
| 214 | * |
||
| 215 | * @return string|null The SKU |
||
| 216 | */ |
||
| 217 | public function getLastSku() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set's the ID of the product that has been created recently. |
||
| 224 | * |
||
| 225 | * @param string $lastEntityId The entity ID |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function setLastEntityId($lastEntityId) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return's the ID of the product that has been created recently. |
||
| 236 | * |
||
| 237 | * @return string The entity Id |
||
| 238 | */ |
||
| 239 | public function getLastEntityId() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Queries whether or not the SKU has already been processed. |
||
| 246 | * |
||
| 247 | * @param string $sku The SKU to check been processed |
||
| 248 | * |
||
| 249 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
| 250 | */ |
||
| 251 | public function hasBeenProcessed($sku) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Queries whether or not the passed PK and store view code has already been processed. |
||
| 258 | * |
||
| 259 | * @param string $pk The PK to check been processed |
||
| 260 | * @param string $storeViewCode The store view code to check been processed |
||
| 261 | * |
||
| 262 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
| 263 | */ |
||
| 264 | public function storeViewHasBeenProcessed($pk, $storeViewCode) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Add the passed SKU => entity ID mapping. |
||
| 271 | * |
||
| 272 | * @param string $sku The SKU |
||
| 273 | * @param integer|null $entityId The optional entity ID, the last processed entity ID is used, if not set |
||
| 274 | * |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | public function addSkuEntityIdMapping($sku, $entityId = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Add the passed SKU => store view code mapping. |
||
| 284 | * |
||
| 285 | * @param string $sku The SKU |
||
| 286 | * @param string $storeViewCode The store view code |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 297 | * |
||
| 298 | * @param string $serial The serial of the actual import |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | public function setUp($serial) |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Clean up the global data after importing the bunch. |
||
| 326 | * |
||
| 327 | * @param string $serial The serial of the actual import |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | public function tearDown($serial) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return's the available image types. |
||
| 352 | * |
||
| 353 | * @return array The array with the available image types |
||
| 354 | */ |
||
| 355 | public function getImageTypes() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return's the link type code => colums mapping. |
||
| 362 | * |
||
| 363 | * @return array The mapping with the link type codes => colums |
||
| 364 | */ |
||
| 365 | public function getLinkTypeMappings() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Return's the store ID of the actual row, or of the default store |
||
| 372 | * if no store view code is set in the CSV file. |
||
| 373 | * |
||
| 374 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 375 | * |
||
| 376 | * @return integer The ID of the actual store |
||
| 377 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 378 | */ |
||
| 379 | public function getRowStoreId($default = null) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Return's the store for the passed store code. |
||
| 406 | * |
||
| 407 | * @param string $storeCode The store code to return the store for |
||
| 408 | * |
||
| 409 | * @return array The requested store |
||
| 410 | * @throws \Exception Is thrown, if the requested store is not available |
||
| 411 | */ |
||
| 412 | public function getStoreByStoreCode($storeCode) |
||
| 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 | * @param string $storeViewCode The code of a store view, defaults to admin |
||
| 481 | * |
||
| 482 | * @return array The category |
||
| 483 | * @throws \Exception Is thrown, if the requested category is not available |
||
| 484 | */ |
||
| 485 | View Code Duplication | public function getCategoryByPath($path, $storeViewCode = StoreViewCodes::ADMIN) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Query's whether or not the category with the passed path is available or not. |
||
| 506 | * |
||
| 507 | * @param string $path The path of the category to query |
||
| 508 | * |
||
| 509 | * @return boolean TRUE if the category is available, else FALSE |
||
| 510 | */ |
||
| 511 | public function hasCategoryByPath($path) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Retrieve categories by given store view code. |
||
| 518 | * |
||
| 519 | * @param string $storeViewCode The store view code to retrieve the categories for |
||
| 520 | * |
||
| 521 | * @return array The array with the categories for the passed store view code |
||
| 522 | */ |
||
| 523 | public function getCategoriesByStoreViewCode($storeViewCode) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Return's the category with the passed ID. |
||
| 530 | * |
||
| 531 | * @param integer $categoryId The ID of the category to return |
||
| 532 | * @param string $storeViewCode The code of a store view, defaults to "admin" |
||
| 533 | * |
||
| 534 | * @return array The category data |
||
| 535 | * @throws \Exception Is thrown, if the category is not available |
||
| 536 | */ |
||
| 537 | View Code Duplication | public function getCategory($categoryId, $storeViewCode = StoreViewCodes::ADMIN) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Return's the root category for the actual view store. |
||
| 560 | * |
||
| 561 | * @return array The store's root category |
||
| 562 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 563 | */ |
||
| 564 | public function getRootCategory() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Returns an array with the codes of the store views related with the passed website code. |
||
| 588 | * |
||
| 589 | * @param string $websiteCode The code of the website to return the store view codes for |
||
| 590 | * |
||
| 591 | * @return array The array with the matching store view codes |
||
| 592 | */ |
||
| 593 | public function getStoreViewCodesByWebsiteCode($websiteCode) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Merge the columns from the configuration with all image type columns to define which |
||
| 625 | * columns should be cleaned-up. |
||
| 626 | * |
||
| 627 | * @return array The columns that has to be cleaned-up |
||
| 628 | */ |
||
| 629 | public function getCleanUpColumns() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Marks the relation combination processed. |
||
| 670 | * |
||
| 671 | * @param string $key The key of the relation |
||
| 672 | * @param string $value One of the relation values |
||
| 673 | * @param string $type The relation type to add |
||
| 674 | * |
||
| 675 | * @return void |
||
| 676 | */ |
||
| 677 | public function addProcessedRelation($key, $value, $type = RelationTypes::PRODUCT_RELATION) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Query's whether or not the relation with the passed key |
||
| 690 | * value combination and the given type has been processed. |
||
| 691 | * |
||
| 692 | * @param string $key The key of the relation |
||
| 693 | * @param string $value One of the relation values |
||
| 694 | * @param string $type The relation type to add |
||
| 695 | * |
||
| 696 | * @return boolean TRUE if the combination has been processed, else FALSE |
||
| 697 | */ |
||
| 698 | public function hasBeenProcessedRelation($key, $value, $type = RelationTypes::PRODUCT_RELATION) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Return's the link type ID for the passed link type code. |
||
| 712 | * |
||
| 713 | * @param string $linkTypeCode The link type code to return the link type ID for |
||
| 714 | * |
||
| 715 | * @return integer The mapped link type ID |
||
| 716 | * @throws \TechDivision\Import\Product\Exceptions\MapLinkTypeCodeToIdException Is thrown if the link type code is not mapped yet |
||
| 717 | */ |
||
| 718 | View Code Duplication | public function mapLinkTypeCodeToLinkTypeId($linkTypeCode) |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Return's the link attribute for the passed link type ID and attribute code. |
||
| 736 | * |
||
| 737 | * @param integer $linkTypeId The link type |
||
| 738 | * @param string $attributeCode The attribute code |
||
| 739 | * |
||
| 740 | * @return array The link attribute |
||
| 741 | */ |
||
| 742 | View Code Duplication | public function getProductLinkAttribute($linkTypeId, $attributeCode) |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Return's the link attribute for the passed link type and attribute code. |
||
| 758 | * |
||
| 759 | * @param string $linkTypeCode The link type code |
||
| 760 | * @param string $attributeCode The attribute code |
||
| 761 | * |
||
| 762 | * @return array The link attribute |
||
| 763 | */ |
||
| 764 | View Code Duplication | public function getProductLinkAttributeByLinkTypeCodeAndAttributeCode($linkTypeCode, $attributeCode) |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Returns the product link attributes for the passed link type code. |
||
| 783 | * |
||
| 784 | * @param string $linkTypeCode The link type code |
||
| 785 | * |
||
| 786 | * @return array The product link types |
||
| 787 | */ |
||
| 788 | public function getProductLinkAttributes($linkTypeCode) |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Maps the link type code to the apropriate column name. |
||
| 811 | * |
||
| 812 | * @param string $linkTypeCode The link type code to map |
||
| 813 | * |
||
| 814 | * @return string The mapped column name |
||
| 815 | */ |
||
| 816 | public function mapLinkTypeCodeToColumnName($linkTypeCode) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Return the entity ID for the passed SKU. |
||
| 830 | * |
||
| 831 | * @param string $sku The SKU to return the entity ID for |
||
| 832 | * |
||
| 833 | * @return integer The mapped entity ID |
||
| 834 | * @throws \TechDivision\Import\Product\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
||
| 835 | */ |
||
| 836 | public function mapSkuToEntityId($sku) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Return's the link type code => colums mapping. |
||
| 854 | * |
||
| 855 | * @return array The mapping with the link type codes => colums |
||
| 856 | */ |
||
| 857 | public function prepareLinkTypeMappings() |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Returns the callback method used to extract the value of the passed |
||
| 901 | * column to create the link type attribute value with. |
||
| 902 | * |
||
| 903 | * @param string $columnName The column name to create the callback for |
||
| 904 | * |
||
| 905 | * @return callable The callback |
||
| 906 | */ |
||
| 907 | public function getLinkTypeColumnCallback($columnName) |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Extracts the keys of the passed value by exploding them |
||
| 924 | * with the also passed delimiter/value delmiter. |
||
| 925 | * |
||
| 926 | * @param string $value The value to extract |
||
| 927 | * @param string $delimiter The delimiter used to extract the elements |
||
| 928 | * @param string $valueDelimiter The delimiter used to extract the key from the value |
||
| 929 | * |
||
| 930 | * @return array The exploded keys |
||
| 931 | */ |
||
| 932 | public function explodeKey($value, $delimiter = ',', $valueDelimiter = '=') |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Extracts the values of the passed value by exploding them |
||
| 954 | * with the also passed delimiter/value delimiter. |
||
| 955 | * |
||
| 956 | * @param string $value The value to extract |
||
| 957 | * @param string $delimiter The delimiter used to extract the elements |
||
| 958 | * @param string $valueDelimiter The delimiter used to extract the key from the value |
||
| 959 | * |
||
| 960 | * @return array The exploded values |
||
| 961 | */ |
||
| 962 | public function explodeValue($value, $delimiter = ',', $valueDelimiter = '=') |
||
| 985 | } |
||
| 986 |