PassportAccount::hasScope()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Authentication;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Passport\PassportAccountInterface;
22
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface;
23
use function array_key_exists;
24
use function assert;
25
use function is_int;
26
use function is_string;
27
28
/**
29
 * @package Limoncello\Passport
30
 */
31
class PassportAccount implements PassportAccountInterface
32
{
33
    /**
34
     * @var array
35
     */
36
    private $properties;
37
38
    /**
39
     * @var DatabaseSchemaInterface
40
     */
41
    private $schema;
42
43
    /**
44
     * @var bool|string
45
     */
46
    private $userIdentityKey = false;
47
48
    /**
49
     * @var bool|string
50
     */
51
    private $clientIdentityKey = false;
52
53
    /**
54
     * @var bool|string
55
     */
56
    private $scopesKey = false;
57
58
    /**
59
     * @param DatabaseSchemaInterface $schema
60
     * @param array                   $properties
61
     */
62 2
    public function __construct(DatabaseSchemaInterface $schema, array $properties = [])
63
    {
64 2
        $this->schema = $schema;
65 2
        $this->setPassportProperties($properties);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 2
    public function setPassportProperties(array $properties): PassportAccountInterface
72
    {
73 2
        $this->properties = $properties;
74
75 2
        return $this;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 1
    public function hasProperty($key): bool
82
    {
83 1
        assert(is_string($key) || is_int($key));
84
85 1
        return array_key_exists($key, $this->properties);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 1
    public function getProperty($key)
92
    {
93 1
        assert($this->hasProperty($key));
94
95 1
        $value = $this->properties[$key];
96
97 1
        return $value;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 1
    public function hasUserIdentity(): bool
104
    {
105 1
        return $this->hasProperty($this->getUserIdentityKey());
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 1
    public function getUserIdentity()
112
    {
113 1
        return $this->getProperty($this->getUserIdentityKey());
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119 1
    public function hasClientIdentity(): bool
120
    {
121 1
        return $this->hasProperty($this->getClientIdentityKey());
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 1
    public function getClientIdentity()
128
    {
129 1
        return $this->getProperty($this->getClientIdentityKey());
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135 1
    public function hasScopes(): bool
136
    {
137 1
        return $this->hasProperty($this->getScopesKey());
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143 1
    public function getScopes(): array
144
    {
145 1
        return $this->getProperty($this->getScopesKey());
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151 1
    public function hasScope(string $scope): bool
152
    {
153 1
        return $this->hasScopes() === true && in_array($scope, $this->getScopes()) === true;
154
    }
155
156
    /**
157
     * @return string
158
     */
159 1
    protected function getUserIdentityKey(): string
160
    {
161 1
        if ($this->userIdentityKey === false) {
162 1
            $this->userIdentityKey = $this->getSchema()->getTokensUserIdentityColumn();
163
        }
164
165 1
        return $this->userIdentityKey;
166
    }
167
168
    /**
169
     * @return string
170
     */
171 1
    protected function getClientIdentityKey(): string
172
    {
173 1
        if ($this->clientIdentityKey === false) {
174 1
            $this->clientIdentityKey = $this->getSchema()->getTokensClientIdentityColumn();
175
        }
176
177 1
        return $this->clientIdentityKey;
178
    }
179
180
    /**
181
     * @return string
182
     */
183 1
    protected function getScopesKey(): string
184
    {
185 1
        if ($this->scopesKey === false) {
186 1
            $this->scopesKey = $this->getSchema()->getTokensViewScopesColumn();
187
        }
188
189 1
        return $this->scopesKey;
190
    }
191
192
    /**
193
     * @return DatabaseSchemaInterface
194
     */
195 1
    protected function getSchema(): DatabaseSchemaInterface
196
    {
197 1
        return $this->schema;
198
    }
199
}
200