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