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 |
||
| 24 | class Handler implements BaseLocationHandler |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Gateway for handling location data. |
||
| 28 | * |
||
| 29 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 30 | */ |
||
| 31 | protected $locationGateway; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Location locationMapper. |
||
| 35 | * |
||
| 36 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
| 37 | */ |
||
| 38 | protected $locationMapper; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Content handler. |
||
| 42 | * |
||
| 43 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Handler |
||
| 44 | */ |
||
| 45 | protected $contentHandler; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Object state handler. |
||
| 49 | * |
||
| 50 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler |
||
| 51 | */ |
||
| 52 | protected $objectStateHandler; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Tree handler. |
||
| 56 | * |
||
| 57 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler |
||
| 58 | */ |
||
| 59 | protected $treeHandler; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Construct from userGateway. |
||
| 63 | * |
||
| 64 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 65 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
| 66 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Handler $contentHandler |
||
| 67 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler $objectStateHandler |
||
| 68 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler $treeHandler |
||
| 69 | * |
||
| 70 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler |
||
|
|
|||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns parent path string for a path string. |
||
| 88 | * |
||
| 89 | * @param string $pathString |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | protected function getParentPathString($pathString) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function load($locationId, array $translations = null, bool $useAlwaysAvailable = true) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function loadList(array $locationIds, array $translations = null, bool $useAlwaysAvailable = true): iterable |
||
| 110 | { |
||
| 111 | $list = $this->locationGateway->getNodeDataList($locationIds, $translations, $useAlwaysAvailable); |
||
| 112 | |||
| 113 | $locations = []; |
||
| 114 | foreach ($list as $row) { |
||
| 115 | $id = (int)$row['node_id']; |
||
| 116 | if (!isset($locations[$id])) { |
||
| 117 | $locations[$id] = $this->locationMapper->createLocationFromRow($row); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return $locations; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Loads the subtree ids of the location identified by $locationId. |
||
| 126 | * |
||
| 127 | * @param int $locationId |
||
| 128 | * |
||
| 129 | * @return array Location ids are in the index, Content ids in the value. |
||
| 130 | */ |
||
| 131 | public function loadSubtreeIds($locationId) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function loadByRemoteId($remoteId, array $translations = null, bool $useAlwaysAvailable = true) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Loads all locations for $contentId, optionally limited to a sub tree |
||
| 148 | * identified by $rootLocationId. |
||
| 149 | * |
||
| 150 | * @param int $contentId |
||
| 151 | * @param int $rootLocationId |
||
| 152 | * |
||
| 153 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 154 | */ |
||
| 155 | public function loadLocationsByContent($contentId, $rootLocationId = null) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @see \eZ\Publish\SPI\Persistence\Content\Location\Handler::loadParentLocationsForDraftContent |
||
| 164 | */ |
||
| 165 | public function loadParentLocationsForDraftContent($contentId) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns an array of default content states with content state group id as key. |
||
| 174 | * |
||
| 175 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
| 176 | */ |
||
| 177 | protected function getDefaultContentStates() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param Content $content |
||
| 194 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState[] $contentStates |
||
| 195 | */ |
||
| 196 | protected function setContentStates(Content $content, array $contentStates) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Copy location object identified by $sourceId, into destination identified by $destinationParentId. |
||
| 209 | * |
||
| 210 | * Performs a deep copy of the location identified by $sourceId and all of |
||
| 211 | * its child locations, copying the most recent published content object |
||
| 212 | * for each location to a new content object without any additional version |
||
| 213 | * information. Relations are not copied. URLs are not touched at all. |
||
| 214 | * |
||
| 215 | * @todo Either move to async/batch or find ways toward optimizing away operations per object. |
||
| 216 | * @todo Optionally retain dates and set creator |
||
| 217 | * |
||
| 218 | * @param mixed $sourceId |
||
| 219 | * @param mixed $destinationParentId |
||
| 220 | * @param int|null $newOwnerId |
||
| 221 | * |
||
| 222 | * @return Location the newly created Location. |
||
| 223 | * |
||
| 224 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 225 | */ |
||
| 226 | public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Retrieves section ID of the location's content. |
||
| 331 | * |
||
| 332 | * @param int $locationId |
||
| 333 | * |
||
| 334 | * @return int |
||
| 335 | */ |
||
| 336 | private function getSectionId($locationId) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * If the location is the main location for its content, updates subtree section. |
||
| 346 | * |
||
| 347 | * @param Location $location |
||
| 348 | * @param int $sectionId |
||
| 349 | */ |
||
| 350 | private function updateSubtreeSectionIfNecessary(Location $location, $sectionId) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Checks if the location is the main location for its content. |
||
| 359 | * |
||
| 360 | * @param Location $location |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | private function isMainLocation(Location $location) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Moves location identified by $sourceId into new parent identified by $destinationParentId. |
||
| 373 | * |
||
| 374 | * Performs a full move of the location identified by $sourceId to a new |
||
| 375 | * destination, identified by $destinationParentId. Relations do not need |
||
| 376 | * to be updated, since they refer to Content. URLs are not touched. |
||
| 377 | * |
||
| 378 | * @param mixed $sourceId |
||
| 379 | * @param mixed $destinationParentId |
||
| 380 | * |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function move($sourceId, $destinationParentId) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Marks the given nodes and all ancestors as modified. |
||
| 407 | * |
||
| 408 | * Optionally a time stamp with the modification date may be specified, |
||
| 409 | * otherwise the current time is used. |
||
| 410 | * |
||
| 411 | * @param int|string $locationId |
||
| 412 | * @param int $timestamp |
||
| 413 | */ |
||
| 414 | public function markSubtreeModified($locationId, $timestamp = null) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Sets a location to be hidden, and it self + all children to invisible. |
||
| 423 | * |
||
| 424 | * @param mixed $id Location ID |
||
| 425 | */ |
||
| 426 | public function hide($id) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree. |
||
| 435 | * If not make sure only children down to first hidden node is marked visible. |
||
| 436 | * |
||
| 437 | * @param mixed $id |
||
| 438 | */ |
||
| 439 | public function unHide($id) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Sets a location + all children to invisible. |
||
| 448 | * |
||
| 449 | * @param int $id Location ID |
||
| 450 | */ |
||
| 451 | public function setInvisible(int $id): void |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Sets a location + all children to visible. |
||
| 460 | * |
||
| 461 | * @param int $id Location ID |
||
| 462 | */ |
||
| 463 | public function setVisible(int $id): void |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Swaps the content object being pointed to by a location object. |
||
| 472 | * |
||
| 473 | * Make the location identified by $locationId1 refer to the Content |
||
| 474 | * referred to by $locationId2 and vice versa. |
||
| 475 | * |
||
| 476 | * @param mixed $locationId1 |
||
| 477 | * @param mixed $locationId2 |
||
| 478 | * |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function swap($locationId1, $locationId2) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Updates an existing location. |
||
| 488 | * |
||
| 489 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
| 490 | * @param int $locationId |
||
| 491 | */ |
||
| 492 | public function update(UpdateStruct $location, $locationId) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Creates a new location rooted at $location->parentId. |
||
| 499 | * |
||
| 500 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
| 501 | * |
||
| 502 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
| 503 | */ |
||
| 504 | public function create(CreateStruct $createStruct) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Removes all Locations under and including $locationId. |
||
| 519 | * |
||
| 520 | * Performs a recursive delete on the location identified by $locationId, |
||
| 521 | * including all of its child locations. Content which is not referred to |
||
| 522 | * by any other location is automatically removed. Content which looses its |
||
| 523 | * main Location will get the first of its other Locations assigned as the |
||
| 524 | * new main Location. |
||
| 525 | * |
||
| 526 | * @param mixed $locationId |
||
| 527 | * |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function removeSubtree($locationId) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Set section on all content objects in the subtree. |
||
| 537 | * |
||
| 538 | * @param mixed $locationId |
||
| 539 | * @param mixed $sectionId |
||
| 540 | */ |
||
| 541 | public function setSectionForSubtree($locationId, $sectionId) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Changes main location of content identified by given $contentId to location identified by given $locationId. |
||
| 548 | * |
||
| 549 | * Updates ezcontentobject_tree and eznode_assignment tables (eznode_assignment for content current version number). |
||
| 550 | * |
||
| 551 | * @param mixed $contentId |
||
| 552 | * @param mixed $locationId |
||
| 553 | */ |
||
| 554 | public function changeMainLocation($contentId, $locationId) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Get the total number of all existing Locations. Can be combined with loadAllLocations. |
||
| 561 | * |
||
| 562 | * @return int |
||
| 563 | */ |
||
| 564 | public function countAllLocations() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results. |
||
| 571 | * |
||
| 572 | * @param int $offset |
||
| 573 | * @param int $limit |
||
| 574 | * |
||
| 575 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 576 | */ |
||
| 577 | public function loadAllLocations($offset, $limit) |
||
| 583 | } |
||
| 584 |
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.