Completed
Push — qa-cumulative-ezp-31278-31279-... ( 4a8f9c )
by
unknown
14:58
created

LimitationService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getLimitationType() 0 8 2
A validateLimitations() 0 12 3
A validateLimitation() 0 18 2
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
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\Permission;
10
11
use eZ\Publish\API\Repository\Values\User\Limitation;
12
use eZ\Publish\Core\Base\Exceptions\NotFound\LimitationNotFoundException;
13
use eZ\Publish\Core\Base\Exceptions\BadStateException;
14
use eZ\Publish\SPI\Limitation\Type;
15
use Traversable;
16
17
/**
18
 * Internal service to deal with limitations and limitation types.
19
 *
20
 * @internal Meant for internal use by Repository.
21
 */
22
class LimitationService
23
{
24
    /** @var \eZ\Publish\SPI\Limitation\Type[] */
25
    private $limitationTypes;
26
27
    public function __construct(Traversable $limitationTypes = null)
28
    {
29
        $this->limitationTypes = null !== $limitationTypes
30
            ? iterator_to_array($limitationTypes) :
31
            [];
32
    }
33
34
    /**
35
     * Returns the LimitationType registered with the given identifier.
36
     *
37
     * Returns the correct implementation of API Limitation value object
38
     * based on provided identifier
39
     *
40
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFound\LimitationNotFoundException
41
     */
42
    public function getLimitationType(string $identifier): Type
43
    {
44
        if (!isset($this->limitationTypes[$identifier])) {
45
            throw new LimitationNotFoundException($identifier);
46
        }
47
48
        return $this->limitationTypes[$identifier];
49
    }
50
51
    /**
52
     * Validates an array of Limitations.
53
     *
54
     * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations
55
     *
56
     * @return \eZ\Publish\SPI\FieldType\ValidationError[][]
57
     *
58
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
59
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
60
     */
61
    public function validateLimitations(array $limitations): array
62
    {
63
        $allErrors = [];
64
        foreach ($limitations as $limitation) {
65
            $errors = $this->validateLimitation($limitation);
66
            if (!empty($errors)) {
67
                $allErrors[$limitation->getIdentifier()] = $errors;
68
            }
69
        }
70
71
        return $allErrors;
72
    }
73
74
    /**
75
     * Validates single Limitation.
76
     *
77
     * @return \eZ\Publish\SPI\FieldType\ValidationError[]
78
     *
79
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If the Role settings is in a bad state*@throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
80
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
81
     */
82
    public function validateLimitation(Limitation $limitation): array
83
    {
84
        $identifier = $limitation->getIdentifier();
85
        if (!isset($this->limitationTypes[$identifier])) {
86
            throw new BadStateException(
87
                '$identifier',
88
                "limitationType[{$identifier}] is not configured"
89
            );
90
        }
91
92
        $type = $this->limitationTypes[$identifier];
93
94
        // This will throw if it does not pass
95
        $type->acceptValue($limitation);
96
97
        // This return array of validation errors
98
        return $type->validate($limitation);
99
    }
100
}
101