buildObjectStateGroupUpdateInputStruct()   F
last analyzed

Complexity

Conditions 20
Paths 223

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
nc 223
nop 5
dl 0
loc 57
rs 3.0458
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
 * @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\Repository;
8
9
use eZ\Publish\API\Repository\ObjectStateService as ObjectStateServiceInterface;
10
use eZ\Publish\API\Repository\PermissionResolver;
11
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
12
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler;
13
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
14
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct;
15
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
16
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct;
17
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
18
use eZ\Publish\API\Repository\Values\ObjectState\ObjectState as APIObjectState;
19
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup as APIObjectStateGroup;
20
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectState;
21
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectStateGroup;
22
use eZ\Publish\SPI\Persistence\Content\ObjectState as SPIObjectState;
23
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as SPIObjectStateGroup;
24
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct;
25
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
26
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
27
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
28
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
29
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
30
use Exception;
31
32
/**
33
 * ObjectStateService service.
34
 *
35
 * @example Examples/objectstates.php tbd.
36
 */
37
class ObjectStateService implements ObjectStateServiceInterface
38
{
39
    /** @var \eZ\Publish\API\Repository\Repository */
40
    protected $repository;
41
42
    /** @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler */
43
    protected $objectStateHandler;
44
45
    /** @var array */
46
    protected $settings;
47
48
    /** @var \eZ\Publish\API\Repository\PermissionResolver */
49
    private $permissionResolver;
50
51
    /**
52
     * Setups service with reference to repository object that created it & corresponding handler.
53
     *
54
     * @param \eZ\Publish\API\Repository\Repository $repository
55
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler
56
     * @param array $settings
57
     */
58
    public function __construct(
59
        RepositoryInterface $repository,
60
        Handler $objectStateHandler,
61
        PermissionResolver $permissionResolver,
62
        array $settings = []
63
    ) {
64
        $this->repository = $repository;
65
        $this->objectStateHandler = $objectStateHandler;
66
        $this->permissionResolver = $permissionResolver;
67
        // Union makes sure default settings are ignored if provided in argument
68
        $this->settings = $settings + [
69
            //'defaultSetting' => array(),
70
        ];
71
    }
72
73
    /**
74
     * Creates a new object state group.
75
     *
76
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group
77
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
78
     *
79
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct
80
     *
81
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
82
     */
83
    public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct): APIObjectStateGroup
84
    {
85
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroupCreateStruct)) {
86
            throw new UnauthorizedException('state', 'administrate');
87
        }
88
89
        $inputStruct = $this->buildCreateInputStruct(
90
            $objectStateGroupCreateStruct->identifier,
91
            $objectStateGroupCreateStruct->defaultLanguageCode,
92
            $objectStateGroupCreateStruct->names,
93
            $objectStateGroupCreateStruct->descriptions
94
        );
95
96
        try {
97
            $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier);
98
            throw new InvalidArgumentException(
99
                'objectStateGroupCreateStruct',
100
                'An Object state group with the provided identifier already exists'
101
            );
102
        } catch (APINotFoundException $e) {
103
            // Do nothing
104
        }
105
106
        $this->repository->beginTransaction();
107
        try {
108
            $spiObjectStateGroup = $this->objectStateHandler->createGroup($inputStruct);
109
            $this->repository->commit();
110
        } catch (Exception $e) {
111
            $this->repository->rollback();
112
            throw $e;
113
        }
114
115
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function loadObjectStateGroup(int $objectStateGroupId, array $prioritizedLanguages = []): APIObjectStateGroup
122
    {
123
        $spiObjectStateGroup = $this->objectStateHandler->loadGroup($objectStateGroupId);
124
125
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup, $prioritizedLanguages);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function loadObjectStateGroups(int $offset = 0, int $limit = -1, array $prioritizedLanguages = []): iterable
132
    {
133
        $spiObjectStateGroups = $this->objectStateHandler->loadAllGroups($offset, $limit);
134
135
        $objectStateGroups = [];
136
        foreach ($spiObjectStateGroups as $spiObjectStateGroup) {
137
            $objectStateGroups[] = $this->buildDomainObjectStateGroupObject(
138
                $spiObjectStateGroup,
139
                $prioritizedLanguages
140
            );
141
        }
142
143
        return $objectStateGroups;
144
    }
