Completed
Push — missing_siteaccess_aware_tests ( c030ea...f3bf75 )
by
unknown
29:08 queued 08:31
created

ObjectStateService::buildCreateInputStruct()   D

Complexity

Conditions 18
Paths 20

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
nc 20
nop 4
dl 0
loc 52
rs 4.8666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the eZ\Publish\API\Repository\ObjectStateService class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository;
10
11
use eZ\Publish\API\Repository\ObjectStateService as ObjectStateServiceInterface;
12
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
13
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler;
14
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
15
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct;
16
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
17
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct;
18
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
19
use eZ\Publish\API\Repository\Values\ObjectState\ObjectState as APIObjectState;
20
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup as APIObjectStateGroup;
21
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectState;
22
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectStateGroup;
23
use eZ\Publish\SPI\Persistence\Content\ObjectState as SPIObjectState;
24
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as SPIObjectStateGroup;
25
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct;
26
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
27
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
28
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
29
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
30
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
31
use Exception;
32
33
/**
34
 * ObjectStateService service.
35
 *
36
 * @example Examples/objectstates.php tbd.
37
 */
38
class ObjectStateService implements ObjectStateServiceInterface
39
{
40
    /** @var \eZ\Publish\API\Repository\Repository */
41
    protected $repository;
42
43
    /** @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler */
44
    protected $objectStateHandler;
45
46
    /** @var array */
47
    protected $settings;
48
49
    /**
50
     * Setups service with reference to repository object that created it & corresponding handler.
51
     *
52
     * @param \eZ\Publish\API\Repository\Repository $repository
53
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler
54
     * @param array $settings
55
     */
56
    public function __construct(RepositoryInterface $repository, Handler $objectStateHandler, array $settings = [])
57
    {
58
        $this->repository = $repository;
59
        $this->objectStateHandler = $objectStateHandler;
60
        // Union makes sure default settings are ignored if provided in argument
61
        $this->settings = $settings + [
62
            //'defaultSetting' => array(),
63
        ];
64
    }
65
66
    /**
67
     * Creates a new object state group.
68
     *
69
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group
70
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
71
     *
72
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct
73
     *
74
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
75
     */
76 View Code Duplication
    public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct)
77
    {
78
        if (!$this->repository->canUser('state', 'administrate', $objectStateGroupCreateStruct)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
            throw new UnauthorizedException('state', 'administrate');
80
        }
81
82
        $inputStruct = $this->buildCreateInputStruct(
83
            $objectStateGroupCreateStruct->identifier,
84
            $objectStateGroupCreateStruct->defaultLanguageCode,
85
            $objectStateGroupCreateStruct->names,
86
            $objectStateGroupCreateStruct->descriptions
87
        );
88
89
        try {
90
            $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier);
91
            throw new InvalidArgumentException(
92
                'objectStateGroupCreateStruct',
93
                'Object state group with provided identifier already exists'
94
            );
95
        } catch (APINotFoundException $e) {
96
            // Do nothing
97
        }
98
99
        $this->repository->beginTransaction();
100
        try {
101
            $spiObjectStateGroup = $this->objectStateHandler->createGroup($inputStruct);
102
            $this->repository->commit();
103
        } catch (Exception $e) {
104
            $this->repository->rollback();
105
            throw $e;
106
        }
107
108
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function loadObjectStateGroup($objectStateGroupId, array $prioritizedLanguages = [])
115
    {
116
        $spiObjectStateGroup = $this->objectStateHandler->loadGroup($objectStateGroupId);
117
118
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup, $prioritizedLanguages);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function loadObjectStateGroups($offset = 0, $limit = -1, array $prioritizedLanguages = [])
125
    {
126
        $spiObjectStateGroups = $this->objectStateHandler->loadAllGroups($offset, $limit);
127
128
        $objectStateGroups = [];
129
        foreach ($spiObjectStateGroups as $spiObjectStateGroup) {
130
            $objectStateGroups[] = $this->buildDomainObjectStateGroupObject(
131
                $spiObjectStateGroup,
132
                $prioritizedLanguages
133
            );
134
        }
135
136
        return $objectStateGroups;
137
    }
138
139
    /**
140
     * This method returns the ordered list of object states of a group.
141
     *
142
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
143
     * @param string[] $prioritizedLanguages
144
     *
145
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[]
146
     */
