Passed
Push — master ( f3b330...57677b )
by Rutger
04:01
created

Oauth2OidcScope   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 98.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 50
c 1
b 0
f 0
dl 0
loc 149
rs 10
ccs 53
cts 54
cp 0.9815

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getClaims() 0 3 1
B addClaims() 0 38 8
A getClaim() 0 3 1
A setClaims() 0 4 1
A clearClaims() 0 4 1
A removeClaim() 0 4 1
A getIdentifier() 0 6 2
A setIdentifier() 0 4 1
A addClaim() 0 20 4
A hasClaim() 0 3 1
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\openidconnect\scopes;
4
5
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcClaimInterface;
6
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeInterface;
7
use Yii;
8
use yii\base\BaseObject;
9
use yii\base\InvalidArgumentException;
10
use yii\base\InvalidCallException;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\ArrayHelper;
13
14
class Oauth2OidcScope extends BaseObject implements Oauth2OidcScopeInterface
15
{
16
    /**
17
     * @var string|null
18
     */
19
    protected $_identifier;
20
21
    /**
22
     * @var array
23
     */
24
    protected $_claims;
25
26
    /**
27
     * @inheritDoc
28
     */
29 2
    public function getIdentifier()
30
    {
31 2
        if (empty($this->_identifier)) {
32 1
            throw new InvalidCallException('Trying to get scope identifier without it being set.');
33
        }
34 1
        return $this->_identifier;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40 1
    public function setIdentifier($identifier)
41
    {
42 1
        $this->_identifier = $identifier;
43 1
        return $this;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 1
    public function getClaims()
50
    {
51 1
        return $this->_claims;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 1
    public function setClaims($claims)
58
    {
59 1
        $this->clearClaims();
60 1
        return $this->addClaims($claims);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 3
    public function addClaims($claims)
67
    {
68 3
        foreach ($claims as $claimIdentifier => $claimConfig) {
69 3
            if ($claimConfig instanceof Oauth2OidcClaimInterface) {
70 1
                $this->addClaim($claimConfig);
71 3
            } elseif (is_string($claimConfig)) {
72 1
                if (is_numeric($claimIdentifier)) {
73
                    // e.g. ['claim_identifier']
74 1
                    $this->addClaim($claimConfig);
75
                } else {
76
                    // e.g. ['claim_identifier' => 'determiner']
77 1
                    $this->addClaim([
78 1
                        'identifier' => $claimIdentifier,
79 1
                        'determiner' => $claimConfig,
80
                    ]);
81
                }
82 3
            } elseif (is_array($claimConfig)) {
83 2
                if (is_numeric($claimIdentifier) && !array_key_exists('identifier', $claimConfig)) {
84 1
                    throw new InvalidArgumentException(
85
                        'If an element is an array it should either be declared as an associative element'
86 1
                            . ' or contain an "identifier" key.'
87
                    );
88
                }
89
                // e.g. ['claim' => [...]]
90 1
                $this->addClaim(ArrayHelper::merge(
91
                    [
92 1
                        'identifier' => $claimIdentifier,
93
                    ],
94 1
                    $claimConfig
95
                ));
96
            } else {
97 1
                throw new InvalidArgumentException(
98 1
                    'Elements must either be an array, string or a ' . Oauth2OidcClaimInterface::class
99
                );
100
            }
101
        }
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109 1
    public function clearClaims()
110
    {
111 1
        $this->_claims = [];
112 1
        return $this;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118 2
    public function getClaim($claimIdentifier)
119
    {
120 2
        return $this->_claims[$claimIdentifier] ?? null;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126 2
    public function addClaim($claim)
127
    {
128 2
        if (is_string($claim)) {
129 2
            $claim = ['identifier' => $claim];
130
        }
131 2
        if (is_array($claim)) {
132 2
            $claim = Yii::createObject(ArrayHelper::merge(
133
                [
134 2
                    'class' => Oauth2OidcClaimInterface::class,
135
                ],
136 2
                $claim
137
            ));
138
        }
139
140 2
        $identifier = $claim->getIdentifier();
141 2
        if (empty($identifier)) {
142
            throw new InvalidArgumentException('Claim identifier must be set.');
143
        }
144 2
        $this->_claims[$identifier] = $claim;
145 2
        return $this;
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151 1
    public function removeClaim($claimIdentifier)
152
    {
153 1
        unset($this->_claims[$claimIdentifier]);
154 1
        return $this;
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160 2
    public function hasClaim($claimIdentifier)
161
    {
162 2
        return array_key_exists($claimIdentifier, $this->_claims);
163
    }
164
}
165