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 |
||
28 | class LocationService implements APILocationService, Sessionable |
||
29 | { |
||
30 | /** |
||
31 | * @var \eZ\Publish\Core\REST\Client\HttpClient |
||
32 | */ |
||
33 | private $client; |
||
34 | |||
35 | /** |
||
36 | * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher |
||
37 | */ |
||
38 | private $inputDispatcher; |
||
39 | |||
40 | /** |
||
41 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor |
||
42 | */ |
||
43 | private $outputVisitor; |
||
44 | |||
45 | /** |
||
46 | * @var \eZ\Publish\Core\REST\Common\RequestParser |
||
47 | */ |
||
48 | private $requestParser; |
||
49 | |||
50 | /** |
||
51 | * @param \eZ\Publish\Core\REST\Client\HttpClient $client |
||
52 | * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher |
||
53 | * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor |
||
54 | * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser |
||
55 | */ |
||
56 | View Code Duplication | public function __construct(HttpClient $client, Dispatcher $inputDispatcher, Visitor $outputVisitor, RequestParser $requestParser) |
|
63 | |||
64 | /** |
||
65 | * Set session ID. |
||
66 | * |
||
67 | * Only for testing |
||
68 | * |
||
69 | * @param mixed $id |
||
70 | * |
||
71 | * @private |
||
72 | */ |
||
73 | public function setSession($id) |
||
79 | |||
80 | /** |
||
81 | * Instantiates a new location create class. |
||
82 | * |
||
83 | * @param mixed $parentLocationId the parent under which the new location should be created |
||
84 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType |
||
85 | * |
||
86 | * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
||
87 | */ |
||
88 | View Code Duplication | public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null) |
|
100 | |||
101 | /** |
||
102 | * Creates the new $location in the content repository for the given content. |
||
103 | * |
||
104 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
||
105 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
||
106 | * or the parent is a sub location of the location the content |
||
107 | * or if set the remoteId exists already |
||
108 | * |
||
109 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
110 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
111 | * |
||
112 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
||
113 | */ |
||
114 | public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
||
128 | |||
129 | /** |
||
130 | * Loads a location object from its $locationId. |
||
131 | * |
||
132 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
||
133 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
||
134 | * |
||
135 | * @param mixed $locationId |
||
136 | * @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object. |
||
137 | * |
||
138 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
139 | */ |
||
140 | View Code Duplication | public function loadLocation($locationId, array $prioritizedLanguages = null) |
|
152 | |||
153 | /** |
||
154 | * Loads a location object from its $remoteId. |
||
155 | * |
||
156 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
||
157 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
||
158 | * |
||
159 | * @param string $remoteId |
||
160 | * @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object. |
||
161 | * |
||
162 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
163 | */ |
||
164 | public function loadLocationByRemoteId($remoteId, array $prioritizedLanguages = null) |
||
176 | |||
177 | /** |
||
178 | * Instantiates a new location update class. |
||
179 | * |
||
180 | * @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
||
181 | */ |
||
182 | public function newLocationUpdateStruct() |
||
186 | |||
187 | /** |
||
188 | * Updates $location in the content repository. |
||
189 | * |
||
190 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
||
191 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
||
192 | * |
||
193 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
194 | * @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
||
195 | * |
||
196 | * @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
||
197 | */ |
||
198 | public function updateLocation(Location $location, LocationUpdateStruct $locationUpdateStruct) |
||
212 | |||
213 | /** |
||
214 | * Loads the locations for the given content object. |
||
215 | * |
||
216 | * If a $rootLocation is given, only locations that belong to this location are returned. |
||
217 | * The location list is also filtered by permissions on reading locations. |
||
218 | * |
||
219 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if there is no published version yet |
||
220 | * |
||
221 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
222 | * @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation |
||
223 | * @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object. |
||
224 | * |
||
225 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] |
||
226 | */ |
||
227 | View Code Duplication | public function loadLocations(ContentInfo $contentInfo, Location $rootLocation = null, array $prioritizedLanguages = null) |
|
240 | |||
241 | /** |
||
242 | * Loads children which are readable by the current user of a location object sorted by sortField and sortOrder. |
||
243 | * |
||
244 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
245 | * @param int $offset the start offset for paging |
||
246 | * @param int $limit the number of locations returned |
||
247 | * @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object. |
||
248 | * |
||
249 | * @return \eZ\Publish\API\Repository\Values\Content\LocationList |
||
250 | */ |
||
251 | View Code Duplication | public function loadLocationChildren(Location $location, $offset = 0, $limit = 25, array $prioritizedLanguages = null) |
|
264 | |||
265 | /** |
||
266 | * Load parent Locations for Content Draft. |
||
267 | * |
||
268 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
269 | * @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object. |
||
270 | * |
||
271 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] List of parent Locations |
||
272 | */ |
||
273 | public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, array $prioritizedLanguages = null) |
||
277 | |||
278 | /** |
||
279 | * Returns the number of children which are readable by the current user of a location object. |
||
280 | * |
||
281 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
282 | * |
||
283 | * @return int |
||
284 | */ |
||
285 | public function getLocationChildCount(Location $location) |
||
289 | |||
290 | /** |
||
291 | * Swaps the contents hold by the $location1 and $location2. |
||
292 | * |
||
293 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
||
294 | * |
||
295 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
||
296 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
||
297 | */ |
||
298 | public function swapLocation(Location $location1, Location $location2) |
||
302 | |||
303 | /** |
||
304 | * Hides the $location and marks invisible all descendants of $location. |
||
305 | * |
||
306 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
||
307 | * |
||
308 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
309 | * |
||
310 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
311 | */ |
||
312 | public function hideLocation(Location $location) |
||
316 | |||
317 | /** |
||
318 | * Unhides the $location. |
||
319 | * |
||
320 | * This method and marks visible all descendants of $locations |
||
321 | * until a hidden location is found. |
||
322 | * |
||
323 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
||
324 | * |
||
325 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
326 | * |
||
327 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
328 | */ |
||
329 | public function unhideLocation(Location $location) |
||
333 | |||
334 | /** |
||
335 | * Deletes $location and all its descendants. |
||
336 | * |
||
337 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
||
338 | * |
||
339 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
340 | */ |
||
341 | public function deleteLocation(Location $location) |
||
345 | |||
346 | /** |
||
347 | * Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
||
348 | * |
||
349 | * Only the items on which the user has read access are copied. |
||
350 | * |
||
351 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
||
352 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
||
353 | * |
||
354 | * @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
||
355 | * @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
||
356 | * |
||
357 | * @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
||
358 | * |
||
359 | * @todo enhancement - this method should return a result structure containing the new location and a list |
||
360 | * of locations which are not copied due to permission denials. |
||
361 | */ |
||
362 | public function copySubtree(Location $subtree, Location $targetParentLocation) |
||
366 | |||
367 | /** |
||
368 | * Moves the subtree to $newParentLocation. |
||
369 | * |
||
370 | * If a user has the permission to move the location to a target location |
||
371 | * he can do it regardless of an existing descendant on which the user has no permission. |
||
372 | * |
||
373 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
||
374 | * |
||
375 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
376 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
377 | */ |
||
378 | public function moveSubtree(Location $location, Location $newParentLocation) |
||
382 | |||
383 | public function getAllLocationsCount(): int |
||
387 | |||
388 | /** |
||
389 | * @param int $limit |
||
390 | * @param int $offset |
||
391 | * |
||
392 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] |
||
393 | * |
||
394 | * @throws \Exception |
||
395 | */ |
||
396 | public function loadAllLocations(int $offset = 0, int $limit = 25): array |
||
400 | } |
||
401 |