Completed
Push — master ( 38afed...b860d1 )
by Mārtiņš
02:36
created

Identity   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 30
eloc 65
c 2
b 0
f 0
dl 0
loc 245
ccs 45
cts 47
cp 0.9574
rs 10

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenAction() 0 3 1
A getStatus() 0 3 1
A getLastUsed() 0 3 1
A setId() 0 3 1
A setTokenAction() 0 7 2
A setStatusChangedOn() 0 3 1
A setExpiresOn() 0 3 1
A setTokenPayload() 0 3 1
A getParentId() 0 3 1
A setLastUsed() 0 3 1
A getTokenEndOfLife() 0 3 1
A setToken() 0 7 3
A setStatus() 0 7 2
A getToken() 0 3 1
A generateToken() 0 6 2
A getAccountId() 0 3 1
A getExpiresOn() 0 3 1
A getTokenPayload() 0 3 1
A setTokenEndOfLife() 0 3 1
A getStatusChangedOn() 0 3 1
A getType() 0 3 1
A clearToken() 0 6 1
A setAccountId() 0 3 1
A getId() 0 3 1
A setParentId() 0 3 1
1
<?php
2
3
namespace Palladium\Entity;
4
5
/**
6
 * Abstraction, that contains information about user's authentication details
7
 */
8
9
use Palladium\Contract\HasId;
10
use Palladium\Exception\TokenGenerationFailed;
11
use Palladium\Exception\InvalidToken;
12
13
14
class Identity implements HasId
15
{
16
17
    const TOKEN_SIZE = 16;
18
19
    const ACTION_NONE = null;
20
    const ACTION_VERIFY = 1;
21
    const ACTION_RESET = 2;
22
    const ACTION_UPDATE = 4;
23
24
    const STATUS_ANY = null;
25
    const STATUS_NEW = 1; // not verified user
26
    const STATUS_ACTIVE = 2; // this is the "good" state
27
    const STATUS_DISCARDED = 4; // user logged out or changed password
28
    const STATUS_BLOCKED = 8; // someone tried to us an invalid auth cookie
29
    const STATUS_EXPIRED = 16;
30
31
    const TYPE_ANY = null;
32
    const TYPE_STANDARD = 1;
33
    const TYPE_COOKIE = 2;
34
    const TYPE_NONCE = 4;
35
36
37
    private $identityId;
38
    private $parentId;
39
    private $accountId;
40
    protected $type = Identity::TYPE_ANY;
41
42
    private $status;
43
    private $statusChangedOn;
44
45
    private $usedOn;
46
    private $expiresOn;
47
48
    private $token;
49
    private $tokenAction;
50
    private $tokenExpiresOn;
51
    private $tokenPayload;
52
53
54 21
    public function setId(int $identityId)
55
    {
56 21
        $this->identityId = $identityId;
57 21
    }
58
59
60
    /**
61
     * @codeCoverageIgnore
62
     */
63
    public function getId()
64
    {
65
        return $this->identityId;
66
    }
67
68
69 17
    public function setParentId(int $parentId = null)
70
    {
71 17
        $this->parentId = $parentId;
72 17
    }
73
74
75
    /**
76
     * @codeCoverageIgnore
77
     */
78
    public function getParentId()
79
    {
80
        return $this->parentId;
81
    }
82
83
84 21
    public function setAccountId(int $accountId)
85
    {
86 21
        $this->accountId = $accountId;
87 21
    }
88
89
90
    /**
91
     * @codeCoverageIgnore
92
     */
93
    public function getAccountId()
94
    {
95
        return $this->accountId;
96
    }
97
98
99
    /**
100
     * @codeCoverageIgnore
101
     */
102
    public function getType()
103
    {
104
        return $this->type;
105
    }
106
107
108 16
    public function setExpiresOn(int $expiresOn)
109
    {
110 16
        $this->expiresOn = $expiresOn;
111 16
    }
112
113
114
    /**
115
     * @codeCoverageIgnore
116
     */
117
    public function getExpiresOn()
118
    {
119
        return $this->expiresOn;
120
    }
121
122
123 20
    public function setStatus($status)
124
    {
125 20
        if ($status !== $this->status) {
126 20
            $this->setStatusChangedOn(time());
127
        }
128
129 20
        $this->status = (int)$status;
130 20
    }
131
132
133
    /**
134
     * @codeCoverageIgnore
135
     */
136
    public function getStatus()
137
    {
138
        return $this->status;
139
    }
140
141
142 24
    public function setStatusChangedOn(int $timestamp)
143
    {
144 24
        $this->statusChangedOn = $timestamp;
145 24
    }
146
147
148
    /**
149
     * @codeCoverageIgnore
150
     */
151
    public function getStatusChangedOn()
152
    {
153
        return $this->statusChangedOn;
154
    }
155
156
157 18
    public function setToken(string $token = null)
158
    {
159 18
        if ($token !== null && strlen($token) !== 2 * Identity::TOKEN_SIZE) {
160 1
            throw new InvalidToken;
161
        }
162
163 17
        $this->token = $token;
164 17
    }
165
166
167 7
    public function generateToken()
168
    {
169
        try {
170 7
            $this->token = bin2hex(random_bytes(Identity::TOKEN_SIZE));
171
        } catch (\Exception $e) {
172
            throw new TokenGenerationFailed;
173
        }
174 7
    }
175
176
177
    /**
178
     * @codeCoverageIgnore
179
     */
180
    public function getToken()
181
    {
182
        return $this->token;
183
    }
184
185
186 20
    public function setTokenAction(int $tokenAction = null)
187
    {
188 20
        if ($tokenAction < 0) {
189 1
            $tokenAction = Identity::ACTION_NONE;
190
        }
191
192 20
        $this->tokenAction = $tokenAction;
193 20
    }
194
195
196
    /**
197
     * @codeCoverageIgnore
198
     */
199
    public function getTokenAction()
200
    {
201
        return $this->tokenAction;
202
    }
203
204
205 22
    public function setTokenEndOfLife(int $timestamp = null)
206
    {
207 22
        $this->tokenExpiresOn = $timestamp;
208 22
    }
209
210
211
    /**
212
     * @codeCoverageIgnore
213
     */
214
    public function getTokenEndOfLife()
215
    {
216
        return $this->tokenExpiresOn;
217
    }
218
219
220
    /**
221
     * @codeCoverageIgnore
222
     */
223
    public function setTokenPayload(array $tokenPayload = null)
224
    {
225
        $this->tokenPayload = $tokenPayload;
226
    }
227
228
229
    /**
230
     * @codeCoverageIgnore
231
     */
232
    public function getTokenPayload()
233
    {
234
        return $this->tokenPayload;
235
    }
236
237
238 2
    public function clearToken()
239
    {
240 2
        $this->token = null;
241 2
        $this->tokenAction = Identity::ACTION_NONE;
242 2
        $this->tokenExpiresOn = null;
243 2
        $this->tokenPayload = null;
244 2
    }
245
246
247 20
    public function setLastUsed(int $timestamp = null)
248
    {
249 20
        $this->usedOn = $timestamp;
250 20
    }
251
252
253
    /**
254
     * @codeCoverageIgnore
255
     */
256
    public function getLastUsed()
257
    {
258
        return $this->usedOn;
259
    }
260
}
261