145
146
    /**
147
     * This method returns the ordered list of object states of a group.
148
     *
149
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
150
     * @param string[] $prioritizedLanguages
151
     *
152
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[]
153
     */
154
    public function loadObjectStates(
155
        APIObjectStateGroup $objectStateGroup,
156
        array $prioritizedLanguages = []
157
    ): iterable {
158
        $spiObjectStates = $this->objectStateHandler->loadObjectStates($objectStateGroup->id);
159
160
        $objectStates = [];
161
        foreach ($spiObjectStates as $spiObjectState) {
162
            $objectStates[] = $this->buildDomainObjectStateObject(
163
                $spiObjectState,
164
                $objectStateGroup,
165
                $prioritizedLanguages
166
            );
167
        }
168
169
        return $objectStates;
170
    }
171
172
    /**
173
     * Updates an object state group.
174
     *
175
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group
176
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
177
     *
178
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
179
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct
180
     *
181
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
182
     */
183
    public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct): APIObjectStateGroup
184
    {
185
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroup)) {
186
            throw new UnauthorizedException('state', 'administrate');
187
        }
188
189
        $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id);
190
191
        $inputStruct = $this->buildObjectStateGroupUpdateInputStruct(
192
            $loadedObjectStateGroup,
193
            $objectStateGroupUpdateStruct->identifier,
194
            $objectStateGroupUpdateStruct->defaultLanguageCode,
195
            $objectStateGroupUpdateStruct->names,
196
            $objectStateGroupUpdateStruct->descriptions
197
        );
198
199
        if ($objectStateGroupUpdateStruct->identifier !== null) {
200
            try {
201
                $existingObjectStateGroup = $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier);
202
                if ($existingObjectStateGroup->id != $loadedObjectStateGroup->id) {
203
                    throw new InvalidArgumentException(
204
                        'objectStateGroupUpdateStruct',
205
                        'An Object state group with the provided identifier already exists'
206
                    );
207
                }
208
            } catch (APINotFoundException $e) {
209
                // Do nothing
210
            }
211
        }
212
213
        $this->repository->beginTransaction();
214
        try {
215
            $spiObjectStateGroup = $this->objectStateHandler->updateGroup(
216
                $loadedObjectStateGroup->id,
217
                $inputStruct
218
            );
219
            $this->repository->commit();
220
        } catch (Exception $e) {
221
            $this->repository->rollback();
222
            throw $e;
223
        }
224
225
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup, $objectStateGroup->prioritizedLanguages);
0 ignored issues
show
Documentation introduced by
The property prioritizedLanguages does not exist on object<eZ\Publish\API\Re...State\ObjectStateGroup>. 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...
226
    }
227
228
    /**
229
     * Deletes a object state group including all states and links to content.
230
     *
231
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group
232
     *
233
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
234
     */
235
    public function deleteObjectStateGroup(APIObjectStateGroup $objectStateGroup): void
236
    {
237
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroup)) {
238
            throw new UnauthorizedException('state', 'administrate');
239
        }
240
241
        $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id);
242
243
        $this->repository->beginTransaction();
244
        try {
245
            $this->objectStateHandler->deleteGroup($loadedObjectStateGroup->id);
246
            $this->repository->commit();
247
        } catch (Exception $e) {
248
            $this->repository->rollback();
249
            throw $e;
250
        }
251
    }
252
253
    /**
254
     * Creates a new object state in the given group.
255
     *
256
     * Note: in current kernel: If it is the first state all content objects will
257
     * set to this state.
258
     *
259
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state
260
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
261
     *
262
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
263
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct
264
     *
265
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
266
     */
267
    public function createObjectState(APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct): APIObjectState
268
    {
269
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateCreateStruct, [$objectStateGroup])) {
270
            throw new UnauthorizedException('state', 'administrate');
271
        }
272
273
        $inputStruct = $this->buildCreateInputStruct(
274
            $objectStateCreateStruct->identifier,
275
            $objectStateCreateStruct->defaultLanguageCode,
276
            $objectStateCreateStruct->names,
277
            $objectStateCreateStruct->descriptions
278
        );
