Passed
Push — master ( 85a2d4...167ff7 )
by Dāvis
04:44
created

ValidatorChain::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 2
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Openidconnect\Specification;
4
5
use Lcobucci\JWT\Token;
0 ignored issues
show
Bug introduced by
The type Lcobucci\JWT\Token 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
7
class ValidatorChain
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $validators = [];
13
14
    /**
15
     * @var array
16
     */
17
    protected $messages = [];
18
19
    /**
20
     * @param AbstractSpecification[] $validators
21
     *
22
     * @return $this
23
     */
24
    public function setValidators(array $validators)
25
    {
26
        $this->validators = [];
27
28
        foreach ($validators as $validator) {
29
            $this->addValidator($validator);
30
        }
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param AbstractSpecification $validator
37
     *
38
     * @return $this
39
     * @internal param string $claim
40
     */
41
    public function addValidator(AbstractSpecification $validator)
42
    {
43
        $this->validators[$validator->getName()] = $validator;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param array $data
50
     * @param Token $token
51
     *
52
     * @return bool
53
     * @throws \OutOfBoundsException
54
     */
55
    public function validate(array $data, Token $token)
56
    {
57
        $valid = true;
58
        foreach ($this->validators as $claim => $validator) {
59
            if ($token->hasClaim($claim) === false) {
60
                if ($validator->isRequired()) {
61
                    $valid = false;
62
                    $this->messages[$claim] = sprintf('Missing required value for claim %s', $claim);
63
                }
64
            } else {
65
                if (isset($data[$claim]) && !$validator->isSatisfiedBy($data[$claim], $token->getClaim($claim))) {
66
                    $valid = false;
67
                    $this->messages[$claim] = $validator->getMessage();
68
                }
69
            }
70
        }
71
72
        return $valid;
73
    }
74
75
    /**
76
     * @param $name
77
     *
78
     * @return bool
79
     */
80
    public function hasValidator($name)
81
    {
82
        return array_key_exists($name, $this->validators);
83
    }
84
85
    /**
86
     * @param $name
87
     *
88
     * @return mixed
89
     */
90
    public function getValidator($name)
91
    {
92
        return $this->validators[$name];
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getMessages()
99
    {
100
        return $this->messages;
101
    }
102
}
103