147
    public function loadObjectStates(
148
        APIObjectStateGroup $objectStateGroup,
149
        array $prioritizedLanguages = []
150
    ) {
151
        $spiObjectStates = $this->objectStateHandler->loadObjectStates($objectStateGroup->id);
152
153
        $objectStates = [];
154
        foreach ($spiObjectStates as $spiObjectState) {
155
            $objectStates[] = $this->buildDomainObjectStateObject(
156
                $spiObjectState,
157
                $objectStateGroup,
158
                $prioritizedLanguages
159
            );
160
        }
161
162
        return $objectStates;
163
    }
164
165
    /**
166
     * Updates an object state group.
167
     *
168
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group
169
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
170
     *
171
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
172
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct
173
     *
174
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
175
     */
176 View Code Duplication
    public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct)
177
    {
178
        if (!$this->repository->canUser('state', 'administrate', $objectStateGroup)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
179
            throw new UnauthorizedException('state', 'administrate');
180
        }
181
182
        $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id);
183
184
        $inputStruct = $this->buildObjectStateGroupUpdateInputStruct(
185
            $loadedObjectStateGroup,
186
            $objectStateGroupUpdateStruct->identifier,
187
            $objectStateGroupUpdateStruct->defaultLanguageCode,
188
            $objectStateGroupUpdateStruct->names,
189
            $objectStateGroupUpdateStruct->descriptions
190
        );
191
192
        if ($objectStateGroupUpdateStruct->identifier !== null) {
193
            try {
194
                $existingObjectStateGroup = $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier);
195
                if ($existingObjectStateGroup->id != $loadedObjectStateGroup->id) {
196
                    throw new InvalidArgumentException(
197
                        'objectStateGroupUpdateStruct',
198
                        'Object state group with provided identifier already exists'
199
                    );
200
                }
201
            } catch (APINotFoundException $e) {
202
                // Do nothing
203
            }
204
        }
205
206
        $this->repository->beginTransaction();
207
        try {
208
            $spiObjectStateGroup = $this->objectStateHandler->updateGroup(
209
                $loadedObjectStateGroup->id,
210
                $inputStruct
211
            );
212
            $this->repository->commit();
213
        } catch (Exception $e) {
214
            $this->repository->rollback();
215
            throw $e;
216
        }
217
218
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup);
219
    }
220
221
    /**
222
     * Deletes a object state group including all states and links to content.
223
     *
224
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group
225
     *
226
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
227
     */
228 View Code Duplication
    public function deleteObjectStateGroup(APIObjectStateGroup $objectStateGroup)
229
    {
230
        if (!$this->repository->canUser('state', 'administrate', $objectStateGroup)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
231
            throw new UnauthorizedException('state', 'administrate');
232
        }
233
234
        $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id);
235
236
        $this->repository->beginTransaction();
237
        try {
238
            $this->objectStateHandler->deleteGroup($loadedObjectStateGroup->id);
239
            $this->repository->commit();
240
        } catch (Exception $e) {
241
            $this->repository->rollback();
242
            throw $e;
243
        }
244
    }
245
246
    /**
247
     * Creates a new object state in the given group.
248
     *
249
     * Note: in current kernel: If it is the first state all content objects will
250
     * set to this state.
251
     *
252
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state
253
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
254
     *
255
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
256
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct
257
     *
258
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
259
     */
260
    public function createObjectState(APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct)
261
    {
262
        if (!$this->repository->canUser('state', 'administrate', $objectStateCreateStruct, [$objectStateGroup])) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
263
            throw new UnauthorizedException('state', 'administrate');
264
        }
265
266
        $inputStruct = $this->buildCreateInputStruct(
267
            $objectStateCreateStruct->identifier,
268
            $objectStateCreateStruct->defaultLanguageCode,
269
            $objectStateCreateStruct->names,
270
            $objectStateCreateStruct->descriptions
271
        );
272
273
        try {
274
            $this->objectStateHandler->loadByIdentifier($inputStruct->identifier, $objectStateGroup->id);
275
            throw new InvalidArgumentException(
276
                'objectStateCreateStruct',
277
                'Object state with provided identifier already exists in provided object state group'
278
            );
279
        } catch (APINotFoundException $e) {
280
            // Do nothing
281
        }
