Complex classes like UrlRewriteObserver 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 UrlRewriteObserver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class UrlRewriteObserver extends AbstractProductImportObserver |
||
| 40 | { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The entity type to load the URL rewrites for. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const ENTITY_TYPE = 'product'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The key for the category in the metadata. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | const CATEGORY_ID = 'category_id'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The URL key from the CSV file column that has to be processed by the observer. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $urlKey; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The actual category ID to process. |
||
| 65 | * |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | protected $categoryId; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The actual entity ID to process. |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | protected $entityId; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The ID of the recently created URL rewrite. |
||
| 79 | * |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | protected $urlRewriteId; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The array with the URL rewrites that has to be created. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $urlRewrites = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The array with the category IDs related with the product. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $productCategoryIds = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The product bunch processor instance. |
||
| 100 | * |
||
| 101 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
| 102 | */ |
||
| 103 | protected $productBunchProcessor; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Initialize the observer with the passed product bunch processor instance. |
||
| 107 | * |
||
| 108 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
| 109 | */ |
||
| 110 | 3 | public function __construct(ProductBunchProcessorInterface $productBunchProcessor) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Return's the product bunch processor instance. |
||
| 117 | * |
||
| 118 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
| 119 | */ |
||
| 120 | 3 | protected function getProductBunchProcessor() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Process the observer's business logic. |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | 3 | protected function process() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Initialize the category product with the passed attributes and returns an instance. |
||
| 208 | * |
||
| 209 | * @param array $attr The category product attributes |
||
| 210 | * |
||
| 211 | * @return array The initialized category product |
||
| 212 | */ |
||
| 213 | 2 | protected function initializeUrlRewrite(array $attr) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Initialize the URL rewrite product => category relation with the passed attributes |
||
| 220 | * and returns an instance. |
||
| 221 | * |
||
| 222 | * @param array $attr The URL rewrite product => category relation attributes |
||
| 223 | * |
||
| 224 | * @return array The initialized URL rewrite product => category relation |
||
| 225 | */ |
||
| 226 | 2 | protected function initializeUrlRewriteProductCategory($attr) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Prepare's the URL rewrites that has to be created/updated. |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | 3 | protected function prepareUrlRewrites() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Resolve's the parent categories of the category with the passed ID and relate's |
||
| 270 | * it with the product with the passed ID, if the category is top level OR has the |
||
| 271 | * anchor flag set. |
||
| 272 | * |
||
| 273 | * @param integer $categoryId The ID of the category to resolve the parents |
||
| 274 | * @param integer $entityId The ID of the product entity to relate the category with |
||
| 275 | * @param boolean $topLevel TRUE if the passed category has top level, else FALSE |
||
| 276 | * |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | 3 | protected function resolveCategoryIds($categoryId, $entityId, $topLevel = false) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Prepare the attributes of the entity that has to be persisted. |
||
| 316 | * |
||
| 317 | * @return array The prepared attributes |
||
| 318 | */ |
||
| 319 | 3 | protected function prepareAttributes() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Prepare's the URL rewrite product => category relation attributes. |
||
| 351 | * |
||
| 352 | * @return array The prepared attributes |
||
| 353 | */ |
||
| 354 | 3 | protected function prepareUrlRewriteProductCategoryAttributes() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Prepare's the target path for a URL rewrite. |
||
| 369 | * |
||
| 370 | * @param array $category The categroy with the URL path |
||
| 371 | * |
||
| 372 | * @return string The target path |
||
| 373 | */ |
||
| 374 | 3 | protected function prepareTargetPath(array $category) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Prepare's the request path for a URL rewrite or the target path for a 301 redirect. |
||
| 393 | * |
||
| 394 | * @param array $category The categroy with the URL path |
||
| 395 | * |
||
| 396 | * @return string The request path |
||
| 397 | * @throws \RuntimeException Is thrown, if the passed category has no or an empty value for attribute "url_path" |
||
| 398 | */ |
||
| 399 | 3 | protected function prepareRequestPath(array $category) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
| 430 | * |
||
| 431 | * @param array $category The category used for preparation |
||
| 432 | * |
||
| 433 | * @return array The metadata |
||
| 434 | */ |
||
| 435 | 3 | protected function prepareMetadata(array $category) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Make's the passed URL key unique by adding the next number to the end. |
||
| 455 | * |
||
| 456 | * @param string $urlKey The URL key to make unique |
||
| 457 | * |
||
| 458 | * @return string The unique URL key |
||
| 459 | */ |
||
| 460 | 3 | protected function makeUrlKeyUnique($urlKey) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Query whether or not the actual entity is visible or not. |
||
| 529 | * |
||
| 530 | * @return boolean TRUE if the entity is NOT visible, else FALSE |
||
| 531 | */ |
||
| 532 | 3 | protected function isNotVisible() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created |
||
| 539 | * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will |
||
| 540 | * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>. |
||
| 541 | * |
||
| 542 | * @return integer The visibility ID |
||
| 543 | * @throws \Exception Is thrown, if the entity ID has not been mapped |
||
| 544 | * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue() |
||
| 545 | */ |
||
| 546 | 3 | protected function getEntityIdVisibilityIdMapping() |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Return's TRUE, if the passed URL key varchar value IS related with the actual PK. |
||
| 553 | * |
||
| 554 | * @param array $productVarcharAttribute The varchar value to check |
||
| 555 | * |
||
| 556 | * @return boolean TRUE if the URL key is related, else FALSE |
||
| 557 | */ |
||
| 558 | protected function isUrlKeyOf(array $productVarcharAttribute) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Return's the entity type for the configured entity type code. |
||
| 565 | * |
||
| 566 | * @return array The requested entity type |
||
| 567 | * @throws \Exception Is thrown, if the requested entity type is not available |
||
| 568 | */ |
||
| 569 | 3 | protected function getEntityType() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Return's the root category for the actual view store. |
||
| 576 | * |
||
| 577 | * @return array The store's root category |
||
| 578 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 579 | */ |
||
| 580 | 3 | protected function getRootCategory() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
| 587 | * |
||
| 588 | * @param array $category The category to query |
||
| 589 | * |
||
| 590 | * @return boolean TRUE if the passed category IS the root category |
||
| 591 | */ |
||
| 592 | 3 | protected function isRootCategory(array $category) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Return's the list with category IDs the product is related with. |
||
| 604 | * |
||
| 605 | * @return array The product's category IDs |
||
| 606 | */ |
||
| 607 | 3 | protected function getProductCategoryIds() |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Return's the category with the passed ID. |
||
| 614 | * |
||
| 615 | * @param integer $categoryId The ID of the category to return |
||
| 616 | * |
||
| 617 | * @return array The category data |
||
| 618 | */ |
||
| 619 | 2 | protected function getCategory($categoryId) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Persist's the URL rewrite with the passed data. |
||
| 626 | * |
||
| 627 | * @param array $row The URL rewrite to persist |
||
| 628 | * |
||
| 629 | * @return string The ID of the persisted entity |
||
| 630 | */ |
||
| 631 | 3 | protected function persistUrlRewrite($row) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Persist's the URL rewrite product => category relation with the passed data. |
||
| 638 | * |
||
| 639 | * @param array $row The URL rewrite product => category relation to persist |
||
| 640 | * |
||
| 641 | * @return void |
||
| 642 | */ |
||
| 643 | 3 | protected function persistUrlRewriteProductCategory($row) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Return's the column name that contains the primary key. |
||
| 650 | * |
||
| 651 | * @return string the column name that contains the primary key |
||
| 652 | */ |
||
| 653 | 3 | protected function getPrimaryKeyColumnName() |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Queries whether or not the passed SKU and store view code has already been processed. |
||
| 660 | * |
||
| 661 | * @param string $sku The SKU to check been processed |
||
| 662 | * @param string $storeViewCode The store view code to check been processed |
||
| 663 | * |
||
| 664 | * @return boolean TRUE if the SKU and store view code has been processed, else FALSE |
||
| 665 | */ |
||
| 666 | 3 | protected function storeViewHasBeenProcessed($sku, $storeViewCode) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Add the entity ID => visibility mapping for the actual entity ID. |
||
| 673 | * |
||
| 674 | * @param string $visibility The visibility of the actual entity to map |
||
| 675 | * |
||
| 676 | * @return void |
||
| 677 | */ |
||
| 678 | 3 | protected function addEntityIdVisibilityIdMapping($visibility) |
|
| 682 | } |
||
| 683 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: