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