282
283
        $this->repository->beginTransaction();
284
        try {
285
            $spiObjectState = $this->objectStateHandler->create($objectStateGroup->id, $inputStruct);
286
287
            if (is_int($objectStateCreateStruct->priority)) {
288
                $this->objectStateHandler->setPriority(
289
                    $spiObjectState->id,
290
                    $objectStateCreateStruct->priority
291
                );
292
293
                // Reload the object state to have the updated priority,
294
                // considering that priorities are always incremental within a group
295
                $spiObjectState = $this->objectStateHandler->load($spiObjectState->id);
296
            }
297
298
            $this->repository->commit();
299
        } catch (Exception $e) {
300
            $this->repository->rollback();
301
            throw $e;
302
        }
303
304
        return $this->buildDomainObjectStateObject($spiObjectState);
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310
    public function loadObjectState($stateId, array $prioritizedLanguages = [])
311
    {
312
        $spiObjectState = $this->objectStateHandler->load($stateId);
313
314
        return $this->buildDomainObjectStateObject($spiObjectState, null, $prioritizedLanguages);
315
    }
316
317
    /**
318
     * Updates an object state.
319
     *
320
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state
321
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
322
     *
323
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
324
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct
325
     *
326
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
327
     */
328 View Code Duplication
    public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct)
329
    {
330
        if (!$this->repository->canUser('state', 'administrate', $objectState)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
331
            throw new UnauthorizedException('state', 'administrate');
332
        }
333
334
        $loadedObjectState = $this->loadObjectState($objectState->id);
335
336
        $inputStruct = $this->buildObjectStateUpdateInputStruct(
337
            $loadedObjectState,
338
            $objectStateUpdateStruct->identifier,
339
            $objectStateUpdateStruct->defaultLanguageCode,
340
            $objectStateUpdateStruct->names,
341
            $objectStateUpdateStruct->descriptions
342
        );
343
344
        if ($objectStateUpdateStruct->identifier !== null) {
345
            try {
346
                $existingObjectState = $this->objectStateHandler->loadByIdentifier(
347
                    $inputStruct->identifier,
348
                    $loadedObjectState->getObjectStateGroup()->id
349
                );
350
351
                if ($existingObjectState->id != $loadedObjectState->id) {
352
                    throw new InvalidArgumentException(
353
                        'objectStateUpdateStruct',
354
                        'Object state with provided identifier already exists in provided object state group'
355
                    );
356
                }
357
            } catch (APINotFoundException $e) {
358
                // Do nothing
359
            }
360
        }
361
362
        $this->repository->beginTransaction();
363
        try {
364
            $spiObjectState = $this->objectStateHandler->update(
365
                $loadedObjectState->id,
366
                $inputStruct
367
            );
368
            $this->repository->commit();
369
        } catch (Exception $e) {
370
            $this->repository->rollback();
371
            throw $e;
372
        }
373
374
        return $this->buildDomainObjectStateObject($spiObjectState);
375
    }
376
377
    /**
378
     * Changes the priority of the state.
379
     *
380
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state
381
     *
382
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
383
     * @param int $priority
384
     */
385 View Code Duplication
    public function setPriorityOfObjectState(APIObjectState $objectState, $priority)
386
    {
387
        if (!is_int($priority)) {
388
            throw new InvalidArgumentValue('priority', $priority);
389
        }
390
391
        if (!$this->repository->canUser('state', 'administrate', $objectState)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
392
            throw new UnauthorizedException('state', 'administrate');
393
        }
394
395
        $loadedObjectState = $this->loadObjectState($objectState->id);
396
397
        $this->repository->beginTransaction();
398
        try {
399
            $this->objectStateHandler->setPriority(
400
                $loadedObjectState->id,
401
                $priority
402
            );
403
            $this->repository->commit();
404
        } catch (Exception $e) {
405
            $this->repository->rollback();
406
            throw $e;
407
        }
408
    }
409
410
    /**
411
     * Deletes a object state. The state of the content objects is reset to the
412
     * first object state in the group.
413
     *
414
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state
415
     *
416
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
417
     */
418 View Code Duplication
    public function deleteObjectState(APIObjectState $objectState)
