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 |
||
| 38 | class UrlRewriteObserver extends AbstractProductImportObserver |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The entity type to load the URL rewrites for. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const ENTITY_TYPE = 'product'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The URL key from the CSV file column that has to be processed by the observer. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $urlKey; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Will be invoked by the action on the events the listener has been registered for. |
||
| 57 | * |
||
| 58 | * @param array $row The row to handle |
||
| 59 | * |
||
| 60 | * @return array The modified row |
||
| 61 | * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
||
| 62 | */ |
||
| 63 | 3 | public function handle(array $row) |
|
| 64 | { |
||
| 65 | |||
| 66 | // load the header information |
||
| 67 | 3 | $headers = $this->getHeaders(); |
|
| 68 | |||
| 69 | // query whether or not, we've found a new SKU => means we've found a new product |
||
| 70 | 3 | if ($this->isLastSku($row[$headers[ColumnKeys::SKU]])) { |
|
| 71 | return $row; |
||
| 72 | } |
||
| 73 | |||
| 74 | // prepare the URL key, return immediately if not available |
||
| 75 | 3 | if ($this->prepareUrlKey($row) == null) { |
|
| 76 | return $row; |
||
| 77 | } |
||
| 78 | |||
| 79 | // initialize the store view code |
||
| 80 | 3 | $this->setStoreViewCode($row[$headers[ColumnKeys::STORE_VIEW_CODE]] ?: StoreViewCodes::DEF); |
|
| 81 | |||
| 82 | // load the ID of the last entity |
||
| 83 | 3 | $lastEntityId = $this->getLastEntityId(); |
|
| 84 | |||
| 85 | // initialize the entity type to use |
||
| 86 | 3 | $entityType = UrlRewriteObserver::ENTITY_TYPE; |
|
| 87 | |||
| 88 | // load the product category IDs |
||
| 89 | 3 | $productCategoryIds = $this->getProductCategoryIds(); |
|
| 90 | |||
| 91 | // load the URL rewrites for the entity type and ID |
||
| 92 | 3 | $urlRewrites = $this->getUrlRewritesByEntityTypeAndEntityId($entityType, $lastEntityId); |
|
| 93 | |||
| 94 | // prepare the existing URLs => unserialize the metadata |
||
| 95 | 3 | $existingProductCategoryUrlRewrites = $this->prepareExistingCategoryUrlRewrites($urlRewrites); |
|
| 96 | |||
| 97 | // delete/create/update the URL rewrites |
||
| 98 | 3 | $this->deleteUrlRewrites($existingProductCategoryUrlRewrites); |
|
| 99 | 3 | $this->updateUrlRewrites(array_intersect_key($existingProductCategoryUrlRewrites, $productCategoryIds)); |
|
| 100 | 3 | $this->createUrlRewrites($productCategoryIds); |
|
| 101 | |||
| 102 | // returns the row |
||
| 103 | 3 | return $row; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Prepare's and set's the URL key from the passed row of the CSV file. |
||
| 108 | * |
||
| 109 | * @param array $row The row with the CSV data |
||
| 110 | * |
||
| 111 | * @return boolean TRUE, if the URL key has been prepared, else FALSE |
||
| 112 | */ |
||
| 113 | 3 | protected function prepareUrlKey($row) |
|
| 114 | { |
||
| 115 | |||
| 116 | // load the header information |
||
| 117 | 3 | $headers = $this->getHeaders(); |
|
| 118 | |||
| 119 | // query whether or not we've a URL key available in the CSV file row |
||
| 120 | 3 | if (isset($row[$headers[ColumnKeys::URL_KEY]])) { |
|
| 121 | 3 | $urlKey = $row[$headers[ColumnKeys::URL_KEY]]; |
|
| 122 | } |
||
| 123 | |||
| 124 | // query whether or not an URL key has been specified in the CSV file |
||
| 125 | 3 | if (empty($urlKey)) { |
|
| 126 | // if not, try to use the product name |
||
| 127 | if (isset($row[$headers[ColumnKeys::NAME]])) { |
||
| 128 | $productName = $row[$headers[ColumnKeys::NAME]]; |
||
| 129 | } |
||
| 130 | |||
| 131 | // if nor URL key AND product name are empty, return immediately |
||
| 132 | if (empty($productName)) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | // initialize the URL key with product name |
||
| 137 | $urlKey = $productName; |
||
| 138 | } |
||
| 139 | |||
| 140 | // convert and set the URL key |
||
| 141 | 3 | $this->setUrlKey($this->convertNameToUrlKey($urlKey)); |
|
| 142 | |||
| 143 | // return TRUE if the URL key has been prepared |
||
| 144 | 3 | return true; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set's the prepared URL key. |
||
| 149 | * |
||
| 150 | * @param string $urlKey The prepared URL key |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | 3 | protected function setUrlKey($urlKey) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Return's the prepared URL key. |
||
| 161 | * |
||
| 162 | * @return string The prepared URL key |
||
| 163 | */ |
||
| 164 | 3 | protected function getUrlKey() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Initialize's and return's the URL key filter. |
||
| 171 | * |
||
| 172 | * @return \TechDivision\Import\Product\Utils\ConvertLiteralUrl The URL key filter |
||
| 173 | */ |
||
| 174 | 3 | protected function getUrlKeyFilter() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Convert's the passed string into a valid URL key. |
||
| 181 | * |
||
| 182 | * @param string $string The string to be converted, e. g. the product name |
||
| 183 | * |
||
| 184 | * @return string The converted string as valid URL key |
||
| 185 | */ |
||
| 186 | 3 | protected function convertNameToUrlKey($string) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Convert's the passed URL rewrites into an array with the category ID from the |
||
| 193 | * metadata as key and the URL rewrite as value. |
||
| 194 | * |
||
| 195 | * If now category ID can be found in the metadata, the ID of the store's root |
||
| 196 | * category is used. |
||
| 197 | * |
||
| 198 | * @param array $urlRewrites The URL rewrites to convert |
||
| 199 | * |
||
| 200 | * @return array The converted array with the de-serialized category IDs as key |
||
| 201 | */ |
||
| 202 | 3 | protected function prepareExistingCategoryUrlRewrites(array $urlRewrites) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Remove's the URL rewrites with the passed data. |
||
| 236 | * |
||
| 237 | * @param array $existingProductCategoryUrlRewrites The array with the URL rewrites to remove |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | 3 | protected function deleteUrlRewrites(array $existingProductCategoryUrlRewrites) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Create's the URL rewrites from the passed data. |
||
| 257 | * |
||
| 258 | * @param array $productCategoryIds The categories to create a URL rewrite for |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | 3 | protected function createUrlRewrites(array $productCategoryIds) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Update's existing URL rewrites by creating 301 redirect URL rewrites for each. |
||
| 290 | * |
||
| 291 | * @param array $existingProductCategoryUrlRewrites The array with the existing URL rewrites |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | 3 | protected function updateUrlRewrites(array $existingProductCategoryUrlRewrites) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Prepare's the target path for a URL rewrite. |
||
| 324 | * |
||
| 325 | * @param array $category The categroy with the URL path |
||
| 326 | * |
||
| 327 | * @return string The target path |
||
| 328 | */ |
||
| 329 | 3 | protected function prepareTargetPath(array $category) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Prepare's the request path for a URL rewrite or the target path for a 301 redirect. |
||
| 348 | * |
||
| 349 | * @param array $category The categroy with the URL path |
||
| 350 | * |
||
| 351 | * @return string The request path |
||
| 352 | */ |
||
| 353 | 3 | protected function prepareRequestPath(array $category) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
| 369 | * |
||
| 370 | * @param array $category The category used for preparation |
||
| 371 | * |
||
| 372 | * @return array The metadata |
||
| 373 | */ |
||
| 374 | 3 | protected function prepareMetadata(array $category) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Set's the store view code the create the product/attributes for. |
||
| 394 | * |
||
| 395 | * @param string $storeViewCode The store view code |
||
| 396 | * |
||
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | 3 | public function setStoreViewCode($storeViewCode) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Return's the root category for the actual view store. |
||
| 406 | * |
||
| 407 | * @return array The store's root category |
||
| 408 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 409 | */ |
||
| 410 | 3 | public function getRootCategory() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
| 417 | * |
||
| 418 | * @param array $category The category to query |
||
| 419 | * |
||
| 420 | * @return boolean TRUE if the passed category IS the root category |
||
| 421 | */ |
||
| 422 | 3 | public function isRootCategory(array $category) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Return's the list with category IDs the product is related with. |
||
| 434 | * |
||
| 435 | * @return array The product's category IDs |
||
| 436 | */ |
||
| 437 | 4 | public function getProductCategoryIds() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Return's the category with the passed ID. |
||
| 444 | * |
||
| 445 | * @param integer $categoryId The ID of the category to return |
||
| 446 | * |
||
| 447 | * @return array The category data |
||
| 448 | */ |
||
| 449 | 3 | public function getCategory($categoryId) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
| 456 | * |
||
| 457 | * @param string $entityType The entity type to load the URL rewrites for |
||
| 458 | * @param integer $entityId The entity ID to laod the rewrites for |
||
| 459 | * |
||
| 460 | * @return array The URL rewrites |
||
| 461 | */ |
||
| 462 | 4 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Persist's the URL write with the passed data. |
||
| 469 | * |
||
| 470 | * @param array $row The URL rewrite to persist |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | 4 | public function persistUrlRewrite($row) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Delete's the URL rewrite with the passed attributes. |
||
| 481 | * |
||
| 482 | * @param array $row The attributes of the entity to remove |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | 3 | public function removeUrlRewrite($row) |
|
| 490 | } |
||
| 491 |