Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TrashService 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 TrashService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class TrashService implements TrashServiceInterface |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 41 | */ |
||
| 42 | protected $repository; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 46 | */ |
||
| 47 | protected $persistenceHandler; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $settings; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 56 | */ |
||
| 57 | protected $nameSchemaService; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 61 | * |
||
| 62 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 63 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 64 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 65 | * @param \eZ\Publish\API\Repository\PermissionCriterionResolver $permissionCriterionResolver |
||
| 66 | * @param array $settings |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 69 | RepositoryInterface $repository, |
||
| 70 | Handler $handler, |
||
| 71 | Helper\NameSchemaService $nameSchemaService, |
||
| 72 | PermissionCriterionResolver $permissionCriterionResolver, |
||
| 73 | array $settings = array() |
||
| 74 | ) { |
||
| 75 | $this->permissionCriterionResolver = $permissionCriterionResolver; |
||
|
|
|||
| 76 | $this->repository = $repository; |
||
| 77 | $this->persistenceHandler = $handler; |
||
| 78 | $this->nameSchemaService = $nameSchemaService; |
||
| 79 | // Union makes sure default settings are ignored if provided in argument |
||
| 80 | $this->settings = $settings + array( |
||
| 81 | //'defaultSetting' => array(), |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Loads a trashed location object from its $id. |
||
| 87 | * |
||
| 88 | * Note that $id is identical to original location, which has been previously trashed |
||
| 89 | * |
||
| 90 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
||
| 91 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
||
| 92 | * |
||
| 93 | * @param mixed $trashItemId |
||
| 94 | * |
||
| 95 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 96 | */ |
||
| 97 | public function loadTrashItem($trashItemId) |
||
| 98 | { |
||
| 99 | $spiTrashItem = $this->persistenceHandler->trashHandler()->loadTrashItem($trashItemId); |
||
| 100 | $trash = $this->buildDomainTrashItemObject( |
||
| 101 | $spiTrashItem, |
||
| 102 | $this->repository->getContentService()->internalLoadContent($spiTrashItem->contentId) |
||
| 103 | ); |
||
| 104 | if (!$this->repository->canUser('content', 'read', $trash->getContentInfo())) { |
||
| 105 | throw new UnauthorizedException('content', 'read'); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!$this->repository->canUser('content', 'restore', $trash->getContentInfo())) { |
||
| 109 | throw new UnauthorizedException('content', 'restore'); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $trash; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
| 117 | * |
||
| 118 | * The current user may not have access to the returned trash item, check before using it. |
||
| 119 | * Content is left untouched. |
||
| 120 | * |
||
| 121 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
| 122 | * |
||
| 123 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 124 | * |
||
| 125 | * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem |
||
| 126 | */ |
||
| 127 | public function trash(Location $location) |
||
| 128 | { |
||
| 129 | if (empty($location->id)) { |
||
| 130 | throw new InvalidArgumentValue('id', $location->id, 'Location'); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (!$this->userHasPermissionsToRemove($location->getContentInfo(), $location)) { |
||
| 134 | throw new UnauthorizedException('content', 'remove'); |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->repository->beginTransaction(); |
||
| 138 | try { |
||
| 139 | $spiTrashItem = $this->persistenceHandler->trashHandler()->trashSubtree($location->id); |
||
| 140 | $this->persistenceHandler->urlAliasHandler()->locationDeleted($location->id); |
||
| 141 | $this->repository->commit(); |
||
| 142 | } catch (Exception $e) { |
||
| 143 | $this->repository->rollback(); |
||
| 144 | throw $e; |
||
| 145 | } |
||
| 146 | |||
| 147 | // Use internalLoadContent() as we want a trash item regardless of user access to the trash or not. |
||
| 148 | try { |
||
| 149 | return isset($spiTrashItem) |
||
| 150 | ? $this->buildDomainTrashItemObject( |
||
| 151 | $spiTrashItem, |
||
| 152 | $this->repository->getContentService()->internalLoadContent($spiTrashItem->contentId) |
||
| 153 | ) |
||
| 154 | : null; |
||
| 155 | } catch (Exception $e) { |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Recovers the $trashedLocation at its original place if possible. |
||
| 162 | * |
||
| 163 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
| 164 | * |
||
| 165 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
| 166 | * |
||
| 167 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 168 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 169 | * |
||
| 170 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
| 171 | */ |
||
| 172 | public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
||
| 173 | { |
||
| 174 | if (!is_numeric($trashItem->id)) { |
||
| 175 | throw new InvalidArgumentValue('id', $trashItem->id, 'TrashItem'); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($newParentLocation === null && !is_numeric($trashItem->parentLocationId)) { |
||
| 179 | throw new InvalidArgumentValue('parentLocationId', $trashItem->parentLocationId, 'TrashItem'); |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($newParentLocation !== null && !is_numeric($newParentLocation->id)) { |
||
| 183 | throw new InvalidArgumentValue('parentLocationId', $newParentLocation->id, 'Location'); |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!$this->repository->canUser( |
||
| 187 | 'content', |
||
| 188 | 'restore', |
||
| 189 | $trashItem->getContentInfo(), |
||
| 190 | [$newParentLocation ?: $trashItem] |
||
| 191 | )) { |
||
| 192 | throw new UnauthorizedException('content', 'restore'); |
||
| 193 | } |
||
| 194 | |||
| 195 | $this->repository->beginTransaction(); |
||
| 196 | try { |
||
| 197 | $newParentLocationId = $newParentLocation ? $newParentLocation->id : $trashItem->parentLocationId; |
||
| 198 | $newLocationId = $this->persistenceHandler->trashHandler()->recover( |
||
| 199 | $trashItem->id, |
||
| 200 | $newParentLocationId |
||
| 201 | ); |
||
| 202 | |||
| 203 | $content = $this->repository->getContentService()->loadContent($trashItem->contentId); |
||
| 204 | $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
||
| 205 | |||
| 206 | // Publish URL aliases for recovered location |
||
| 207 | foreach ($urlAliasNames as $languageCode => $name) { |
||
| 208 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 209 | $newLocationId, |
||
| 210 | $newParentLocationId, |
||
| 211 | $name, |
||
| 212 | $languageCode, |
||
| 213 | $content->contentInfo->alwaysAvailable |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->repository->commit(); |
||
| 218 | } catch (Exception $e) { |
||
| 219 | $this->repository->rollback(); |
||
| 220 | throw $e; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $this->repository->getLocationService()->loadLocation($newLocationId); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Empties trash. |
||
| 228 | * |
||
| 229 | * All locations contained in the trash will be removed. Content objects will be removed |
||
| 230 | * if all locations of the content are gone. |
||
| 231 | * |
||
| 232 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function emptyTrash() |
|
| 235 | { |
||
| 236 | // Will throw if you have Role assignment limitation where you have content/cleantrash permission. |
||
| 237 | // This is by design and means you can only delete one and one trash item, or you'll need to change how |
||
| 238 | // permissions is assigned to be on separate role with no Role assignment limitation. |
||
| 239 | if ($this->repository->hasAccess('content', 'cleantrash') !== true) { |
||
| 240 | throw new UnauthorizedException('content', 'cleantrash'); |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->repository->beginTransaction(); |
||
| 244 | try { |
||
| 245 | // Persistence layer takes care of deleting content objects |
||
| 246 | $this->persistenceHandler->trashHandler()->emptyTrash(); |
||
| 247 | $this->repository->commit(); |
||
| 248 | } catch (Exception $e) { |
||
| 249 | $this->repository->rollback(); |
||
| 250 | throw $e; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Deletes a trash item. |
||
| 256 | * |
||
| 257 | * The corresponding content object will be removed |
||
| 258 | * |
||
| 259 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
| 260 | * |
||
| 261 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 262 | */ |
||
| 263 | public function deleteTrashItem(APITrashItem $trashItem) |
||
| 264 | { |
||
| 265 | if (!$this->repository->canUser('content', 'cleantrash', $trashItem->getContentInfo())) { |
||
| 266 | throw new UnauthorizedException('content', 'cleantrash'); |
||
| 267 | } |
||
| 268 | |||
| 269 | if (!is_numeric($trashItem->id)) { |
||
| 270 | throw new InvalidArgumentValue('id', $trashItem->id, 'TrashItem'); |
||
| 271 | } |
||
| 272 | |||
| 273 | $this->repository->beginTransaction(); |
||
| 274 | try { |
||
| 275 | $this->persistenceHandler->trashHandler()->deleteTrashItem($trashItem->id); |
||
| 276 | $this->repository->commit(); |
||
| 277 | } catch (Exception $e) { |
||
| 278 | $this->repository->rollback(); |
||
| 279 | throw $e; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns a collection of Trashed locations contained in the trash, which are readable by the current user. |
||
| 285 | * |
||
| 286 | * $query allows to filter/sort the elements to be contained in the collection. |
||
| 287 | * |
||
| 288 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 289 | * |
||
| 290 | * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult |
||
| 291 | */ |
||
| 292 | public function findTrashItems(Query $query) |
||
| 293 | { |
||
| 294 | if ($query->filter !== null && !$query->filter instanceof Criterion) { |
||
| 295 | throw new InvalidArgumentValue('query->filter', $query->filter, 'Query'); |
||
| 296 | } |
||
| 297 | |||
| 298 | if ($query->sortClauses !== null) { |
||
| 299 | if (!is_array($query->sortClauses)) { |
||
| 300 | throw new InvalidArgumentValue('query->sortClauses', $query->sortClauses, 'Query'); |
||
| 301 | } |
||
| 302 | |||
| 303 | foreach ($query->sortClauses as $sortClause) { |
||
| 304 | if (!$sortClause instanceof SortClause) { |
||
| 305 | throw new InvalidArgumentValue('query->sortClauses', 'only instances of SortClause class are allowed'); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | View Code Duplication | if ($query->offset !== null && !is_numeric($query->offset)) { |
|
| 311 | throw new InvalidArgumentValue('query->offset', $query->offset, 'Query'); |
||
| 312 | } |
||
| 313 | |||
| 314 | View Code Duplication | if ($query->limit !== null && !is_numeric($query->limit)) { |
|
| 315 | throw new InvalidArgumentValue('query->limit', $query->limit, 'Query'); |
||
| 316 | } |
||
| 317 | |||
| 318 | $spiTrashItems = $this->persistenceHandler->trashHandler()->findTrashItems( |
||
| 319 | $query->filter, |
||
| 320 | $query->offset !== null && $query->offset > 0 ? (int)$query->offset : 0, |
||
| 321 | $query->limit !== null && $query->limit >= 1 ? (int)$query->limit : null, |
||
| 322 | $query->sortClauses |
||
| 323 | ); |
||
| 324 | |||
| 325 | $trashItems = $this->buildDomainTrashItems($spiTrashItems); |
||
| 326 | $searchResult = new SearchResult(); |
||
| 327 | $searchResult->totalCount = $searchResult->count = count($trashItems); |
||
| 328 | $searchResult->items = $trashItems; |
||
| 329 | |||
| 330 | return $searchResult; |
||
| 331 | } |
||
| 332 | |||
| 333 | protected function buildDomainTrashItems(array $spiTrashItems): array |
||
| 350 | |||
| 351 | protected function buildDomainTrashItemObject(Trashed $spiTrashItem, Content $content): APITrashItem |
||
| 352 | { |
||
| 353 | return new TrashItem( |
||
| 354 | array( |
||
| 355 | 'content' => $content, |
||
| 356 | 'contentInfo' => $content->contentInfo, |
||
| 357 | 'id' => $spiTrashItem->id, |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param int $timestamp |
||
| 374 | * |
||
| 375 | * @return \DateTime |
||
| 376 | */ |
||
| 377 | protected function getDateTime($timestamp) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 387 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | * |
||
| 391 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 392 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 393 | */ |
||
| 394 | private function userHasPermissionsToRemove(ContentInfo $contentInfo, Location $location) |
||
| 418 | } |
||
| 419 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: