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() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Initialize the category product with the passed attributes and returns an instance. |
||
| 220 | * |
||
| 221 | * @param array $attr The category product attributes |
||
| 222 | * |
||
| 223 | * @return array The initialized category product |
||
| 224 | */ |
||
| 225 | 2 | protected function initializeUrlRewrite(array $attr) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Initialize the URL rewrite product => category relation with the passed attributes |
||
| 232 | * and returns an instance. |
||
| 233 | * |
||
| 234 | * @param array $attr The URL rewrite product => category relation attributes |
||
| 235 | * |
||
| 236 | * @return array The initialized URL rewrite product => category relation |
||
| 237 | */ |
||
| 238 | 2 | protected function initializeUrlRewriteProductCategory($attr) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Prepare's the URL rewrites that has to be created/updated. |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | 3 | protected function prepareUrlRewrites() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Resolve's the parent categories of the category with the passed ID and relate's |
||
| 282 | * it with the product with the passed ID, if the category is top level OR has the |
||
| 283 | * anchor flag set. |
||
| 284 | * |
||
| 285 | * @param integer $categoryId The ID of the category to resolve the parents |
||
| 286 | * @param integer $entityId The ID of the product entity to relate the category with |
||
| 287 | * @param boolean $topLevel TRUE if the passed category has top level, else FALSE |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | 3 | protected function resolveCategoryIds($categoryId, $entityId, $topLevel = false) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Prepare the attributes of the entity that has to be persisted. |
||
| 328 | * |
||
| 329 | * @return array The prepared attributes |
||
| 330 | */ |
||
| 331 | 3 | protected function prepareAttributes() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Prepare's the URL rewrite product => category relation attributes. |
||
| 363 | * |
||
| 364 | * @return array The prepared attributes |
||
| 365 | */ |
||
| 366 | 3 | protected function prepareUrlRewriteProductCategoryAttributes() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Prepare's the target path for a URL rewrite. |
||
| 381 | * |
||
| 382 | * @param array $category The categroy with the URL path |
||
| 383 | * |
||
| 384 | * @return string The target path |
||
| 385 | */ |
||
| 386 | 3 | protected function prepareTargetPath(array $category) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Prepare's the request path for a URL rewrite or the target path for a 301 redirect. |
||
| 405 | * |
||
| 406 | * @param array $category The categroy with the URL path |
||
| 407 | * |
||
| 408 | * @return string The request path |
||
| 409 | * @throws \RuntimeException Is thrown, if the passed category has no or an empty value for attribute "url_path" |
||
| 410 | */ |
||
| 411 | 3 | protected function prepareRequestPath(array $category) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
| 439 | * |
||
| 440 | * @param array $category The category used for preparation |
||
| 441 | * |
||
| 442 | * @return array The metadata |
||
| 443 | */ |
||
| 444 | 3 | protected function prepareMetadata(array $category) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Query whether or not the actual entity is visible or not. |
||
| 464 | * |
||
| 465 | * @return boolean TRUE if the entity is NOT visible, else FALSE |
||
| 466 | */ |
||
| 467 | 3 | protected function isNotVisible() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created |
||
| 474 | * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will |
||
| 475 | * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>. |
||
| 476 | * |
||
| 477 | * @return integer The visibility ID |
||
| 478 | * @throws \Exception Is thrown, if the entity ID has not been mapped |
||
| 479 | * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue() |
||
| 480 | */ |
||
| 481 | 3 | protected function getEntityIdVisibilityIdMapping() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Return's the root category for the actual view store. |
||
| 488 | * |
||
| 489 | * @return array The store's root category |
||
| 490 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 491 | */ |
||
| 492 | 3 | protected function getRootCategory() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
| 499 | * |
||
| 500 | * @param array $category The category to query |
||
| 501 | * |
||
| 502 | * @return boolean TRUE if the passed category IS the root category |
||
| 503 | */ |
||
| 504 | 3 | protected function isRootCategory(array $category) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Return's the list with category IDs the product is related with. |
||
| 516 | * |
||
| 517 | * @return array The product's category IDs |
||
| 518 | */ |
||
| 519 | 3 | protected function getProductCategoryIds() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Return's the category with the passed ID. |
||
| 526 | * |
||
| 527 | * @param integer $categoryId The ID of the category to return |
||
| 528 | * |
||
| 529 | * @return array The category data |
||
| 530 | */ |
||
| 531 | 2 | protected function getCategory($categoryId) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Persist's the URL rewrite with the passed data. |
||
| 538 | * |
||
| 539 | * @param array $row The URL rewrite to persist |
||
| 540 | * |
||
| 541 | * @return string The ID of the persisted entity |
||
| 542 | */ |
||
| 543 | 3 | protected function persistUrlRewrite($row) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Persist's the URL rewrite product => category relation with the passed data. |
||
| 550 | * |
||
| 551 | * @param array $row The URL rewrite product => category relation to persist |
||
| 552 | * |
||
| 553 | * @return void |
||
| 554 | */ |
||
| 555 | 3 | protected function persistUrlRewriteProductCategory($row) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Return's the column name that contains the primary key. |
||
| 562 | * |
||
| 563 | * @return string the column name that contains the primary key |
||
| 564 | */ |
||
| 565 | 3 | protected function getPrimaryKeyColumnName() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Queries whether or not the passed SKU and store view code has already been processed. |
||
| 572 | * |
||
| 573 | * @param string $sku The SKU to check been processed |
||
| 574 | * @param string $storeViewCode The store view code to check been processed |
||
| 575 | * |
||
| 576 | * @return boolean TRUE if the SKU and store view code has been processed, else FALSE |
||
| 577 | */ |
||
| 578 | 3 | protected function storeViewHasBeenProcessed($sku, $storeViewCode) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Add the entity ID => visibility mapping for the actual entity ID. |
||
| 585 | * |
||
| 586 | * @param string $visibility The visibility of the actual entity to map |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | 3 | protected function addEntityIdVisibilityIdMapping($visibility) |
|
| 594 | } |
||
| 595 |
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: