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($sku = $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->prepareStoreViewCode($row); |
|
| 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) |
|
| 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) |
|
| 330 | { |
||
| 331 | |||
| 332 | // load the actual entity ID |
||
| 333 | 3 | $lastEntityId = $this->getLastEntityId(); |
|
| 334 | |||
| 335 | // query whether or not, the category is the root category |
||
| 336 | 3 | if ($this->isRootCategory($category)) { |
|
| 337 | 3 | $targetPath = sprintf('catalog/product/view/id/%d', $lastEntityId); |
|
| 338 | 3 | } else { |
|
| 339 | 2 | $targetPath = sprintf('catalog/product/view/id/%d/category/%d', $lastEntityId, $category[MemberNames::ENTITY_ID]); |
|
| 340 | } |
||
| 341 | |||
| 342 | // return the target path |
||
| 343 | 3 | return $targetPath; |
|
| 344 | } |
||
| 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) |
|
| 354 | { |
||
| 355 | |||
| 356 | // query whether or not, the category is the root category |
||
| 357 | 3 | if ($this->isRootCategory($category)) { |
|
| 358 | 3 | $requestPath = sprintf('%s.html', $this->getUrlKey()); |
|
| 359 | 3 | } else { |
|
| 360 | 2 | $requestPath = sprintf('%s/%s.html', $category[MemberNames::URL_PATH], $this->getUrlKey()); |
|
| 361 | } |
||
| 362 | |||
| 363 | // return the request path |
||
| 364 | 3 | return $requestPath; |
|
| 365 | } |
||
| 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 | * Return's the default store. |
||
| 469 | * |
||
| 470 | * @return array The default store |
||
| 471 | */ |
||
| 472 | public function getDefaultStore() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Persist's the URL write with the passed data. |
||
| 479 | * |
||
| 480 | * @param array $row The URL rewrite to persist |
||
| 481 | * |
||
| 482 | * @return void |
||
| 483 | */ |
||
| 484 | 4 | public function persistUrlRewrite($row) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Delete's the URL rewrite with the passed attributes. |
||
| 491 | * |
||
| 492 | * @param array $row The attributes of the entity to remove |
||
| 493 | * |
||
| 494 | * @return void |
||
| 495 | */ |
||
| 496 | 3 | public function removeUrlRewrite($row) |
|
| 500 | } |
||
| 501 |