279
280
        try {
281
            $this->objectStateHandler->loadByIdentifier($inputStruct->identifier, $objectStateGroup->id);
282
            throw new InvalidArgumentException(
283
                'objectStateCreateStruct',
284
                'An Object state with the provided identifier already exists in the provided Object state group'
285
            );
286
        } catch (APINotFoundException $e) {
287
            // Do nothing
288
        }
289
290
        $this->repository->beginTransaction();
291
        try {
292
            $spiObjectState = $this->objectStateHandler->create($objectStateGroup->id, $inputStruct);
293
294
            if (is_int($objectStateCreateStruct->priority)) {
295
                $this->objectStateHandler->setPriority(
296
                    $spiObjectState->id,
297
                    $objectStateCreateStruct->priority
298
                );
299
300
                // Reload the object state to have the updated priority,
301
                // considering that priorities are always incremental within a group
302
                $spiObjectState = $this->objectStateHandler->load($spiObjectState->id);
303
            }
304
305
            $this->repository->commit();
306
        } catch (Exception $e) {
307
            $this->repository->rollback();
308
            throw $e;
309
        }
310
311
        return $this->buildDomainObjectStateObject($spiObjectState);
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function loadObjectState(int $stateId, array $prioritizedLanguages = []): APIObjectState
318
    {
319
        $spiObjectState = $this->objectStateHandler->load($stateId);
320
321
        return $this->buildDomainObjectStateObject($spiObjectState, null, $prioritizedLanguages);
322
    }
323
324
    /**
325
     * Updates an object state.
326
     *
327
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state
328
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
329
     *
330
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
331
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct
332
     *
333
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
334
     */
335
    public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct): APIObjectState
336
    {
337
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectState)) {
338
            throw new UnauthorizedException('state', 'administrate');
339
        }
340
341
        $loadedObjectState = $this->loadObjectState($objectState->id);
342
343
        $inputStruct = $this->buildObjectStateUpdateInputStruct(
344
            $loadedObjectState,
345
            $objectStateUpdateStruct->identifier,
346
            $objectStateUpdateStruct->defaultLanguageCode,
347
            $objectStateUpdateStruct->names,
348
            $objectStateUpdateStruct->descriptions
349
        );
350
351
        if ($objectStateUpdateStruct->identifier !== null) {
352
            try {
353
                $existingObjectState = $this->objectStateHandler->loadByIdentifier(
354
                    $inputStruct->identifier,
355
                    $loadedObjectState->getObjectStateGroup()->id
356
                );
357
358
                if ($existingObjectState->id != $loadedObjectState->id) {
359
                    throw new InvalidArgumentException(
360
                        'objectStateUpdateStruct',
361
                        'An Object state with the provided identifier already exists in provided Object state group'
362
                    );
363
                }
364
            } catch (APINotFoundException $e) {
365
                // Do nothing
366
            }
367
        }
368
369
        $this->repository->beginTransaction();
370
        try {
371
            $spiObjectState = $this->objectStateHandler->update(
372
                $loadedObjectState->id,
373
                $inputStruct
374
            );
375
            $this->repository->commit();
376
        } catch (Exception $e) {
377
            $this->repository->rollback();
378
            throw $e;
379
        }
380
381
        return $this->buildDomainObjectStateObject($spiObjectState, null, $objectState->prioritizedLanguages);
0 ignored issues
show
Documentation introduced by
The property prioritizedLanguages 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...
382
    }
383
384
    /**
385
     * Changes the priority of the state.
386
     *
387
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state
388
     *
389
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
390
     * @param int $priority
391
     */
392
    public function setPriorityOfObjectState(APIObjectState $objectState, int $priority): void
393
    {
394
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectState)) {
395
            throw new UnauthorizedException('state', 'administrate');
396
        }
397
398
        $loadedObjectState = $this->loadObjectState($objectState->id);
399
400
        $this->repository->beginTransaction();
401
        try {
402
            $this->objectStateHandler->setPriority(
403
                $loadedObjectState->id,
404
                $priority
405
            );
406
            $this->repository->commit();
407
        } catch (Exception $e) {
408
            $this->repository->rollback();
409
            throw $e;
410
        }
411
    }
412
413
    /**
414
     * Deletes a object state. The state of the content objects is reset to the
415
     * first object state in the group.
416
     *
417
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state
418
     *
419
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
420
     */
