ValidValidator::validate()   D
last analyzed

Complexity

Conditions 21
Paths 4

Size

Total Lines 110
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 21
eloc 82
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 110
rs 4.1666

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
namespace App\Validator\Constraints\Role\Permissions;
4
5
use Ds\Component\Api\Collection\ServiceCollection;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Api\Collection\ServiceCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Ds\Component\Acl\Model\Permission;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Acl\Model\Permission was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\Validator\Constraint;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\Constraint was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Validator\ConstraintValidator;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\ConstraintValidator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Class ValidValidator
12
 *
13
 * @example
14
 * <code>
15
 * {
16
 *     "identities": [
17
 *         {
18
 *             "scope": {
19
 *                 "type": "owned_by",
20
 *                 "entity": "BusinessUnit",
21
 *                 "entityUuid": "aff1370c-9cb7-432d-a608-57021637f278"
22
 *             },
23
 *             "permissions": [
24
 *                 {
25
 *                     "key": "individual",
26
 *                     "attributes": ["BROWSE", "READ"]
27
 *                 },
28
 *                 {
29
 *                     "key": "individual_uuid",
30
 *                     "attributes": ["BROWSE", "READ"]
31
 *                 }
32
 *             ]
33
 *         }
34
 *     ]
35
 * }
36
 * </code>
37
 */
38
final class ValidValidator extends ConstraintValidator
39
{
40
    /**
41
     * @var \Ds\Component\Api\Collection\ServiceCollection
42
     */
43
    protected $serviceCollection;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param \Ds\Component\Api\Collection\ServiceCollection $serviceCollection
49
     */
50
    public function __construct(ServiceCollection $serviceCollection)
51
    {
52
        $this->serviceCollection = $serviceCollection;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function validate($role, Constraint $constraint)
59
    {
60
        foreach ($role->getPermissions() as $service => $permissions) {
61
            if (!$this->serviceCollection->containsKey($service.'.access')) {
62
                $this->context
63
                    ->buildViolation($constraint->serviceUndefined)
64
                    ->setParameter('{{ service }}', '"' . $service . '"')
65
                    ->atPath('permissions.' . $service)
66
                    ->addViolation();
67
68
                continue;
69
            }
70
71
            if (!is_array($permissions)) {
72
                $this->context
73
                    ->buildViolation($constraint->permissionsNotArray)
74
                    ->setParameter('{{ service }}', '"'.$service.'"')
75
                    ->atPath('permissions.'.$service)
76
                    ->addViolation();
77
78
                continue;
79
            }
80
81
            foreach ($permissions as $index => $permission) {
82
                if (!is_array($permission)) {
83
                    $this->context
84
                        ->buildViolation($constraint->permissionNotObject)
85
                        ->setParameter('{{ service }}', '"'.$service.'"')
86
                        ->atPath('permissions.'.$service.'['.$index.']')
87
                        ->addViolation();
88
89
                    continue;
90
                }
91
92
                foreach (['scope', 'permissions'] as $attribute) {
93
                    if (!array_key_exists($attribute, $permission)) {
94
                        $this->context
95
                            ->buildViolation($constraint->permissionAttributeMissing)
96
                            ->setParameter('{{ service }}', '"'.$service.'"')
97
                            ->setParameter('{{ attribute }}', '"'.$attribute.'"')
98
                            ->atPath('permissions.'.$service.'['.$index.'].'.$attribute)
99
                            ->addViolation();
100
                    }
101
102
                    if ('scope' === $attribute) {
103
                        if (!is_array($permission['scope'])) {
104
                            $this->context
105
                                ->buildViolation($constraint->subscopeNotArray)
106
                                ->setParameter('{{ service }}', '"'.$service.'"')
107
                                ->atPath('scope.'.$service.'['.$index.'].permissions')
108
                                ->addViolation();
109
110
                            continue;
111
                        }
112
113
                        foreach (['type', 'entity', 'entityUuid'] as $subattribute) {
114
                            if (!array_key_exists($subattribute, $permission['scope'])) {
115
                                $this->context
116
                                    ->buildViolation($constraint->subscopeAttributeMissing)
117
                                    ->setParameter('{{ service }}', '"'.$service.'"')
118
                                    ->setParameter('{{ attribute }}', '"'.$subattribute.'"')
119
                                    ->atPath('permissions.'.$service.'['.$index.'].'.$attribute.'.'.$subattribute)
120
                                    ->addViolation();
121
122
                                continue;
123
                            }
124
                        }
125
                    } else if ('permissions' === $attribute) {
126
                        if (!is_array($permission['permissions'])) {
127
                            $this->context
128
                                ->buildViolation($constraint->subpermissionsNotArray)
129
                                ->setParameter('{{ service }}', '"'.$service.'"')
130
                                ->atPath('permissions.'.$service.'['.$index.'].permissions')
131
                                ->addViolation();
132
133
                            continue;
134
                        }
135
136
                        foreach ($permission['permissions'] as $subindex => $subpermission) {
137
                            if (!is_array($subpermission)) {
138
                                $this->context
139
                                    ->buildViolation($constraint->subpermissionNotObject)
140
                                    ->setParameter('{{ service }}', '"'.$service.'"')
141
                                    ->atPath('permissions.'.$service.'['.$index.'].permissions['.$subindex.']')
142
                                    ->addViolation();
143
144
                                continue;
145
                            }
146
147
                            foreach (['key', 'attributes'] as $subattribute) {
148
                                if (!array_key_exists($subattribute, $subpermission)) {
149
                                    $this->context
150
                                        ->buildViolation($constraint->subpermissionAttributeMissing)
151
                                        ->setParameter('{{ service }}', '"'.$service.'"')
152
                                        ->setParameter('{{ attribute }}', '"'.$subattribute.'"')
153
                                        ->atPath('permissions.'.$service.'['.$index.'].'.$attribute.'['.$subindex.'].'.$subattribute)
154
                                        ->addViolation();
155
156
                                    continue;
157
                                }
158
159
                                if ('attributes' === $subattribute) {
160
                                    foreach ($subpermission['attributes'] as $item) {
161
                                        if (!in_array($item, [Permission::BROWSE, Permission::READ, Permission::EDIT, Permission::ADD, Permission::DELETE, Permission::EXECUTE])) {
162
                                            $this->context
163
                                                ->buildViolation($constraint->subpermissionUndefined)
164
                                                ->setParameter('{{ service }}', '"'.$service.'"')
165
                                                ->setParameter('{{ attribute }}', '"'.$item.'"')
166
                                                ->atPath('permissions.'.$service.'['.$index.'].'.$attribute.'['.$subindex.'].attributes')
167
                                                ->addViolation();
168
                                        }
169
                                    }
170
                                }
171
                            }
172
                        }
173
                    }
174
                }
175
            }
176
        }
177
    }
178
}
179