419
    {
420
        if (!$this->repository->canUser('state', 'administrate', $objectState)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
421
            throw new UnauthorizedException('state', 'administrate');
422
        }
423
424
        $loadedObjectState = $this->loadObjectState($objectState->id);
425
426
        $this->repository->beginTransaction();
427
        try {
428
            $this->objectStateHandler->delete($loadedObjectState->id);
429
            $this->repository->commit();
430
        } catch (Exception $e) {
431
            $this->repository->rollback();
432
            throw $e;
433
        }
434
    }
435
436
    /**
437
     * Sets the object-state of a state group to $state for the given content.
438
     *
439
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group
440
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state
441
     *
442
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
443
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
444
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
445
     */
446
    public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState)
447
    {
448
        if ($this->repository->canUser('state', 'assign', $contentInfo, $objectState) !== true) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
449
            throw new UnauthorizedException('state', 'assign', ['contentId' => $contentInfo->id]);
450
        }
451
452
        $loadedObjectState = $this->loadObjectState($objectState->id);
453
454
        if ($loadedObjectState->getObjectStateGroup()->id != $objectStateGroup->id) {
455
            throw new InvalidArgumentException('objectState', 'Object state does not belong to the given group');
456
        }
457
458
        $this->repository->beginTransaction();
459
        try {
460
            $this->objectStateHandler->setContentState(
461
                $contentInfo->id,
462
                $objectStateGroup->id,
463
                $loadedObjectState->id
464
            );
465
            $this->repository->commit();
466
        } catch (Exception $e) {
467
            $this->repository->rollback();
468
            throw $e;
469
        }
470
    }
471
472
    /**
473
     * Gets the object-state of object identified by $contentId.
474
     *
475
     * The $state is the id of the state within one group.
476
     *
477
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
478
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
479
     *
480
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
481
     */
482
    public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup)
483
    {
484
        $spiObjectState = $this->objectStateHandler->getContentState(
485
            $contentInfo->id,
486
            $objectStateGroup->id
487
        );
488
489
        return $this->buildDomainObjectStateObject($spiObjectState, $objectStateGroup);
490
    }
491
492
    /**
493
     * Returns the number of objects which are in this state.
494
     *
495
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
496
     *
497
     * @return int
498
     */
499
    public function getContentCount(APIObjectState $objectState)
500
    {
501
        return $this->objectStateHandler->getContentCount(
502
            $objectState->id
503
        );
504
    }
505
506
    /**
507
     * Instantiates a new Object State Group Create Struct and sets $identified in it.
508
     *
509
     * @param string $identifier
510
     *
511
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct
512
     */
513
    public function newObjectStateGroupCreateStruct($identifier)
514
    {
515
        $objectStateGroupCreateStruct = new ObjectStateGroupCreateStruct();
516
        $objectStateGroupCreateStruct->identifier = $identifier;
517
518
        return $objectStateGroupCreateStruct;
519
    }
520
521
    /**
522
     * Instantiates a new Object State Group Update Struct.
523
     *
524
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct
525
     */
526
    public function newObjectStateGroupUpdateStruct()
527
    {
528
        return new ObjectStateGroupUpdateStruct();
529
    }
530
531
    /**
532
     * Instantiates a new Object State Create Struct and sets $identifier in it.
533
     *
534
     * @param string $identifier
535
     *
536
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct
537
     */
538
    public function newObjectStateCreateStruct($identifier)
539
    {
540
        $objectStateCreateStruct = new ObjectStateCreateStruct();
541
        $objectStateCreateStruct->identifier = $identifier;
542
543
        return $objectStateCreateStruct;
544
    }
545
546
    /**
547
     * Instantiates a new Object State Update Struct.
548
     *
549
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct
550
     */
551
    public function newObjectStateUpdateStruct()
552
    {
553
        return new ObjectStateUpdateStruct();
554
    }
555
556
    /**
557
     * Converts the object state SPI value object to API value object.
558
     *
559
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState
560
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
561
     * @param string[] $prioritizedLanguages
562
     *
563
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
564
     */
