Complex classes like BunchSubject 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 BunchSubject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The trait that implements the export functionality. |
||
| 43 | * |
||
| 44 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
| 45 | */ |
||
| 46 | use ExportableTrait; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The array with the pre-loaded entity IDs. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $preLoadedEntityIds = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Mappings for the table column => CSV column header. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $headerStockMappings = array( |
||
| 61 | 'qty' => array('qty', 'float'), |
||
| 62 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
| 63 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
| 64 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
| 65 | 'backorders' => array('allow_backorders', 'int'), |
||
| 66 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
| 67 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
| 68 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
| 69 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
| 70 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
| 71 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
| 72 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
| 73 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
| 74 | 'manage_stock' => array('manage_stock', 'int'), |
||
| 75 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
| 76 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
| 77 | 'qty_increments' => array('qty_increments', 'float'), |
||
| 78 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
| 79 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
| 80 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
| 81 | ); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The array with the available visibility keys. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $availableVisibilities = array( |
||
| 89 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
| 90 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
| 91 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
| 92 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The category IDs the product is related with. |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $productCategoryIds = array(); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The default callback mappings for the Magento standard product attributes. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $defaultCallbackMappings = array( |
||
| 108 | 'visibility' => array('import_product.callback.visibility'), |
||
| 109 | 'tax_class_id' => array('import_product.callback.tax.class'), |
||
| 110 | 'bundle_price_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 111 | 'bundle_sku_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 112 | 'bundle_weight_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 113 | 'bundle_price_view' => array('import_product_bundle.callback.bundle.price.view'), |
||
| 114 | 'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type') |
||
| 115 | ); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Clean up the global data after importing the bunch. |
||
| 119 | * |
||
| 120 | * @param string $serial The serial of the actual import |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function tearDown($serial) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return's the default callback mappings. |
||
| 144 | * |
||
| 145 | * @return array The default callback mappings |
||
| 146 | */ |
||
| 147 | public function getDefaultCallbackMappings() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Return's the mappings for the table column => CSV column header. |
||
| 154 | * |
||
| 155 | * @return array The header stock mappings |
||
| 156 | */ |
||
| 157 | 1 | public function getHeaderStockMappings() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Return's the visibility key for the passed visibility string. |
||
| 164 | * |
||
| 165 | * @param string $visibility The visibility string to return the key for |
||
| 166 | * |
||
| 167 | * @return integer The requested visibility key |
||
| 168 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
| 169 | */ |
||
| 170 | public function getVisibilityIdByValue($visibility) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Add the passed category ID to the product's category list. |
||
| 188 | * |
||
| 189 | * @param integer $categoryId The category ID to add |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function addProductCategoryId($categoryId) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return's the list with category IDs the product is related with. |
||
| 200 | * |
||
| 201 | * @return array The product's category IDs |
||
| 202 | */ |
||
| 203 | public function getProductCategoryIds() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Pre-load the entity ID for the product with the passed SKU |
||
| 220 | * and persist it temporary in the registry. |
||
| 221 | * |
||
| 222 | * @param string $sku The SKU of the product to be pre-loaded |
||
| 223 | * |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function preLoadEntityId($sku) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
| 238 | * |
||
| 239 | * @param integer $isUserDefined The flag itself |
||
| 240 | * |
||
| 241 | * @return array The array with the EAV attributes matching the passed flag |
||
| 242 | */ |
||
| 243 | public function getEavAttributeByIsUserDefined($isUserDefined = 1) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
| 250 | * |
||
| 251 | * @param string $entityType The entity type to load the URL rewrites for |
||
| 252 | * @param integer $entityId The entity ID to laod the rewrites for |
||
| 253 | * |
||
| 254 | * @return array The URL rewrites |
||
| 255 | */ |
||
| 256 | 1 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Load's and return's the product with the passed SKU. |
||
| 263 | * |
||
| 264 | * @param string $sku The SKU of the product to load |
||
| 265 | * |
||
| 266 | * @return array The product |
||
| 267 | */ |
||
| 268 | public function loadProduct($sku) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Load's and return's the product website relation with the passed product and website ID. |
||
| 275 | * |
||
| 276 | * @param string $productId The product ID of the relation |
||
| 277 | * @param string $websiteId The website ID of the relation |
||
| 278 | * |
||
| 279 | * @return array The product website |
||
| 280 | */ |
||
| 281 | public function loadProductWebsite($productId, $websiteId) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Return's the category product relation with the passed category/product ID. |
||
| 288 | * |
||
| 289 | * @param integer $categoryId The category ID of the category product relation to return |
||
| 290 | * @param integer $productId The product ID of the category product relation to return |
||
| 291 | * |
||
| 292 | * @return array The category product relation |
||
| 293 | */ |
||
| 294 | public function loadCategoryProduct($categoryId, $productId) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
| 301 | * |
||
| 302 | * @param integer $productId The product ID of the stock status to load |
||
| 303 | * @param integer $websiteId The website ID of the stock status to load |
||
| 304 | * @param integer $stockId The stock ID of the stock status to load |
||
| 305 | * |
||
| 306 | * @return array The stock status |
||
| 307 | */ |
||
| 308 | public function loadStockStatus($productId, $websiteId, $stockId) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
| 315 | * |
||
| 316 | * @param integer $productId The product ID of the stock item to load |
||
| 317 | * @param integer $websiteId The website ID of the stock item to load |
||
| 318 | * @param integer $stockId The stock ID of the stock item to load |
||
| 319 | * |
||
| 320 | * @return array The stock item |
||
| 321 | */ |
||
| 322 | public function loadStockItem($productId, $websiteId, $stockId) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Load's and return's the datetime attribute with the passed entity/attribute/store ID. |
||
| 329 | * |
||
| 330 | * @param integer $entityId The entity ID of the attribute |
||
| 331 | * @param integer $attributeId The attribute ID of the attribute |
||
| 332 | * @param integer $storeId The store ID of the attribute |
||
| 333 | * |
||
| 334 | * @return array|null The datetime attribute |
||
| 335 | */ |
||
| 336 | public function loadProductDatetimeAttribute($entityId, $attributeId, $storeId) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Load's and return's the decimal attribute with the passed entity/attribute/store ID. |
||
| 343 | * |
||
| 344 | * @param integer $entityId The entity ID of the attribute |
||
| 345 | * @param integer $attributeId The attribute ID of the attribute |
||
| 346 | * @param integer $storeId The store ID of the attribute |
||
| 347 | * |
||
| 348 | * @return array|null The decimal attribute |
||
| 349 | */ |
||
| 350 | public function loadProductDecimalAttribute($entityId, $attributeId, $storeId) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Load's and return's the integer attribute with the passed entity/attribute/store ID. |
||
| 357 | * |
||
| 358 | * @param integer $entityId The entity ID of the attribute |
||
| 359 | * @param integer $attributeId The attribute ID of the attribute |
||
| 360 | * @param integer $storeId The store ID of the attribute |
||
| 361 | * |
||
| 362 | * @return array|null The integer attribute |
||
| 363 | */ |
||
| 364 | public function loadProductIntAttribute($entityId, $attributeId, $storeId) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Load's and return's the text attribute with the passed entity/attribute/store ID. |
||
| 371 | * |
||
| 372 | * @param integer $entityId The entity ID of the attribute |
||
| 373 | * @param integer $attributeId The attribute ID of the attribute |
||
| 374 | * @param integer $storeId The store ID of the attribute |
||
| 375 | * |
||
| 376 | * @return array|null The text attribute |
||
| 377 | */ |
||
| 378 | public function loadProductTextAttribute($entityId, $attributeId, $storeId) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Load's and return's the varchar attribute with the passed entity/attribute/store ID. |
||
| 385 | * |
||
| 386 | * @param integer $entityId The entity ID of the attribute |
||
| 387 | * @param integer $attributeId The attribute ID of the attribute |
||
| 388 | * @param integer $storeId The store ID of the attribute |
||
| 389 | * |
||
| 390 | * @return array|null The varchar attribute |
||
| 391 | */ |
||
| 392 | public function loadProductVarcharAttribute($entityId, $attributeId, $storeId) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Return's the URL rewrite product category relation for the passed |
||
| 399 | * product and category ID. |
||
| 400 | * |
||
| 401 | * @param integer $productId The product ID to load the URL rewrite product category relation for |
||
| 402 | * @param integer $categoryId The category ID to load the URL rewrite product category relation for |
||
| 403 | * |
||
| 404 | * @return array|false The URL rewrite product category relations |
||
| 405 | */ |
||
| 406 | public function loadUrlRewriteProductCategory($productId, $categoryId) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Persist's the passed product data and return's the ID. |
||
| 413 | * |
||
| 414 | * @param array $product The product data to persist |
||
| 415 | * |
||
| 416 | * @return string The ID of the persisted entity |
||
| 417 | */ |
||
| 418 | public function persistProduct($product) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Persist's the passed product varchar attribute. |
||
| 425 | * |
||
| 426 | * @param array $attribute The attribute to persist |
||
| 427 | * |
||
| 428 | * @return void |
||
| 429 | */ |
||
| 430 | public function persistProductVarcharAttribute($attribute) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Persist's the passed product integer attribute. |
||
| 437 | * |
||
| 438 | * @param array $attribute The attribute to persist |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | public function persistProductIntAttribute($attribute) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Persist's the passed product decimal attribute. |
||
| 449 | * |
||
| 450 | * @param array $attribute The attribute to persist |
||
| 451 | * |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | public function persistProductDecimalAttribute($attribute) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Persist's the passed product datetime attribute. |
||
| 461 | * |
||
| 462 | * @param array $attribute The attribute to persist |
||
| 463 | * |
||
| 464 | * @return void |
||
| 465 | */ |
||
| 466 | public function persistProductDatetimeAttribute($attribute) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Persist's the passed product text attribute. |
||
| 473 | * |
||
| 474 | * @param array $attribute The attribute to persist |
||
| 475 | * |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | public function persistProductTextAttribute($attribute) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Persist's the passed product website data and return's the ID. |
||
| 485 | * |
||
| 486 | * @param array $productWebsite The product website data to persist |
||
| 487 | * |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | public function persistProductWebsite($productWebsite) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Persist's the passed category product relation. |
||
| 497 | * |
||
| 498 | * @param array $categoryProduct The category product relation to persist |
||
| 499 | * |
||
| 500 | * @return void |
||
| 501 | */ |
||
| 502 | public function persistCategoryProduct($categoryProduct) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Persist's the passed stock item data and return's the ID. |
||
| 509 | * |
||
| 510 | * @param array $stockItem The stock item data to persist |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | */ |
||
| 514 | public function persistStockItem($stockItem) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Persist's the passed stock status data and return's the ID. |
||
| 521 | * |
||
| 522 | * @param array $stockStatus The stock status data to persist |
||
| 523 | * |
||
| 524 | * @return void |
||
| 525 | */ |
||
| 526 | public function persistStockStatus($stockStatus) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Persist's the URL rewrite with the passed data. |
||
| 533 | * |
||
| 534 | * @param array $row The URL rewrite to persist |
||
| 535 | * |
||
| 536 | * @return string The ID of the persisted entity |
||
| 537 | */ |
||
| 538 | 1 | public function persistUrlRewrite($row) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Persist's the URL rewrite product => category relation with the passed data. |
||
| 545 | * |
||
| 546 | * @param array $row The URL rewrite product => category relation to persist |
||
| 547 | * |
||
| 548 | * @return void |
||
| 549 | */ |
||
| 550 | public function persistUrlRewriteProductCategory($row) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Delete's the entity with the passed attributes. |
||
| 557 | * |
||
| 558 | * @param array $row The attributes of the entity to delete |
||
| 559 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 560 | * |
||
| 561 | * @return void |
||
| 562 | */ |
||
| 563 | public function deleteProduct($row, $name = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Delete's the URL rewrite(s) with the passed attributes. |
||
| 570 | * |
||
| 571 | * @param array $row The attributes of the entity to delete |
||
| 572 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 573 | * |
||
| 574 | * @return void |
||
| 575 | */ |
||
| 576 | 1 | public function deleteUrlRewrite($row, $name = null) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Delete's the stock item(s) with the passed attributes. |
||
| 583 | * |
||
| 584 | * @param array $row The attributes of the entity to delete |
||
| 585 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 586 | * |
||
| 587 | * @return void |
||
| 588 | */ |
||
| 589 | public function deleteStockItem($row, $name = null) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Delete's the stock status with the passed attributes. |
||
| 596 | * |
||
| 597 | * @param array $row The attributes of the entity to delete |
||
| 598 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 599 | * |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | public function deleteStockStatus($row, $name = null) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Delete's the product website relations with the passed attributes. |
||
| 609 | * |
||
| 610 | * @param array $row The attributes of the entity to delete |
||
| 611 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 612 | * |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | public function deleteProductWebsite($row, $name = null) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Delete's the category product relations with the passed attributes. |
||
| 622 | * |
||
| 623 | * @param array $row The attributes of the entity to delete |
||
| 624 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 625 | * |
||
| 626 | * @return void |
||
| 627 | */ |
||
| 628 | public function deleteCategoryProduct($row, $name = null) |
||
| 632 | } |
||
| 633 |