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 |
||
| 29 | class LocationService implements LocationServiceInterface |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Aggregated service. |
||
| 33 | * |
||
| 34 | * @var \eZ\Publish\API\Repository\LocationService |
||
| 35 | */ |
||
| 36 | protected $service; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * SignalDispatcher. |
||
| 40 | * |
||
| 41 | * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher |
||
| 42 | */ |
||
| 43 | protected $signalDispatcher; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor. |
||
| 47 | * |
||
| 48 | * Construct service object from aggregated service and signal |
||
| 49 | * dispatcher |
||
| 50 | * |
||
| 51 | * @param \eZ\Publish\API\Repository\LocationService $service |
||
| 52 | * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher |
||
| 53 | */ |
||
| 54 | public function __construct(LocationServiceInterface $service, SignalDispatcher $signalDispatcher) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
||
| 62 | * |
||
| 63 | * Only the items on which the user has read access are copied. |
||
| 64 | * |
||
| 65 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
||
| 66 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
||
| 67 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
||
| 68 | * |
||
| 69 | * @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
||
| 70 | * @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
||
| 71 | * |
||
| 72 | * @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
||
| 73 | */ |
||
| 74 | public function copySubtree(Location $subtree, Location $targetParentLocation) |
||
| 75 | { |
||
| 76 | $returnValue = $this->service->copySubtree($subtree, $targetParentLocation); |
||
| 77 | $this->signalDispatcher->emit( |
||
| 78 | new CopySubtreeSignal( |
||
| 79 | array( |
||
| 80 | 'subtreeId' => $subtree->id, |
||
| 81 | 'targetParentLocationId' => $targetParentLocation->id, |
||
| 82 | 'targetNewSubtreeId' => $returnValue->id, |
||
| 83 | ) |
||
| 84 | ) |
||
| 85 | ); |
||
| 86 | |||
| 87 | return $returnValue; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Loads a location object from its $locationId. |
||
| 92 | * |
||
| 93 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
||
| 94 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
||
| 95 | * |
||
| 96 | * @param mixed $locationId |
||
| 97 | * |
||
| 98 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 99 | */ |
||
| 100 | public function loadLocation($locationId) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Loads a location object from its $remoteId. |
||
| 107 | * |
||
| 108 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
||
| 109 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
||
| 110 | * |
||
| 111 | * @param string $remoteId |
||
| 112 | * |
||
| 113 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 114 | */ |
||
| 115 | public function loadLocationByRemoteId($remoteId) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Loads the locations for the given content object. |
||
| 122 | * |
||
| 123 | * If a $rootLocation is given, only locations that belong to this location are returned. |
||
| 124 | * The location list is also filtered by permissions on reading locations. |
||
| 125 | * |
||
| 126 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if there is no published version yet |
||
| 127 | * |
||
| 128 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 129 | * @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation |
||
| 130 | * |
||
| 131 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] An array of {@link Location} |
||
| 132 | */ |
||
| 133 | public function loadLocations(ContentInfo $contentInfo, Location $rootLocation = null) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Loads children which are readable by the current user of a location object sorted by sortField and sortOrder. |
||
| 140 | * |
||
| 141 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 142 | * @param int $offset the start offset for paging |
||
| 143 | * @param int $limit the number of locations returned |
||
| 144 | * |
||
| 145 | * @return \eZ\Publish\API\Repository\Values\Content\LocationList |
||
| 146 | */ |
||
| 147 | public function loadLocationChildren(Location $location, $offset = 0, $limit = 25) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function loadParentLocationsForDraftContent(VersionInfo $versionInfo) |
||
| 156 | { |
||
| 157 | return $this->service->loadParentLocationsForDraftContent($versionInfo); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the number of children which are readable by the current user of a location object. |
||
| 162 | * |
||
| 163 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 164 | * |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | public function getLocationChildCount(Location $location) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Creates the new $location in the content repository for the given content. |
||
| 174 | * |
||
| 175 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
||
| 176 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
||
| 177 | * or the parent is a sub location of the location of the content |
||
| 178 | * or if set the remoteId exists already |
||
| 179 | * |
||
| 180 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 181 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
| 182 | * |
||
| 183 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
||
| 184 | */ |
||
| 185 | public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Updates $location in the content repository. |
||
| 203 | * |
||
| 204 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
||
| 205 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
||
| 206 | * |
||
| 207 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 208 | * @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
||
| 209 | * |
||
| 210 | * @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
||
| 211 | */ |
||
| 212 | public function updateLocation(Location $location, LocationUpdateStruct $locationUpdateStruct) |
||
| 213 | { |
||
| 214 | $returnValue = $this->service->updateLocation($location, $locationUpdateStruct); |
||
| 215 | $this->signalDispatcher->emit( |
||
| 216 | new UpdateLocationSignal( |
||
| 217 | array( |
||
| 218 | 'contentId' => $location->contentId, |
||
| 219 | 'locationId' => $location->id, |
||
| 220 | 'parentLocationId' => $location->parentLocationId, |
||
| 221 | ) |
||
| 222 | ) |
||
| 223 | ); |
||
| 224 | |||
| 225 | return $returnValue; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Swaps the contents held by $location1 and $location2. |
||
| 230 | * |
||
| 231 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
||
| 232 | * |
||
| 233 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
||
| 234 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
||
| 235 | */ |
||
| 236 | public function swapLocation(Location $location1, Location $location2) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Hides the $location and marks invisible all descendants of $location. |
||
| 257 | * |
||
| 258 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
||
| 259 | * |
||
| 260 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 261 | * |
||
| 262 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function hideLocation(Location $location) |
|
| 265 | { |
||
| 266 | $returnValue = $this->service->hideLocation($location); |
||
| 267 | $this->signalDispatcher->emit( |
||
| 268 | new HideLocationSignal( |
||
| 269 | array( |
||
| 270 | 'locationId' => $location->id, |
||
| 271 | 'contentId' => $location->contentId, |
||
| 272 | 'currentVersionNo' => $returnValue->getContentInfo()->currentVersionNo, |
||
| 273 | 'parentLocationId' => $returnValue->parentLocationId, |
||
| 274 | ) |
||
| 275 | ) |
||
| 276 | ); |
||
| 277 | |||
| 278 | return $returnValue; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Unhides the $location. |
||
| 283 | * |
||
| 284 | * This method and marks visible all descendants of $locations |
||
| 285 | * until a hidden location is found. |
||
| 286 | * |
||
| 287 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
||
| 288 | * |
||
| 289 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 290 | * |
||
| 291 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function unhideLocation(Location $location) |
|
| 294 | { |
||
| 295 | $returnValue = $this->service->unhideLocation($location); |
||
| 296 | $this->signalDispatcher->emit( |
||
| 297 | new UnhideLocationSignal( |
||
| 298 | array( |
||
| 299 | 'locationId' => $location->id, |
||
| 300 | 'contentId' => $location->contentId, |
||
| 301 | 'currentVersionNo' => $returnValue->getContentInfo()->currentVersionNo, |
||
| 302 | 'parentLocationId' => $returnValue->parentLocationId, |
||
| 303 | ) |
||
| 304 | ) |
||
| 305 | ); |
||
| 306 | |||
| 307 | return $returnValue; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Moves the subtree to $newParentLocation. |
||
| 312 | * |
||
| 313 | * If a user has the permission to move the location to a target location |
||
| 314 | * he can do it regardless of an existing descendant on which the user has no permission. |
||
| 315 | * |
||
| 316 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
||
| 317 | * |
||
| 318 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 319 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 320 | */ |
||
| 321 | public function moveSubtree(Location $location, Location $newParentLocation) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Deletes $location and all its descendants. |
||
| 339 | * |
||
| 340 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
||
| 341 | * |
||
| 342 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 343 | */ |
||
| 344 | public function deleteLocation(Location $location) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Instantiates a new location create class. |
||
| 360 | * |
||
| 361 | * @param mixed $parentLocationId the parent under which the new location should be created |
||
| 362 | * |
||
| 363 | * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
||
| 364 | */ |
||
| 365 | public function newLocationCreateStruct($parentLocationId) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Instantiates a new location update class. |
||
| 372 | * |
||
| 373 | * @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
||
| 374 | */ |
||
| 375 | public function newLocationUpdateStruct() |
||
| 379 | } |
||
| 380 |