Completed
Push — master ( 801091...de4e4b )
by Mārtiņš
02:04
created

Identity::getAccountId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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