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:
| 1 | <?php |
||
| 24 | class TrashService implements TrashServiceInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Aggregated service. |
||
| 28 | * |
||
| 29 | * @var \eZ\Publish\API\Repository\TrashService |
||
| 30 | */ |
||
| 31 | protected $service; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * SignalDispatcher. |
||
| 35 | * |
||
| 36 | * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher |
||
| 37 | */ |
||
| 38 | protected $signalDispatcher; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * Construct service object from aggregated service and signal |
||
| 44 | * dispatcher |
||
| 45 | * |
||
| 46 | * @param \eZ\Publish\API\Repository\TrashService $service |
||
| 47 | * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher |
||
| 48 | */ |
||
| 49 | public function __construct(TrashServiceInterface $service, SignalDispatcher $signalDispatcher) |
||
| 50 | { |
||
| 51 | $this->service = $service; |
||
| 52 | $this->signalDispatcher = $signalDispatcher; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Loads a trashed location object from its $id. |
||
| 57 | * |
||
| 58 | * Note that $id is identical to original location, which has been previously trashed |
||
| 59 | * |
||
| 60 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
||
| 61 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
||
| 62 | * |
||
| 63 | * @param mixed $trashItemId |
||
| 64 | * |
||
| 65 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 66 | */ |
||
| 67 | public function loadTrashItem($trashItemId) |
||
| 68 | { |
||
| 69 | return $this->service->loadTrashItem($trashItemId); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
| 74 | * |
||
| 75 | * The current user may not have access to the returned trash item, check before using it. |
||
| 76 | * Content is left untouched. |
||
| 77 | * |
||
| 78 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
| 79 | * |
||
| 80 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 81 | * |
||
| 82 | * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function trash(Location $location) |
|
| 85 | { |
||
| 86 | $returnValue = $this->service->trash($location); |
||
| 87 | $this->signalDispatcher->emit( |
||
| 88 | new TrashSignal( |
||
| 89 | array( |
||
| 90 | 'locationId' => $location->id, |
||
| 91 | 'parentLocationId' => $location->parentLocationId, |
||
| 92 | 'contentId' => $location->contentId, |
||
| 93 | 'contentTrashed' => $returnValue instanceof TrashItem, |
||
| 94 | ) |
||
| 95 | ) |
||
| 96 | ); |
||
| 97 | |||
| 98 | return $returnValue; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Recovers the $trashedLocation at its original place if possible. |
||
| 103 | * |
||
| 104 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
| 105 | * |
||
| 106 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
| 107 | * |
||
| 108 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 109 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 110 | * |
||
| 111 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
| 112 | */ |
||
| 113 | public function recover(TrashItem $trashItem, Location $newParentLocation = null) |
||
| 114 | { |
||
| 115 | $newLocation = $this->service->recover($trashItem, $newParentLocation); |
||
| 116 | $this->signalDispatcher->emit( |
||
| 117 | new RecoverSignal( |
||
| 118 | array( |
||
| 119 | 'trashItemId' => $trashItem->id, |
||
| 120 | 'contentId' => $trashItem->contentId, |
||
| 121 | 'newParentLocationId' => $newLocation->parentLocationId, |
||
| 122 | 'newLocationId' => $newLocation->id, |
||
| 123 | ) |
||
| 124 | ) |
||
| 125 | ); |
||
| 126 | |||
| 127 | return $newLocation; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Empties trash. |
||
| 132 | * |
||
| 133 | * All locations contained in the trash will be removed. Content objects will be removed |
||
| 134 | * if all locations of the content are gone. |
||
| 135 | * |
||
| 136 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
| 137 | */ |
||
| 138 | public function emptyTrash() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Deletes a trash item. |
||
| 148 | * |
||
| 149 | * The corresponding content object will be removed |
||
| 150 | * |
||
| 151 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
| 152 | * |
||
| 153 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 154 | */ |
||
| 155 | public function deleteTrashItem(TrashItem $trashItem) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns a collection of Trashed locations contained in the trash, which are readable by the current user. |
||
| 171 | * |
||
| 172 | * $query allows to filter/sort the elements to be contained in the collection. |
||
| 173 | * |
||
| 174 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 175 | * |
||
| 176 | * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult |
||
| 177 | */ |
||
| 178 | public function findTrashItems(Query $query) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Checks if Content is in trash when we can't rely only on locationId (ie. because ContentId is all we have, |
||
| 185 | * and Content in trash is not part of a tree anymore, so does not have mainLocationId). |
||
| 186 | * |
||
| 187 | * @param int $contentId |
||
| 188 | * |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function isContentInTrash($contentId) |
||
| 195 | } |
||
| 196 |