Completed
Push — EZP-31584 ( d4db81 )
by
unknown
18:34
created

LocationService::loadFirstAvailableLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
nc 1
nop 2
rs 10
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\SignalSlot;
8
9
use eZ\Publish\API\Repository\LocationService as LocationServiceInterface;
10
use eZ\Publish\API\Repository\Values\Content\Location;
11
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
13
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
14
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
15
use eZ\Publish\Core\SignalSlot\Signal\LocationService\CopySubtreeSignal;
16
use eZ\Publish\Core\SignalSlot\Signal\LocationService\CreateLocationSignal;
17
use eZ\Publish\Core\SignalSlot\Signal\LocationService\UpdateLocationSignal;
18
use eZ\Publish\Core\SignalSlot\Signal\LocationService\SwapLocationSignal;
19
use eZ\Publish\Core\SignalSlot\Signal\LocationService\HideLocationSignal;
20
use eZ\Publish\Core\SignalSlot\Signal\LocationService\UnhideLocationSignal;
21
use eZ\Publish\Core\SignalSlot\Signal\LocationService\MoveSubtreeSignal;
22
use eZ\Publish\Core\SignalSlot\Signal\LocationService\DeleteLocationSignal;
23
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
24
25
/**
26
 * LocationService class.
27
 */
28
class LocationService implements LocationServiceInterface
29
{
30
    /**
31
     * Aggregated service.
32
     *
33
     * @var \eZ\Publish\API\Repository\LocationService
34
     */
35
    protected $service;
36
37
    /**
38
     * SignalDispatcher.
39
     *
40
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
41
     */
42
    protected $signalDispatcher;
43
44
    /**
45
     * Constructor.
46
     *
47
     * Construct service object from aggregated service and signal
48
     * dispatcher
49
     *
50
     * @param \eZ\Publish\API\Repository\LocationService $service
51
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
52
     */
53
    public function __construct(LocationServiceInterface $service, SignalDispatcher $signalDispatcher)
54
    {
55
        $this->service = $service;
56
        $this->signalDispatcher = $signalDispatcher;
57
    }
58
59
    /**
60
     * Copies the subtree starting from $subtree as a new subtree of $targetLocation.
61
     *
62
     * Only the items on which the user has read access are copied.
63
     *
64
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location
65
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree
66
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location
67
     *
68
     * @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy
69
     * @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation
70
     *
71
     * @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree
72
     */
73
    public function copySubtree(Location $subtree, Location $targetParentLocation)
74
    {
75
        $returnValue = $this->service->copySubtree($subtree, $targetParentLocation);
76
        $this->signalDispatcher->emit(
77
            new CopySubtreeSignal(
78
                [
79
                    'subtreeId' => $subtree->id,
80
                    'targetParentLocationId' => $targetParentLocation->id,
81
                    'targetNewSubtreeId' => $returnValue->id,
82
                ]
83
            )
84
        );
85
86
        return $returnValue;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function loadLocation($locationId, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null)
93
    {
94
        return $this->service->loadLocation($locationId, $prioritizedLanguages, $useAlwaysAvailable);
0 ignored issues
show
Bug introduced by
It seems like $prioritizedLanguages defined by parameter $prioritizedLanguages on line 92 can also be of type array; however, eZ\Publish\API\Repositor...Service::loadLocation() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function loadLocationList(array $locationIds, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null): iterable
101
    {
102
        return $this->service->loadLocationList($locationIds, $prioritizedLanguages, $useAlwaysAvailable);
0 ignored issues
show
Bug introduced by
It seems like $prioritizedLanguages defined by parameter $prioritizedLanguages on line 100 can also be of type array; however, eZ\Publish\API\Repositor...ice::loadLocationList() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function loadLocationByRemoteId($remoteId, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null)
109
    {
110
        return $this->service->loadLocationByRemoteId($remoteId, $prioritizedLanguages, $useAlwaysAvailable);
0 ignored issues
show
Bug introduced by
It seems like $prioritizedLanguages defined by parameter $prioritizedLanguages on line 108 can also be of type array; however, eZ\Publish\API\Repositor...oadLocationByRemoteId() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function loadLocations(ContentInfo $contentInfo, Location $rootLocation = null, array $prioritizedLanguages = null)
117
    {
118
        return $this->service->loadLocations($contentInfo, $rootLocation, $prioritizedLanguages);
0 ignored issues
show
Bug introduced by
It seems like $prioritizedLanguages defined by parameter $prioritizedLanguages on line 116 can also be of type array; however, eZ\Publish\API\Repositor...ervice::loadLocations() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function loadLocationChildren(Location $location, $offset = 0, $limit = 25, array $prioritizedLanguages = null)
125
    {
126
        return $this->service->loadLocationChildren($location, $offset, $limit, $prioritizedLanguages);
0 ignored issues
show
Bug introduced by
It seems like $prioritizedLanguages defined by parameter $prioritizedLanguages on line 124 can also be of type array; however, eZ\Publish\API\Repositor...:loadLocationChildren() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, array $prioritizedLanguages = null)
133
    {
134
        return $this->service->loadParentLocationsForDraftContent($versionInfo, $prioritizedLanguages);
0 ignored issues
show
Bug introduced by
It seems like $prioritizedLanguages defined by parameter $prioritizedLanguages on line 132 can also be of type array; however, eZ\Publish\API\Repositor...ationsForDraftContent() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
135
    }
136
137
    /**
138
     * Returns the number of children which are readable by the current user of a location object.
139
     *
140
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
141
     *
142
     * @return int
143
     */
144
    public function getLocationChildCount(Location $location)
145
    {
146
        return $this->service->getLocationChildCount($location);
147
    }
148
149
    /**
150
     * Creates the new $location in the content repository for the given content.
151
     *
152
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location
153
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent
154
     *                                        or the parent is a sub location of the location of the content
155
     *                                        or if set the remoteId exists already
156
     *
157
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
158
     * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct
159
     *
160
     * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location
161
     */
162
    public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct)
163
    {
164
        $returnValue = $this->service->createLocation($contentInfo, $locationCreateStruct);
165
        $this->signalDispatcher->emit(
166
            new CreateLocationSignal(
167
                [
168
                    'contentId' => $contentInfo->id,
169
                    'locationId' => $returnValue->id,
170
                    'parentLocationId' => $returnValue->parentLocationId,
171
                ]
172
            )
173
        );
174
175
        return $returnValue;
176
    }
177
178
    /**
179
     * Updates $location in the content repository.
180
     *
181
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location
182
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException   if if set the remoteId exists already
183
     *
184
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
185
     * @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct
186
     *
187
     * @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location
188
     */
189
    public function updateLocation(Location $location, LocationUpdateStruct $locationUpdateStruct)
190
    {
191
        $returnValue = $this->service->updateLocation($location, $locationUpdateStruct);
192
        $this->signalDispatcher->emit(
193
            new UpdateLocationSignal(
194
                [
195
                    'contentId' => $location->contentId,
196
                    'locationId' => $location->id,
197
                    'parentLocationId' => $location->parentLocationId,
198
                ]
199
            )
200
        );
201
202
        return $returnValue;
203
    }
204
205
    /**
206
     * Swaps the contents held by $location1 and $location2.
207
     *
208
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content
209
     *
210
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location1
211
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location2
212
     */
213
    public function swapLocation(Location $location1, Location $location2)
214
    {
215
        $returnValue = $this->service->swapLocation($location1, $location2);
216
        $this->signalDispatcher->emit(
217
            new SwapLocationSignal(
218
                [
219
                    'location1Id' => $location1->id,
220
                    'content1Id' => $location1->contentId,
221
                    'parentLocation1Id' => $location1->parentLocationId,
222
                    'location2Id' => $location2->id,
223
                    'content2Id' => $location2->contentId,
224
                    'parentLocation2Id' => $location2->parentLocationId,
225
                ]
226
            )
227
        );
228
229
        return $returnValue;
230
    }
231
232
    /**
233
     * Hides the $location and marks invisible all descendants of $location.
234
     *
235
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location
236
     *
237
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
238
     *
239
     * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value
240
     */
241 View Code Duplication
    public function hideLocation(Location $location)
242
    {
243
        $returnValue = $this->service->hideLocation($location);
244
        $this->signalDispatcher->emit(
245
            new HideLocationSignal(
246
                [
247
                    'locationId' => $location->id,
248
                    'contentId' => $location->contentId,
249
                    'currentVersionNo' => $returnValue->getContentInfo()->currentVersionNo,
250
                    'parentLocationId' => $returnValue->parentLocationId,
251
                ]
252
            )
253
        );
254
255
        return $returnValue;
256
    }
257
258
    /**
259
     * Unhides the $location.
260
     *
261
     * This method and marks visible all descendants of $locations
262
     * until a hidden location is found.
263
     *
264
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location
265
     *
266
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
267
     *
268
     * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value
269
     */
270 View Code Duplication
    public function unhideLocation(Location $location)
271
    {
272
        $returnValue = $this->service->unhideLocation($location);
273
        $this->signalDispatcher->emit(
274
            new UnhideLocationSignal(
275
                [
276
                    'locationId' => $location->id,
277
                    'contentId' => $location->contentId,
278
                    'currentVersionNo' => $returnValue->getContentInfo()->currentVersionNo,
279
                    'parentLocationId' => $returnValue->parentLocationId,
280
                ]
281
            )
282
        );
283
284
        return $returnValue;
285
    }
286
287
    /**
288
     * Moves the subtree to $newParentLocation.
289
     *
290
     * If a user has the permission to move the location to a target location
291
     * he can do it regardless of an existing descendant on which the user has no permission.
292
     *
293
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target
294
     *
295
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
296
     * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
297
     */
298
    public function moveSubtree(Location $location, Location $newParentLocation)
299
    {
300
        $returnValue = $this->service->moveSubtree($location, $newParentLocation);
301
        $this->signalDispatcher->emit(
302
            new MoveSubtreeSignal(
303
                [
304
                    'locationId' => $location->id,
305
                    'newParentLocationId' => $newParentLocation->id,
306
                    'oldParentLocationId' => $location->parentLocationId,
307
                ]
308
            )
309
        );
310
311
        return $returnValue;
312
    }
313
314
    /**
315
     * Deletes $location and all its descendants.
316
     *
317
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant
318
     *
319
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
320
     */
321 View Code Duplication
    public function deleteLocation(Location $location)
322
    {
323
        $this->service->deleteLocation($location);
324
        $this->signalDispatcher->emit(
325
            new DeleteLocationSignal(
326
                [
327
                    'contentId' => $location->contentId,
328
                    'locationId' => $location->id,
329
                    'parentLocationId' => $location->parentLocationId,
330
                ]
331
            )
332
        );
333
    }
334
335
    /**
336
     * Instantiates a new location create class.
337
     *
338
     * @param mixed $parentLocationId the parent under which the new location should be created
339
     * @param eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType
340
     *
341
     * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
342
     */
343
    public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null)
344
    {
345
        return $this->service->newLocationCreateStruct($parentLocationId, $contentType);
346
    }
347
348
    /**
349
     * Instantiates a new location update class.
350
     *
351
     * @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct
352
     */
353
    public function newLocationUpdateStruct()
354
    {
355
        return $this->service->newLocationUpdateStruct();
356
    }
357
358
    /**
359
     * Get the total number of all existing Locations. Can be combined with loadAllLocations.
360
     *
361
     * @see loadAllLocations
362
     *
363
     * @return int Total number of Locations
364
     */
365
    public function getAllLocationsCount(): int
366
    {
367
        return $this->service->getAllLocationsCount();
368
    }
369
370
    /**
371
     * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results.
372
     *
373
     * @param int $limit
374
     * @param int $offset
375
     *
376
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
377
     */
378
    public function loadAllLocations(int $offset = 0, int $limit = 25): array
379
    {
380
        return $this->service->loadAllLocations($offset, $limit);
381
    }
382
383
    public function loadFirstAvailableLocation(ContentInfo $contentInfo, array $prioritizedLanguages = null): Location
384
    {
385
        return $this->service->loadFirstAvailableLocation($contentInfo, $prioritizedLanguages);
386
    }
387
}
388