Complex classes like Handler 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 Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Handler implements BaseLocationHandler |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Gateway for handling location data. |
||
| 29 | * |
||
| 30 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 31 | */ |
||
| 32 | protected $locationGateway; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Location locationMapper. |
||
| 36 | * |
||
| 37 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
| 38 | */ |
||
| 39 | protected $locationMapper; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Content handler. |
||
| 43 | * |
||
| 44 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Handler |
||
| 45 | */ |
||
| 46 | protected $contentHandler; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Object state handler. |
||
| 50 | * |
||
| 51 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler |
||
| 52 | */ |
||
| 53 | protected $objectStateHandler; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Tree handler. |
||
| 57 | * |
||
| 58 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler |
||
| 59 | */ |
||
| 60 | protected $treeHandler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Construct from userGateway. |
||
| 64 | * |
||
| 65 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 66 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
| 67 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Handler $contentHandler |
||
| 68 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler $objectStateHandler |
||
| 69 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler $treeHandler |
||
| 70 | * |
||
| 71 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler |
||
|
|
|||
| 72 | */ |
||
| 73 | public function __construct( |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns parent path string for a path string. |
||
| 89 | * |
||
| 90 | * @param string $pathString |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | protected function getParentPathString($pathString) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function load($locationId, array $translations = null, bool $useAlwaysAvailable = true) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function loadList(array $locationIds, array $translations = null, bool $useAlwaysAvailable = true): iterable |
||
| 111 | { |
||
| 112 | $list = $this->locationGateway->getNodeDataList($locationIds, $translations, $useAlwaysAvailable); |
||
| 113 | |||
| 114 | $locations = []; |
||
| 115 | foreach ($list as $row) { |
||
| 116 | $id = (int)$row['node_id']; |
||
| 117 | if (!isset($locations[$id])) { |
||
| 118 | $locations[$id] = $this->locationMapper->createLocationFromRow($row); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $locations; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Loads the subtree ids of the location identified by $locationId. |
||
| 127 | * |
||
| 128 | * @param int $locationId |
||
| 129 | * |
||
| 130 | * @return array Location ids are in the index, Content ids in the value. |
||
| 131 | */ |
||
| 132 | public function loadSubtreeIds($locationId) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function loadByRemoteId($remoteId, array $translations = null, bool $useAlwaysAvailable = true) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Loads all locations for $contentId, optionally limited to a sub tree |
||
| 149 | * identified by $rootLocationId. |
||
| 150 | * |
||
| 151 | * @param int $contentId |
||
| 152 | * @param int $rootLocationId |
||
| 153 | * |
||
| 154 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 155 | */ |
||
| 156 | public function loadLocationsByContent($contentId, $rootLocationId = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Loads all locations for $contentId in trash, optionally limited to a sub tree |
||
| 165 | * identified by $rootLocationId. |
||
| 166 | * |
||
| 167 | * @param int $contentId |
||
| 168 | * @param int $rootLocationId |
||
| 169 | * |
||
| 170 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 171 | */ |
||
| 172 | public function loadLocationsByTrashContent($contentId, $rootLocationId = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @see \eZ\Publish\SPI\Persistence\Content\Location\Handler::loadParentLocationsForDraftContent |
||
| 181 | */ |
||
| 182 | public function loadParentLocationsForDraftContent($contentId) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns an array of default content states with content state group id as key. |
||
| 191 | * |
||
| 192 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
| 193 | */ |
||
| 194 | protected function getDefaultContentStates() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param Content $content |
||
| 211 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState[] $contentStates |
||
| 212 | */ |
||
| 213 | protected function setContentStates(Content $content, array $contentStates) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Copy location object identified by $sourceId, into destination identified by $destinationParentId. |
||
| 226 | * |
||
| 227 | * Performs a deep copy of the location identified by $sourceId and all of |
||
| 228 | * its child locations, copying the most recent published content object |
||
| 229 | * for each location to a new content object without any additional version |
||
| 230 | * information. Relations are not copied. URLs are not touched at all. |
||
| 231 | * |
||
| 232 | * @todo Either move to async/batch or find ways toward optimizing away operations per object. |
||
| 233 | * @todo Optionally retain dates and set creator |
||
| 234 | * |
||
| 235 | * @param mixed $sourceId |
||
| 236 | * @param mixed $destinationParentId |
||
| 237 | * @param int|null $newOwnerId |
||
| 238 | * |
||
| 239 | * @return Location the newly created Location. |
||
| 240 | * |
||
| 241 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 242 | */ |
||
| 243 | public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Retrieves section ID of the location's content. |
||
| 348 | * |
||
| 349 | * @param int $locationId |
||
| 350 | * |
||
| 351 | * @return int |
||
| 352 | */ |
||
| 353 | private function getSectionId($locationId) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * If the location is the main location for its content, updates subtree section. |
||
| 363 | * |
||
| 364 | * @param Location $location |
||
| 365 | * @param int $sectionId |
||
| 366 | */ |
||
| 367 | private function updateSubtreeSectionIfNecessary(Location $location, $sectionId) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Checks if the location is the main location for its content. |
||
| 376 | * |
||
| 377 | * @param Location $location |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | private function isMainLocation(Location $location) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Moves location identified by $sourceId into new parent identified by $destinationParentId. |
||
| 390 | * |
||
| 391 | * Performs a full move of the location identified by $sourceId to a new |
||
| 392 | * destination, identified by $destinationParentId. Relations do not need |
||
| 393 | * to be updated, since they refer to Content. URLs are not touched. |
||
| 394 | * |
||
| 395 | * @param mixed $sourceId |
||
| 396 | * @param mixed $destinationParentId |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function move($sourceId, $destinationParentId) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Marks the given nodes and all ancestors as modified. |
||
| 424 | * |
||
| 425 | * Optionally a time stamp with the modification date may be specified, |
||
| 426 | * otherwise the current time is used. |
||
| 427 | * |
||
| 428 | * @param int|string $locationId |
||
| 429 | * @param int $timestamp |
||
| 430 | */ |
||
| 431 | public function markSubtreeModified($locationId, $timestamp = null) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Sets a location to be hidden, and it self + all children to invisible. |
||
| 440 | * |
||
| 441 | * @param mixed $id Location ID |
||
| 442 | */ |
||
| 443 | public function hide($id) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree. |
||
| 452 | * If not make sure only children down to first hidden node is marked visible. |
||
| 453 | * |
||
| 454 | * @param mixed $id |
||
| 455 | */ |
||
| 456 | public function unHide($id) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Sets a location + all children to invisible. |
||
| 465 | * |
||
| 466 | * @param int $id Location ID |
||
| 467 | */ |
||
| 468 | public function setInvisible(int $id): void |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Sets a location + all children to visible. |
||
| 477 | * |
||
| 478 | * @param int $id Location ID |
||
| 479 | */ |
||
| 480 | public function setVisible(int $id): void |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Swaps the content object being pointed to by a location object. |
||
| 489 | * |
||
| 490 | * Make the location identified by $locationId1 refer to the Content |
||
| 491 | * referred to by $locationId2 and vice versa. |
||
| 492 | * |
||
| 493 | * @param mixed $locationId1 |
||
| 494 | * @param mixed $locationId2 |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | public function swap($locationId1, $locationId2) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Updates an existing location. |
||
| 505 | * |
||
| 506 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
| 507 | * @param int $locationId |
||
| 508 | */ |
||
| 509 | public function update(UpdateStruct $location, $locationId) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Creates a new location rooted at $location->parentId. |
||
| 516 | * |
||
| 517 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
| 518 | * |
||
| 519 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
| 520 | */ |
||
| 521 | public function create(CreateStruct $createStruct) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Removes all Locations under and including $locationId. |
||
| 536 | * |
||
| 537 | * Performs a recursive delete on the location identified by $locationId, |
||
| 538 | * including all of its child locations. Content which is not referred to |
||
| 539 | * by any other location is automatically removed. Content which looses its |
||
| 540 | * main Location will get the first of its other Locations assigned as the |
||
| 541 | * new main Location. |
||
| 542 | * |
||
| 543 | * @param mixed $locationId |
||
| 544 | * |
||
| 545 | * @return bool |
||
| 546 | */ |
||
| 547 | public function removeSubtree($locationId) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Set section on all content objects in the subtree. |
||
| 554 | * |
||
| 555 | * @param mixed $locationId |
||
| 556 | * @param mixed $sectionId |
||
| 557 | */ |
||
| 558 | public function setSectionForSubtree($locationId, $sectionId) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Changes main location of content identified by given $contentId to location identified by given $locationId. |
||
| 565 | * |
||
| 566 | * Updates ezcontentobject_tree and eznode_assignment tables (eznode_assignment for content current version number). |
||
| 567 | * |
||
| 568 | * @param mixed $contentId |
||
| 569 | * @param mixed $locationId |
||
| 570 | */ |
||
| 571 | public function changeMainLocation($contentId, $locationId) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get the total number of all existing Locations. Can be combined with loadAllLocations. |
||
| 578 | * |
||
| 579 | * @return int |
||
| 580 | */ |
||
| 581 | public function countAllLocations() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results. |
||
| 588 | * |
||
| 589 | * @param int $offset |
||
| 590 | * @param int $limit |
||
| 591 | * |
||
| 592 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 593 | */ |
||
| 594 | public function loadAllLocations($offset, $limit) |
||
| 600 | } |
||
| 601 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.