421
    public function deleteObjectState(APIObjectState $objectState): void
422
    {
423
        if (!$this->permissionResolver->canUser('state', 'administrate', $objectState)) {
424
            throw new UnauthorizedException('state', 'administrate');
425
        }
426
427
        $loadedObjectState = $this->loadObjectState($objectState->id);
428
429
        $this->repository->beginTransaction();
430
        try {
431
            $this->objectStateHandler->delete($loadedObjectState->id);
432
            $this->repository->commit();
433
        } catch (Exception $e) {
434
            $this->repository->rollback();
435
            throw $e;
436
        }
437
    }
438
439
    /**
440
     * Sets the object-state of a state group to $state for the given content.
441
     *
442
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group
443
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state
444
     *
445
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
446
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
447
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
448
     */
449
    public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState): void
450
    {
451
        if (!$this->permissionResolver->canUser('state', 'assign', $contentInfo, [$objectState])) {
452
            throw new UnauthorizedException('state', 'assign', ['contentId' => $contentInfo->id]);
453
        }
454
455
        $loadedObjectState = $this->loadObjectState($objectState->id);
456
457
        if ($loadedObjectState->getObjectStateGroup()->id != $objectStateGroup->id) {
458
            throw new InvalidArgumentException('objectState', 'Object state does not belong to the given group');
459
        }
460
461
        $this->repository->beginTransaction();
462
        try {
463
            $this->objectStateHandler->setContentState(
464
                $contentInfo->id,
465
                $objectStateGroup->id,
466
                $loadedObjectState->id
467
            );
468
            $this->repository->commit();
469
        } catch (Exception $e) {
470
            $this->repository->rollback();
471
            throw $e;
472
        }
473
    }
474
475
    /**
476
     * Gets the object-state of object identified by $contentId.
477
     *
478
     * The $state is the id of the state within one group.
479
     *
480
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
481
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
482
     *
483
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
484
     */
485
    public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup): APIObjectState
486
    {
487
        $spiObjectState = $this->objectStateHandler->getContentState(
488
            $contentInfo->id,
489
            $objectStateGroup->id
490
        );
491
492
        return $this->buildDomainObjectStateObject($spiObjectState, $objectStateGroup);
493
    }
494
495
    /**
496
     * Returns the number of objects which are in this state.
497
     *
498
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
499
     *
500
     * @return int
501
     */
502
    public function getContentCount(APIObjectState $objectState): int
503
    {
504
        return $this->objectStateHandler->getContentCount(
505
            $objectState->id
506
        );
507
    }
508
509
    /**
510
     * Instantiates a new Object State Group Create Struct and sets $identified in it.
511
     *
512
     * @param string $identifier
513
     *
514
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct
515
     */
516
    public function newObjectStateGroupCreateStruct(string $identifier): ObjectStateGroupCreateStruct
517
    {
518
        $objectStateGroupCreateStruct = new ObjectStateGroupCreateStruct();
519
        $objectStateGroupCreateStruct->identifier = $identifier;
520
521
        return $objectStateGroupCreateStruct;
522
    }
523
524
    /**
525
     * Instantiates a new Object State Group Update Struct.
526
     *
527
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct
528
     */
529
    public function newObjectStateGroupUpdateStruct(): ObjectStateGroupUpdateStruct
530
    {
531
        return new ObjectStateGroupUpdateStruct();
532
    }
533
534
    /**
535
     * Instantiates a new Object State Create Struct and sets $identifier in it.
536
     *
537
     * @param string $identifier
538
     *
539
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct
540
     */
541
    public function newObjectStateCreateStruct(string $identifier): ObjectStateCreateStruct
542
    {
543
        $objectStateCreateStruct = new ObjectStateCreateStruct();
544
        $objectStateCreateStruct->identifier = $identifier;
545
546
        return $objectStateCreateStruct;
547
    }
548
549
    /**
550
     * Instantiates a new Object State Update Struct.
551
     *
552
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct
553
     */
554
    public function newObjectStateUpdateStruct(): ObjectStateUpdateStruct
555
    {
556
        return new ObjectStateUpdateStruct();
557
    }
