Completed
Push — master ( c54917...813cd3 )
by
unknown
29:02 queued 14:06
created

ObjectStateService::setContentState()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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