Completed
Push — master ( 4b9759...190a84 )
by Neomerx
11:49
created

PassportAccount::setPassportProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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