558
559
    /**
560
     * Converts the object state SPI value object to API value object.
561
     *
562
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState
563
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
564
     * @param string[] $prioritizedLanguages
565
     *
566
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
567
     */
568
    protected function buildDomainObjectStateObject(
569
        SPIObjectState $spiObjectState,
570
        APIObjectStateGroup $objectStateGroup = null,
571
        array $prioritizedLanguages = []
572
    ): APIObjectState {
573
        $objectStateGroup = $objectStateGroup ?: $this->loadObjectStateGroup($spiObjectState->groupId, $prioritizedLanguages);
574
575
        return new ObjectState(
576
            [
577
                'id' => $spiObjectState->id,
578
                'identifier' => $spiObjectState->identifier,
579
                'priority' => $spiObjectState->priority,
580
                'mainLanguageCode' => $spiObjectState->defaultLanguage,
581
                'languageCodes' => $spiObjectState->languageCodes,
582
                'names' => $spiObjectState->name,
583
                'descriptions' => $spiObjectState->description,
584
                'objectStateGroup' => $objectStateGroup,
585
                'prioritizedLanguages' => $prioritizedLanguages,
586
            ]
587
        );
588
    }
589
590
    /**
591
     * Converts the object state group SPI value object to API value object.
592
     *
593
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup
594
     * @param array $prioritizedLanguages
595
     *
596
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
597
     */
598
    protected function buildDomainObjectStateGroupObject(
599
        SPIObjectStateGroup $spiObjectStateGroup,
600
        array $prioritizedLanguages = []
601
    ): APIObjectStateGroup {
602
        return new ObjectStateGroup(
603
            [
604
                'id' => $spiObjectStateGroup->id,
605
                'identifier' => $spiObjectStateGroup->identifier,
606
                'mainLanguageCode' => $spiObjectStateGroup->defaultLanguage,
607
                'languageCodes' => $spiObjectStateGroup->languageCodes,
608
                'names' => $spiObjectStateGroup->name,
609
                'descriptions' => $spiObjectStateGroup->description,
610
                'prioritizedLanguages' => $prioritizedLanguages,
611
            ]
612
        );
613
    }
614
615
    /**
616
     * Validates input for creating object states/groups and builds the InputStruct object.
617
     *
618
     * @param string $identifier
619
     * @param string $defaultLanguageCode
620
     * @param string[] $names
621
     * @param string[]|null $descriptions
622
     *
623
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
624
     */
625
    protected function buildCreateInputStruct(
626
        string $identifier,
627
        string $defaultLanguageCode,
628
        array $names,
629
        ?array $descriptions
630
    ): InputStruct {
631
        if (!is_string($identifier) || empty($identifier)) {
632
            throw new InvalidArgumentValue('identifier', $identifier);
633
        }
634
635
        if (!is_string($defaultLanguageCode) || empty($defaultLanguageCode)) {
636
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
637
        }
638
639
        if (!is_array($names) || empty($names)) {
640
            throw new InvalidArgumentValue('names', $names);
641
        }
642
643
        if (!isset($names[$defaultLanguageCode])) {
644
            throw new InvalidArgumentValue('names', $names);
645
        }
646
647
        foreach ($names as $languageCode => $name) {
648
            try {
649
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
650
            } catch (NotFoundException $e) {
651
                throw new InvalidArgumentValue('names', $names);
652
            }
653
654
            if (!is_string($name) || empty($name)) {
655
                throw new InvalidArgumentValue('names', $names);
656
            }
657
        }
658
659
        $descriptions = $descriptions !== null ? $descriptions : [];
660
661
        $inputStruct = new InputStruct();
662
        $inputStruct->identifier = $identifier;
663
        $inputStruct->defaultLanguage = $defaultLanguageCode;
664
        $inputStruct->name = $names;
665
666
        $inputStruct->description = [];
667
        foreach ($names as $languageCode => $name) {
668
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
669
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
670
            } else {
671
                $inputStruct->description[$languageCode] = '';
672
            }
673
        }
674
675
        return $inputStruct;
676
    }
677
678
    /**
679
     * Validates input for updating object states and builds the InputStruct object.
680
     *
681
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
682
     * @param string|null $identifier
683
     * @param string|null $defaultLanguageCode
684
     * @param string[]|null $names
685
     * @param string[]|null $descriptions
686
     *
687
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
688
     */
