Passed
Pull Request — develop (#54)
by Mario
01:35
created

ValidValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 94
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 94
rs 9.568

How to fix   Long Method   

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 JsonSchema\Validator;
0 ignored issues
show
Bug introduced by
The type JsonSchema\Validator 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\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...
9
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...
10
11
/**
12
 * Class ValidValidator
13
 *
14
 * @example
15
 * <code>
16
 * {
17
 *     "identities": [
18
 *         {
19
 *             "scope": {
20
 *                 "type": "owned_by",
21
 *                 "entity": "BusinessUnit",
22
 *                 "entityUuid": "aff1370c-9cb7-432d-a608-57021637f278"
23
 *             },
24
 *             "permissions": [
25
 *                 {
26
 *                     "key": "individual",
27
 *                     "attributes": ["BROWSE", "READ"]
28
 *                 },
29
 *                 {
30
 *                     "key": "individual_uuid",
31
 *                     "attributes": ["BROWSE", "READ"]
32
 *                 }
33
 *             ]
34
 *         }
35
 *     ]
36
 * }
37
 * </code>
38
 */
39
final class ValidValidator extends ConstraintValidator
40
{
41
    /**
42
     * @var \Ds\Component\Api\Collection\ServiceCollection
43
     */
44
    protected $serviceCollection;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param \Ds\Component\Api\Collection\ServiceCollection $serviceCollection
50
     */
51
    public function __construct(ServiceCollection $serviceCollection)
52
    {
53
        $this->serviceCollection = $serviceCollection;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function validate($role, Constraint $constraint)
60
    {
61
        $validator = new Validator;
62
        $property = '
63
            {
64
                "type": "array",
65
                "items": {
66
                    "type": "array",
67
                    "additionalProperties": false,
68
                    "required": ["scope", "permissions"],
69
                    "properties": {
70
                        "scope": {
71
                            "type": "array",
72
                            "additionalProperties": false,
73
                            "properties": {
74
                                "operator": {
75
                                    "type": "string",
76
                                    "enum": ["and", "or"]
77
                                },
78
                                "conditions": {
79
                                    "type": "array"
80
                                },
81
                                "type": {
82
                                    "type": "string",
83
                                    "enum": ["generic", "object", "identity", "owner", "session", "property"]
84
                                },
85
                                "entity": {
86
                                    "type": "string"
87
                                },
88
                                "entityUuid": {
89
                                    "type": ["string", "null"]
90
                                },
91
                                "property": {
92
                                    "type": "string"
93
                                },
94
                                "comparison": {
95
                                    "type": "string",
96
                                    "enum": ["eq", "neq"]
97
                                },
98
                                "value": {}
99
                            }
100
                        },
101
                        "permissions": {
102
                            "type": "array",
103
                            "items": {
104
                                "type": "array",
105
                                "additionalProperties": false,
106
                                "required": ["key", "attributes"],
107
                                "properties": {
108
                                    "key": {
109
                                        "type": "string"
110
                                    },
111
                                    "attributes": {
112
                                        "type": "array",
113
                                        "items": {
114
                                            "type": "string",
115
                                            "enum": ["BROWSE", "READ", "EDIT", "ADD", "DELETE", "EXECUTE"]
116
                                        }
117
                                    }
118
                                }
119
                            }
120
                        }
121
                    }
122
                }
123
            }
124
        ';
125
        $schema = json_decode('
126
            {
127
                "type": "array",
128
                "additionalProperties": false,
129
                "properties": {
130
                    "assets": ' . $property . ',
131
                    "authentication": ' . $property . ',
132
                    "cases": ' . $property . ',
133
                    "cms": ' . $property . ',
134
                    "forms": ' . $property . ',
135
                    "identities": ' . $property . ',
136
                    "microservice": ' . $property . ',
137
                    "records": ' . $property . ',
138
                    "services": ' . $property . ',
139
                    "tasks": ' . $property . ',
140
                    "tenants": ' . $property . '
141
                }
142
            }
143
        ');
144
        $permissions = $role->getPermissions();
145
        $validator->validate($permissions, $schema);
146
147
        if (!$validator->isValid()) {
148
            foreach ($validator->getErrors() as $error) {
149
                $this->context
150
                    ->buildViolation($error['message'])
151
                    ->atPath('permissions.' . $error['property'])
152
                    ->addViolation();
153
            }
154
        }
155
    }
156
}
157