565
    protected function buildDomainObjectStateObject(
566
        SPIObjectState $spiObjectState,
567
        APIObjectStateGroup $objectStateGroup = null,
568
        array $prioritizedLanguages = []
569
    ) {
570
        $objectStateGroup = $objectStateGroup ?: $this->loadObjectStateGroup($spiObjectState->groupId);
571
572
        return new ObjectState(
573
            [
574
                'id' => $spiObjectState->id,
575
                'identifier' => $spiObjectState->identifier,
576
                'priority' => $spiObjectState->priority,
577
                'mainLanguageCode' => $spiObjectState->defaultLanguage,
578
                'languageCodes' => $spiObjectState->languageCodes,
579
                'names' => $spiObjectState->name,
580
                'descriptions' => $spiObjectState->description,
581
                'objectStateGroup' => $objectStateGroup,
582
                'prioritizedLanguages' => $prioritizedLanguages,
583
            ]
584
        );
585
    }
586
587
    /**
588
     * Converts the object state group SPI value object to API value object.
589
     *
590
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup
591
     * @param array $prioritizedLanguages
592
     *
593
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
594
     */
595
    protected function buildDomainObjectStateGroupObject(
596
        SPIObjectStateGroup $spiObjectStateGroup,
597
        array $prioritizedLanguages = []
598
    ) {
599
        return new ObjectStateGroup(
600
            [
601
                'id' => $spiObjectStateGroup->id,
602
                'identifier' => $spiObjectStateGroup->identifier,
603
                'mainLanguageCode' => $spiObjectStateGroup->defaultLanguage,
604
                'languageCodes' => $spiObjectStateGroup->languageCodes,
605
                'names' => $spiObjectStateGroup->name,
606
                'descriptions' => $spiObjectStateGroup->description,
607
                'prioritizedLanguages' => $prioritizedLanguages,
608
            ]
609
        );
610
    }
611
612
    /**
613
     * Validates input for creating object states/groups and builds the InputStruct object.
614
     *
615
     * @param string $identifier
616
     * @param string $defaultLanguageCode
617
     * @param string[] $names
618
     * @param string[] $descriptions
619
     *
620
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
621
     */
622
    protected function buildCreateInputStruct($identifier, $defaultLanguageCode, $names, $descriptions)
623
    {
624
        if (!is_string($identifier) || empty($identifier)) {
625
            throw new InvalidArgumentValue('identifier', $identifier);
626
        }
627
628
        if (!is_string($defaultLanguageCode) || empty($defaultLanguageCode)) {
629
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
630
        }
631
632
        if (!is_array($names) || empty($names)) {
633
            throw new InvalidArgumentValue('names', $names);
634
        }
635
636
        if (!isset($names[$defaultLanguageCode])) {
637
            throw new InvalidArgumentValue('names', $names);
638
        }
639
640
        foreach ($names as $languageCode => $name) {
641
            try {
642
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
643
            } catch (NotFoundException $e) {
644
                throw new InvalidArgumentValue('names', $names);
645
            }
646
647
            if (!is_string($name) || empty($name)) {
648
                throw new InvalidArgumentValue('names', $names);
649
            }
650
        }
651
652
        if ($descriptions !== null && !is_array($descriptions)) {
653
            throw new InvalidArgumentValue('descriptions', $descriptions);
654
        }
655
656
        $descriptions = $descriptions !== null ? $descriptions : [];
657
658
        $inputStruct = new InputStruct();
659
        $inputStruct->identifier = $identifier;
660
        $inputStruct->defaultLanguage = $defaultLanguageCode;
661
        $inputStruct->name = $names;
662
663
        $inputStruct->description = [];
664
        foreach ($names as $languageCode => $name) {
665
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
666
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
667
            } else {
668
                $inputStruct->description[$languageCode] = '';
669
            }
670
        }
671
672
        return $inputStruct;
673
    }
674
675
    /**
676
     * Validates input for updating object states and builds the InputStruct object.
677
     *
678
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
679
     * @param string $identifier
680
     * @param string $defaultLanguageCode
681
     * @param string[] $names
682
     * @param string[] $descriptions
683
     *
684
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
685
     */
686 View Code Duplication
    protected function buildObjectStateUpdateInputStruct(APIObjectState $objectState, $identifier, $defaultLanguageCode, $names, $descriptions)
687
    {
688
        $inputStruct = new InputStruct();
689
690
        if ($identifier !== null && (!is_string($identifier) || empty($identifier))) {
691
            throw new InvalidArgumentValue('identifier', $identifier);
692
        }
693
694
        $inputStruct->identifier = $identifier !== null ? $identifier : $objectState->identifier;
695
696
        if ($defaultLanguageCode !== null && (!is_string($defaultLanguageCode) || empty($defaultLanguageCode))) {
697
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
698
        }
699
700
        $inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectState->defaultLanguageCode;
0 ignored issues
show
Documentation introduced by
The property defaultLanguageCode does not exist on object<eZ\Publish\API\Re...bjectState\ObjectState>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
701
702
        if ($names !== null && (!is_array($names) || empty($names))) {
703
            throw new InvalidArgumentValue('names', $names);
704
        }
705
706
        $inputStruct->name = $names !== null ? $names : $objectState->getNames();
707
708
        if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
709
            throw new InvalidArgumentValue('names', $inputStruct->name);
710
        }
711
712
        foreach ($inputStruct->name as $languageCode => $name) {
713
            try {
714
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
715
            } catch (NotFoundException $e) {
716
                throw new InvalidArgumentValue('names', $inputStruct->name);
717
            }
718
719
            if (!is_string($name) || empty($name)) {
720
                throw new InvalidArgumentValue('names', $inputStruct->name);
721
            }
722
        }
723
724
        if ($descriptions !== null && !is_array($descriptions)) {
725
            throw new InvalidArgumentValue('descriptions', $descriptions);
726
        }
727
728
        $descriptions = $descriptions !== null ? $descriptions : $objectState->getDescriptions();
729
        $descriptions = $descriptions !== null ? $descriptions : [];
730
731
        $inputStruct->description = [];
732
        foreach ($inputStruct->name as $languageCode => $name) {
733
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
734
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
735
            } else {
736
                $inputStruct->description[$languageCode] = '';
737
            }
738
        }
739
740
        return $inputStruct;
741
    }
742
743
    /**
744
     * Validates input for updating object state groups and builds the InputStruct object.
745
     *
746
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
747
     * @param string $identifier
748
     * @param string $defaultLanguageCode
749
     * @param string[] $names
750
     * @param string[] $descriptions
751
     *
752
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
753
     */
754 View Code Duplication
    protected function buildObjectStateGroupUpdateInputStruct(APIObjectStateGroup $objectStateGroup, $identifier, $defaultLanguageCode, $names, $descriptions)
755
    {
756
        $inputStruct = new InputStruct();
757
758
        if ($identifier !== null && (!is_string($identifier) || empty($identifier))) {
759
            throw new InvalidArgumentValue('identifier', $identifier);
760
        }
761
762
        $inputStruct->identifier = $identifier !== null ? $identifier : $objectStateGroup->identifier;
763
764
        if ($defaultLanguageCode !== null && (!is_string($defaultLanguageCode) || empty($defaultLanguageCode))) {
765
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
766
        }
767
768
        $inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectStateGroup->defaultLanguageCode;
769
770
        if ($names !== null && (!is_array($names) || empty($names))) {
771
            throw new InvalidArgumentValue('names', $names);
772
        }
773
774
        $inputStruct->name = $names !== null ? $names : $objectStateGroup->getNames();
775
776
        if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
777
            throw new InvalidArgumentValue('names', $inputStruct->name);
778
        }
779
780
        foreach ($inputStruct->name as $languageCode => $name) {
781
            try {
782
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
783
            } catch (NotFoundException $e) {
784
                throw new InvalidArgumentValue('names', $inputStruct->name);
785
            }
786
787
            if (!is_string($name) || empty($name)) {
788
                throw new InvalidArgumentValue('names', $inputStruct->name);
789
            }
790
        }
791
792
        if ($descriptions !== null && !is_array($descriptions)) {
793
            throw new InvalidArgumentValue('descriptions', $descriptions);
794
        }
795
796
        $descriptions = $descriptions !== null ? $descriptions : $objectStateGroup->getDescriptions();
797
        $descriptions = $descriptions !== null ? $descriptions : [];
798
799
        $inputStruct->description = [];
800
        foreach ($inputStruct->name as $languageCode => $name) {
801
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
802
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
803
            } else {
804
                $inputStruct->description[$languageCode] = '';
805
            }
806
        }
807
808
        return $inputStruct;
809
    }
810
}
811