689
    protected function buildObjectStateUpdateInputStruct(
690
        APIObjectState $objectState,
691
        ?string $identifier,
692
        ?string $defaultLanguageCode,
693
        ?array $names,
694
        ?array $descriptions
695
    ): InputStruct {
696
        $inputStruct = new InputStruct();
697
698
        if ($identifier !== null && (!is_string($identifier) || empty($identifier))) {
699
            throw new InvalidArgumentValue('identifier', $identifier);
700
        }
701
702
        $inputStruct->identifier = $identifier !== null ? $identifier : $objectState->identifier;
703
704
        if ($defaultLanguageCode !== null && (!is_string($defaultLanguageCode) || empty($defaultLanguageCode))) {
705
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
706
        }
707
708
        $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...
709
710
        if ($names !== null && (!is_array($names) || empty($names))) {
711
            throw new InvalidArgumentValue('names', $names);
712
        }
713
714
        $inputStruct->name = $names !== null ? $names : $objectState->getNames();
715
716
        if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
717
            throw new InvalidArgumentValue('names', $inputStruct->name);
718
        }
719
720
        foreach ($inputStruct->name as $languageCode => $name) {
721
            try {
722
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
723
            } catch (NotFoundException $e) {
724
                throw new InvalidArgumentValue('names', $inputStruct->name);
725
            }
726
727
            if (!is_string($name) || empty($name)) {
728
                throw new InvalidArgumentValue('names', $inputStruct->name);
729
            }
730
        }
731
732
        $descriptions = $descriptions !== null ? $descriptions : $objectState->getDescriptions();
733
        $descriptions = $descriptions !== null ? $descriptions : [];
734
735
        $inputStruct->description = [];
736
        foreach ($inputStruct->name as $languageCode => $name) {
737
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
738
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
739
            } else {
740
                $inputStruct->description[$languageCode] = '';
741
            }
742
        }
743
744
        return $inputStruct;
745
    }
746
747
    /**
748
     * Validates input for updating object state groups and builds the InputStruct object.
749
     *
750
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
751
     * @param string|null $identifier
752
     * @param string|null $defaultLanguageCode
753
     * @param string[]|null $names
754
     * @param string[]|null $descriptions
755
     *
756
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
757
     */
758
    protected function buildObjectStateGroupUpdateInputStruct(
759
        APIObjectStateGroup $objectStateGroup,
760
        ?string $identifier,
761
        ?string $defaultLanguageCode,
762
        ?array $names,
763
        ?array $descriptions
764
    ): InputStruct {
765
        $inputStruct = new InputStruct();
766
767
        if ($identifier !== null && empty($identifier)) {
768
            throw new InvalidArgumentValue('identifier', $identifier);
769
        }
770
771
        $inputStruct->identifier = $identifier !== null ? $identifier : $objectStateGroup->identifier;
772
773
        if ($defaultLanguageCode !== null && empty($defaultLanguageCode)) {
774
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
775
        }
776
777
        $inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectStateGroup->defaultLanguageCode;
778
779
        if ($names !== null && empty($names)) {
780
            throw new InvalidArgumentValue('names', $names);
781
        }
782
783
        $inputStruct->name = $names !== null ? $names : $objectStateGroup->getNames();
784
785
        if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
786
            throw new InvalidArgumentValue('names', $inputStruct->name);
787
        }
788
789
        foreach ($inputStruct->name as $languageCode => $name) {
790
            try {
791
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
792
            } catch (NotFoundException $e) {
793
                throw new InvalidArgumentValue('names', $inputStruct->name);
794
            }
795
796
            if (!is_string($name) || empty($name)) {
797
                throw new InvalidArgumentValue('names', $inputStruct->name);
798
            }
799
        }
800
801
        $descriptions = $descriptions !== null ? $descriptions : $objectStateGroup->getDescriptions();
802
        $descriptions = $descriptions !== null ? $descriptions : [];
803
804
        $inputStruct->description = [];
805
        foreach ($inputStruct->name as $languageCode => $name) {
806
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
807
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
808
            } else {
809
                $inputStruct->description[$languageCode] = '';
810
            }
811
        }
812
813
        return $inputStruct;
814
    }
815
}
816