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() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Initialize the category product with the passed attributes and returns an instance. |
||
| 203 | * |
||
| 204 | * @param array $attr The category product attributes |
||
| 205 | * |
||
| 206 | * @return array The initialized category product |
||
| 207 | */ |
||
| 208 | 2 | protected function initializeUrlRewrite(array $attr) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Initialize the URL rewrite product => category relation with the passed attributes |
||
| 215 | * and returns an instance. |
||
| 216 | * |
||
| 217 | * @param array $attr The URL rewrite product => category relation attributes |
||
| 218 | * |
||
| 219 | * @return array The initialized URL rewrite product => category relation |
||
| 220 | */ |
||
| 221 | 2 | protected function initializeUrlRewriteProductCategory($attr) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Prepare's the URL rewrites that has to be created/updated. |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | 3 | protected function prepareUrlRewrites() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Resolve's the parent categories of the category with the passed ID and relate's |
||
| 265 | * it with the product with the passed ID, if the category is top level OR has the |
||
| 266 | * anchor flag set. |
||
| 267 | * |
||
| 268 | * @param integer $categoryId The ID of the category to resolve the parents |
||
| 269 | * @param integer $entityId The ID of the product entity to relate the category with |
||
| 270 | * @param boolean $topLevel TRUE if the passed category has top level, else FALSE |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | 3 | protected function resolveCategoryIds($categoryId, $entityId, $topLevel = false) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Prepare the attributes of the entity that has to be persisted. |
||
| 311 | * |
||
| 312 | * @return array The prepared attributes |
||
| 313 | */ |
||
| 314 | 3 | protected function prepareAttributes() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Prepare's the URL rewrite product => category relation attributes. |
||
| 346 | * |
||
| 347 | * @return array The prepared attributes |
||
| 348 | */ |
||
| 349 | 3 | protected function prepareUrlRewriteProductCategoryAttributes() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Prepare's the target path for a URL rewrite. |
||
| 364 | * |
||
| 365 | * @param array $category The categroy with the URL path |
||
| 366 | * |
||
| 367 | * @return string The target path |
||
| 368 | */ |
||
| 369 | 3 | protected function prepareTargetPath(array $category) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Prepare's the request path for a URL rewrite or the target path for a 301 redirect. |
||
| 388 | * |
||
| 389 | * @param array $category The categroy with the URL path |
||
| 390 | * |
||
| 391 | * @return string The request path |
||
| 392 | * @throws \RuntimeException Is thrown, if the passed category has no or an empty value for attribute "url_path" |
||
| 393 | */ |
||
| 394 | 3 | protected function prepareRequestPath(array $category) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
| 425 | * |
||
| 426 | * @param array $category The category used for preparation |
||
| 427 | * |
||
| 428 | * @return array The metadata |
||
| 429 | */ |
||
| 430 | 3 | protected function prepareMetadata(array $category) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Make's the passed URL key unique by adding the next number to the end. |
||
| 450 | * |
||
| 451 | * @param string $urlKey The URL key to make unique |
||
| 452 | * |
||
| 453 | * @return string The unique URL key |
||
| 454 | */ |
||
| 455 | 3 | protected function makeUrlKeyUnique($urlKey) |
|
| 521 | |||
| 522 | 3 | protected function isNotVisible() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created |
||
| 529 | * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will |
||
| 530 | * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>. |
||
| 531 | * |
||
| 532 | * @return integer The visibility ID |
||
| 533 | * @throws \Exception Is thrown, if the entity ID has not been mapped |
||
| 534 | */ |
||
| 535 | 3 | protected function getVisibilityIdMapping() |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Return's TRUE, if the passed URL key varchar value IS related with the actual PK. |
||
| 542 | * |
||
| 543 | * @param array $productVarcharAttribute The varchar value to check |
||
| 544 | * |
||
| 545 | * @return boolean TRUE if the URL key is related, else FALSE |
||
| 546 | */ |
||
| 547 | protected function isUrlKeyOf(array $productVarcharAttribute) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Return's the entity type for the configured entity type code. |
||
| 554 | * |
||
| 555 | * @return array The requested entity type |
||
| 556 | * @throws \Exception Is thrown, if the requested entity type is not available |
||
| 557 | */ |
||
| 558 | 3 | protected function getEntityType() |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Return's the root category for the actual view store. |
||
| 565 | * |
||
| 566 | * @return array The store's root category |
||
| 567 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 568 | */ |
||
| 569 | 3 | protected function getRootCategory() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
| 576 | * |
||
| 577 | * @param array $category The category to query |
||
| 578 | * |
||
| 579 | * @return boolean TRUE if the passed category IS the root category |
||
| 580 | */ |
||
| 581 | 3 | protected function isRootCategory(array $category) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Return's the list with category IDs the product is related with. |
||
| 593 | * |
||
| 594 | * @return array The product's category IDs |
||
| 595 | */ |
||
| 596 | 3 | protected function getProductCategoryIds() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Return's the category with the passed ID. |
||
| 603 | * |
||
| 604 | * @param integer $categoryId The ID of the category to return |
||
| 605 | * |
||
| 606 | * @return array The category data |
||
| 607 | */ |
||
| 608 | 2 | protected function getCategory($categoryId) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Persist's the URL rewrite with the passed data. |
||
| 615 | * |
||
| 616 | * @param array $row The URL rewrite to persist |
||
| 617 | * |
||
| 618 | * @return string The ID of the persisted entity |
||
| 619 | */ |
||
| 620 | 3 | protected function persistUrlRewrite($row) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Persist's the URL rewrite product => category relation with the passed data. |
||
| 627 | * |
||
| 628 | * @param array $row The URL rewrite product => category relation to persist |
||
| 629 | * |
||
| 630 | * @return void |
||
| 631 | */ |
||
| 632 | 3 | protected function persistUrlRewriteProductCategory($row) |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Return's the column name that contains the primary key. |
||
| 639 | * |
||
| 640 | * @return string the column name that contains the primary key |
||
| 641 | */ |
||
| 642 | 3 | protected function getPrimaryKeyColumnName() |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Queries whether or not the passed SKU and store view code has already been processed. |
||
| 649 | * |
||
| 650 | * @param string $sku The SKU to check been processed |
||
| 651 | * @param string $storeViewCode The store view code to check been processed |
||
| 652 | * |
||
| 653 | * @return boolean TRUE if the SKU and store view code has been processed, else FALSE |
||
| 654 | */ |
||
| 655 | 3 | protected function storeViewHasBeenProcessed($sku, $storeViewCode) |
|
| 659 | } |
||
| 660 |
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: