Completed
Push — ezp25533-http_cache_anonymous_... ( b956b0...914558 )
by
unknown
25:20
created

ObjectStateService   F

Complexity

Total Complexity 119

Size/Duplication

Total Lines 777
Duplicated Lines 26.25 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
dl 204
loc 777
rs 1.263
c 0
b 0
f 0
wmc 119
lcom 1
cbo 18

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B createObjectStateGroup() 0 34 4
A loadObjectStateGroup() 0 6 1
A loadObjectStateGroups() 0 11 2
A loadObjectStates() 0 11 2
B updateObjectStateGroup() 44 44 6
A deleteObjectStateGroup() 0 17 3
B createObjectState() 0 46 5
A loadObjectState() 0 6 1
B updateObjectState() 48 48 6
B setPriorityOfObjectState() 0 24 4
A deleteObjectState() 0 17 3
B setContentState() 0 25 4
A getContentState() 0 9 1
A getContentCount() 0 6 1
A newObjectStateGroupCreateStruct() 0 7 1
A newObjectStateGroupUpdateStruct() 0 4 1
A newObjectStateCreateStruct() 0 7 1
A newObjectStateUpdateStruct() 0 4 1
A buildDomainObjectStateObject() 0 17 2
A buildDomainObjectStateGroupObject() 0 13 1
C buildCreateInputStruct() 0 52 18
C buildObjectStateUpdateInputStruct() 56 56 25
C buildObjectStateGroupUpdateInputStruct() 56 56 25

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ObjectStateService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ObjectStateService, and based on these observations, apply Extract Interface, too.

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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Repository;
12
13
use eZ\Publish\API\Repository\ObjectStateService as ObjectStateServiceInterface;
14
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
15
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler;
16
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
17
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct;
18
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
19
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct;
20
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
21
use eZ\Publish\API\Repository\Values\ObjectState\ObjectState as APIObjectState;
22
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup as APIObjectStateGroup;
23
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectState;
24
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectStateGroup;
25
use eZ\Publish\SPI\Persistence\Content\ObjectState as SPIObjectState;
26
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as SPIObjectStateGroup;
27
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct;
28
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
29
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
30
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
31
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
32
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
33
use Exception;
34
35
/**
36
 * ObjectStateService service.
37
 *
38
 * @example Examples/objectstates.php tbd.
39
 */
40
class ObjectStateService implements ObjectStateServiceInterface
41
{
42
    /**
43
     * @var \eZ\Publish\API\Repository\Repository
44
     */
45
    protected $repository;
46
47
    /**
48
     * @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler
49
     */
50
    protected $objectStateHandler;
51
52
    /**
53
     * @var array
54
     */
55
    protected $settings;
56
57
    /**
58
     * Setups service with reference to repository object that created it & corresponding handler.
59
     *
60
     * @param \eZ\Publish\API\Repository\Repository $repository
61
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler
62
     * @param array $settings
63
     */
64
    public function __construct(RepositoryInterface $repository, Handler $objectStateHandler, array $settings = array())
65
    {
66
        $this->repository = $repository;
67
        $this->objectStateHandler = $objectStateHandler;
68
        // Union makes sure default settings are ignored if provided in argument
69
        $this->settings = $settings + array(
70
            //'defaultSetting' => array(),
71
        );
72
    }
73
74
    /**
75
     * Creates a new object state group.
76
     *
77
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group
78
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
79
     *
80
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct
81
     *
82
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
83
     */
84
    public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct)
85
    {
86
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
87
            throw new UnauthorizedException('state', 'administrate');
88
        }
89
90
        $inputStruct = $this->buildCreateInputStruct(
91
            $objectStateGroupCreateStruct->identifier,
92
            $objectStateGroupCreateStruct->defaultLanguageCode,
93
            $objectStateGroupCreateStruct->names,
94
            $objectStateGroupCreateStruct->descriptions
95
        );
96
97
        try {
98
            $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier);
99
            throw new InvalidArgumentException(
100
                'objectStateGroupCreateStruct',
101
                'Object state group with provided identifier already exists'
102
            );
103
        } catch (APINotFoundException $e) {
104
            // Do nothing
105
        }
106
107
        $this->repository->beginTransaction();
108
        try {
109
            $spiObjectStateGroup = $this->objectStateHandler->createGroup($inputStruct);
110
            $this->repository->commit();
111
        } catch (Exception $e) {
112
            $this->repository->rollback();
113
            throw $e;
114
        }
115
116
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup);
117
    }
118
119
    /**
120
     * Loads a object state group.
121
     *
122
     * @param mixed $objectStateGroupId
123
     *
124
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the group was not found
125
     *
126
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
127
     */
128
    public function loadObjectStateGroup($objectStateGroupId)
129
    {
130
        $spiObjectStateGroup = $this->objectStateHandler->loadGroup($objectStateGroupId);
131
132
        return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup);
133
    }
134
135
    /**
136
     * Loads all object state groups.
137
     *
138
     * @param int $offset
139
     * @param int $limit
140
     *
141
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup[]
142
     */
143
    public function loadObjectStateGroups($offset = 0, $limit = -1)
144
    {
145
        $spiObjectStateGroups = $this->objectStateHandler->loadAllGroups($offset, $limit);
146
147
        $objectStateGroups = array();
148
        foreach ($spiObjectStateGroups as $spiObjectStateGroup) {
149
            $objectStateGroups[] = $this->buildDomainObjectStateGroupObject($spiObjectStateGroup);
150
        }
151
152
        return $objectStateGroups;
153
    }
154
155
    /**
156
     * This method returns the ordered list of object states of a group.
157
     *
158
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
159
     *
160
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[]
161
     */
162
    public function loadObjectStates(APIObjectStateGroup $objectStateGroup)
163
    {
164
        $spiObjectStates = $this->objectStateHandler->loadObjectStates($objectStateGroup->id);
165
166
        $objectStates = array();
167
        foreach ($spiObjectStates as $spiObjectState) {
168
            $objectStates[] = $this->buildDomainObjectStateObject($spiObjectState, $objectStateGroup);
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 View Code Duplication
    public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct)
186
    {
187
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
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)
238
    {
239
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
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)
270
    {
271
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
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
     * Loads an object state.
318
     *
319
     * @param mixed $stateId
320
     *
321
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the state was not found
322
     *
323
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
324
     */
325
    public function loadObjectState($stateId)
326
    {
327
        $spiObjectState = $this->objectStateHandler->load($stateId);
328
329
        return $this->buildDomainObjectStateObject($spiObjectState);
330
    }
331
332
    /**
333
     * Updates an object state.
334
     *
335
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state
336
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
337
     *
338
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
339
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct
340
     *
341
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
342
     */
343 View Code Duplication
    public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct)
344
    {
345
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
346
            throw new UnauthorizedException('state', 'administrate');
347
        }
348
349
        $loadedObjectState = $this->loadObjectState($objectState->id);
350
351
        $inputStruct = $this->buildObjectStateUpdateInputStruct(
352
            $loadedObjectState,
353
            $objectStateUpdateStruct->identifier,
354
            $objectStateUpdateStruct->defaultLanguageCode,
355
            $objectStateUpdateStruct->names,
356
            $objectStateUpdateStruct->descriptions
357
        );
358
359
        if ($objectStateUpdateStruct->identifier !== null) {
360
            try {
361
                $existingObjectState = $this->objectStateHandler->loadByIdentifier(
362
                    $inputStruct->identifier,
363
                    $loadedObjectState->getObjectStateGroup()->id
364
                );
365
366
                if ($existingObjectState->id != $loadedObjectState->id) {
367
                    throw new InvalidArgumentException(
368
                        'objectStateUpdateStruct',
369
                        'Object state with provided identifier already exists in provided object state group'
370
                    );
371
                }
372
            } catch (APINotFoundException $e) {
373
                // Do nothing
374
            }
375
        }
376
377
        $this->repository->beginTransaction();
378
        try {
379
            $spiObjectState = $this->objectStateHandler->update(
380
                $loadedObjectState->id,
381
                $inputStruct
382
            );
383
            $this->repository->commit();
384
        } catch (Exception $e) {
385
            $this->repository->rollback();
386
            throw $e;
387
        }
388
389
        return $this->buildDomainObjectStateObject($spiObjectState);
390
    }
391
392
    /**
393
     * Changes the priority of the state.
394
     *
395
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state
396
     *
397
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
398
     * @param int $priority
399
     */
400
    public function setPriorityOfObjectState(APIObjectState $objectState, $priority)
401
    {
402
        if (!is_int($priority)) {
403
            throw new InvalidArgumentValue('priority', $priority);
404
        }
405
406
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
407
            throw new UnauthorizedException('state', 'administrate');
408
        }
409
410
        $loadedObjectState = $this->loadObjectState($objectState->id);
411
412
        $this->repository->beginTransaction();
413
        try {
414
            $this->objectStateHandler->setPriority(
415
                $loadedObjectState->id,
416
                $priority
417
            );
418
            $this->repository->commit();
419
        } catch (Exception $e) {
420
            $this->repository->rollback();
421
            throw $e;
422
        }
423
    }
424
425
    /**
426
     * Deletes a object state. The state of the content objects is reset to the
427
     * first object state in the group.
428
     *
429
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state
430
     *
431
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
432
     */
433
    public function deleteObjectState(APIObjectState $objectState)
434
    {
435
        if ($this->repository->hasAccess('state', 'administrate') !== true) {
436
            throw new UnauthorizedException('state', 'administrate');
437
        }
438
439
        $loadedObjectState = $this->loadObjectState($objectState->id);
440
441
        $this->repository->beginTransaction();
442
        try {
443
            $this->objectStateHandler->delete($loadedObjectState->id);
444
            $this->repository->commit();
445
        } catch (Exception $e) {
446
            $this->repository->rollback();
447
            throw $e;
448
        }
449
    }
450
451
    /**
452
     * Sets the object-state of a state group to $state for the given content.
453
     *
454
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group
455
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state
456
     *
457
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
458
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
459
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
460
     */
461
    public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState)
462
    {
463
        if ($this->repository->canUser('state', 'assign', $contentInfo, $objectState) !== true) {
464
            throw new UnauthorizedException('state', 'assign', array('contentId' => $contentInfo->id));
465
        }
466
467
        $loadedObjectState = $this->loadObjectState($objectState->id);
468
469
        if ($loadedObjectState->getObjectStateGroup()->id != $objectStateGroup->id) {
470
            throw new InvalidArgumentException('objectState', 'Object state does not belong to the given group');
471
        }
472
473
        $this->repository->beginTransaction();
474
        try {
475
            $this->objectStateHandler->setContentState(
476
                $contentInfo->id,
477
                $objectStateGroup->id,
478
                $loadedObjectState->id
479
            );
480
            $this->repository->commit();
481
        } catch (Exception $e) {
482
            $this->repository->rollback();
483
            throw $e;
484
        }
485
    }
486
487
    /**
488
     * Gets the object-state of object identified by $contentId.
489
     *
490
     * The $state is the id of the state within one group.
491
     *
492
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
493
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
494
     *
495
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
496
     */
497
    public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup)
498
    {
499
        $spiObjectState = $this->objectStateHandler->getContentState(
500
            $contentInfo->id,
501
            $objectStateGroup->id
502
        );
503
504
        return $this->buildDomainObjectStateObject($spiObjectState);
505
    }
506
507
    /**
508
     * Returns the number of objects which are in this state.
509
     *
510
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
511
     *
512
     * @return int
513
     */
514
    public function getContentCount(APIObjectState $objectState)
515
    {
516
        return $this->objectStateHandler->getContentCount(
517
            $objectState->id
518
        );
519
    }
520
521
    /**
522
     * Instantiates a new Object State Group Create Struct and sets $identified in it.
523
     *
524
     * @param string $identifier
525
     *
526
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct
527
     */
528
    public function newObjectStateGroupCreateStruct($identifier)
529
    {
530
        $objectStateGroupCreateStruct = new ObjectStateGroupCreateStruct();
531
        $objectStateGroupCreateStruct->identifier = $identifier;
532
533
        return $objectStateGroupCreateStruct;
534
    }
535
536
    /**
537
     * Instantiates a new Object State Group Update Struct.
538
     *
539
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct
540
     */
541
    public function newObjectStateGroupUpdateStruct()
542
    {
543
        return new ObjectStateGroupUpdateStruct();
544
    }
545
546
    /**
547
     * Instantiates a new Object State Create Struct and sets $identifier in it.
548
     *
549
     * @param string $identifier
550
     *
551
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct
552
     */
553
    public function newObjectStateCreateStruct($identifier)
554
    {
555
        $objectStateCreateStruct = new ObjectStateCreateStruct();
556
        $objectStateCreateStruct->identifier = $identifier;
557
558
        return $objectStateCreateStruct;
559
    }
560
561
    /**
562
     * Instantiates a new Object State Update Struct.
563
     *
564
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct
565
     */
566
    public function newObjectStateUpdateStruct()
567
    {
568
        return new ObjectStateUpdateStruct();
569
    }
570
571
    /**
572
     * Converts the object state SPI value object to API value object.
573
     *
574
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState
575
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
576
     *
577
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
578
     */
579
    protected function buildDomainObjectStateObject(SPIObjectState $spiObjectState, APIObjectStateGroup $objectStateGroup = null)
580
    {
581
        $objectStateGroup = $objectStateGroup ?: $this->loadObjectStateGroup($spiObjectState->groupId);
582
583
        return new ObjectState(
584
            array(
585
                'id' => $spiObjectState->id,
586
                'identifier' => $spiObjectState->identifier,
587
                'priority' => $spiObjectState->priority,
588
                'defaultLanguageCode' => $spiObjectState->defaultLanguage,
589
                'languageCodes' => $spiObjectState->languageCodes,
590
                'names' => $spiObjectState->name,
591
                'descriptions' => $spiObjectState->description,
592
                'objectStateGroup' => $objectStateGroup,
593
            )
594
        );
595
    }
596
597
    /**
598
     * Converts the object state group SPI value object to API value object.
599
     *
600
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup
601
     *
602
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
603
     */
604
    protected function buildDomainObjectStateGroupObject(SPIObjectStateGroup $spiObjectStateGroup)
605
    {
606
        return new ObjectStateGroup(
607
            array(
608
                'id' => $spiObjectStateGroup->id,
609
                'identifier' => $spiObjectStateGroup->identifier,
610
                'defaultLanguageCode' => $spiObjectStateGroup->defaultLanguage,
611
                'languageCodes' => $spiObjectStateGroup->languageCodes,
612
                'names' => $spiObjectStateGroup->name,
613
                'descriptions' => $spiObjectStateGroup->description,
614
            )
615
        );
616
    }
617
618
    /**
619
     * Validates input for creating object states/groups and builds the InputStruct object.
620
     *
621
     * @param string $identifier
622
     * @param string $defaultLanguageCode
623
     * @param string[] $names
624
     * @param string[] $descriptions
625
     *
626
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
627
     */
628
    protected function buildCreateInputStruct($identifier, $defaultLanguageCode, $names, $descriptions)
629
    {
630
        if (!is_string($identifier) || empty($identifier)) {
631
            throw new InvalidArgumentValue('identifier', $identifier);
632
        }
633
634
        if (!is_string($defaultLanguageCode) || empty($defaultLanguageCode)) {
635
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
636
        }
637
638
        if (!is_array($names) || empty($names)) {
639
            throw new InvalidArgumentValue('names', $names);
640
        }
641
642
        if (!isset($names[$defaultLanguageCode])) {
643
            throw new InvalidArgumentValue('names', $names);
644
        }
645
646
        foreach ($names as $languageCode => $name) {
647
            try {
648
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
649
            } catch (NotFoundException $e) {
650
                throw new InvalidArgumentValue('names', $names);
651
            }
652
653
            if (!is_string($name) || empty($name)) {
654
                throw new InvalidArgumentValue('names', $names);
655
            }
656
        }
657
658
        if ($descriptions !== null && !is_array($descriptions)) {
659
            throw new InvalidArgumentValue('descriptions', $descriptions);
660
        }
661
662
        $descriptions = $descriptions !== null ? $descriptions : array();
663
664
        $inputStruct = new InputStruct();
665
        $inputStruct->identifier = $identifier;
666
        $inputStruct->defaultLanguage = $defaultLanguageCode;
667
        $inputStruct->name = $names;
668
669
        $inputStruct->description = array();
670
        foreach ($names as $languageCode => $name) {
671
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
672
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
673
            } else {
674
                $inputStruct->description[$languageCode] = '';
675
            }
676
        }
677
678
        return $inputStruct;
679
    }
680
681
    /**
682
     * Validates input for updating object states and builds the InputStruct object.
683
     *
684
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
685
     * @param string $identifier
686
     * @param string $defaultLanguageCode
687
     * @param string[] $names
688
     * @param string[] $descriptions
689
     *
690
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
691
     */
692 View Code Duplication
    protected function buildObjectStateUpdateInputStruct(APIObjectState $objectState, $identifier, $defaultLanguageCode, $names, $descriptions)
693
    {
694
        $inputStruct = new InputStruct();
695
696
        if ($identifier !== null && (!is_string($identifier) || empty($identifier))) {
697
            throw new InvalidArgumentValue('identifier', $identifier);
698
        }
699
700
        $inputStruct->identifier = $identifier !== null ? $identifier : $objectState->identifier;
701
702
        if ($defaultLanguageCode !== null && (!is_string($defaultLanguageCode) || empty($defaultLanguageCode))) {
703
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
704
        }
705
706
        $inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectState->defaultLanguageCode;
707
708
        if ($names !== null && (!is_array($names) || empty($names))) {
709
            throw new InvalidArgumentValue('names', $names);
710
        }
711
712
        $inputStruct->name = $names !== null ? $names : $objectState->getNames();
713
714
        if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
715
            throw new InvalidArgumentValue('names', $inputStruct->name);
716
        }
717
718
        foreach ($inputStruct->name as $languageCode => $name) {
719
            try {
720
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
721
            } catch (NotFoundException $e) {
722
                throw new InvalidArgumentValue('names', $inputStruct->name);
723
            }
724
725
            if (!is_string($name) || empty($name)) {
726
                throw new InvalidArgumentValue('names', $inputStruct->name);
727
            }
728
        }
729
730
        if ($descriptions !== null && !is_array($descriptions)) {
731
            throw new InvalidArgumentValue('descriptions', $descriptions);
732
        }
733
734
        $descriptions = $descriptions !== null ? $descriptions : $objectState->getDescriptions();
735
        $descriptions = $descriptions !== null ? $descriptions : array();
736
737
        $inputStruct->description = array();
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 $identifier
754
     * @param string $defaultLanguageCode
755
     * @param string[] $names
756
     * @param string[] $descriptions
757
     *
758
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
759
     */
760 View Code Duplication
    protected function buildObjectStateGroupUpdateInputStruct(APIObjectStateGroup $objectStateGroup, $identifier, $defaultLanguageCode, $names, $descriptions)
761
    {
762
        $inputStruct = new InputStruct();
763
764
        if ($identifier !== null && (!is_string($identifier) || empty($identifier))) {
765
            throw new InvalidArgumentValue('identifier', $identifier);
766
        }
767
768
        $inputStruct->identifier = $identifier !== null ? $identifier : $objectStateGroup->identifier;
769
770
        if ($defaultLanguageCode !== null && (!is_string($defaultLanguageCode) || empty($defaultLanguageCode))) {
771
            throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
772
        }
773
774
        $inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectStateGroup->defaultLanguageCode;
0 ignored issues
show
Documentation introduced by
The property $defaultLanguageCode is declared protected in eZ\Publish\API\Repositor...tState\ObjectStateGroup. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
775
776
        if ($names !== null && (!is_array($names) || empty($names))) {
777
            throw new InvalidArgumentValue('names', $names);
778
        }
779
780
        $inputStruct->name = $names !== null ? $names : $objectStateGroup->getNames();
781
782
        if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
783
            throw new InvalidArgumentValue('names', $inputStruct->name);
784
        }
785
786
        foreach ($inputStruct->name as $languageCode => $name) {
787
            try {
788
                $this->repository->getContentLanguageService()->loadLanguage($languageCode);
789
            } catch (NotFoundException $e) {
790
                throw new InvalidArgumentValue('names', $inputStruct->name);
791
            }
792
793
            if (!is_string($name) || empty($name)) {
794
                throw new InvalidArgumentValue('names', $inputStruct->name);
795
            }
796
        }
797
798
        if ($descriptions !== null && !is_array($descriptions)) {
799
            throw new InvalidArgumentValue('descriptions', $descriptions);
800
        }
801
802
        $descriptions = $descriptions !== null ? $descriptions : $objectStateGroup->getDescriptions();
803
        $descriptions = $descriptions !== null ? $descriptions : array();
804
805
        $inputStruct->description = array();
806
        foreach ($inputStruct->name as $languageCode => $name) {
807
            if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
808
                $inputStruct->description[$languageCode] = $descriptions[$languageCode];
809
            } else {
810
                $inputStruct->description[$languageCode] = '';
811
            }
812
        }
813
814
        return $inputStruct;
815
    }
816
}
817