Completed
Push — master ( d22c34...cd27e7 )
by Mārtiņš
02:11
created

Identity::setParentId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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 $parentId;
39
    private $accountId;
40
    protected $type = self::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
52
53
54 8
    public function setId($identityId)
55
    {
56 8
        $data = (int) $identityId;
57
58 8
        if ($data > 0) {
59 3
            $this->identityId = $data;
60
        }
61 8
    }
62
63
64
    /**
65
     * @codeCoverageIgnore
66
     */
67
    public function getId()
68
    {
69
        return $this->identityId;
70
    }
71
72
73 8
    public function setParentId($parentId)
74
    {
75 8
        $data = (int) $parentId;
76
77 8
        if ($data > 0) {
78 3
            $this->parentId = $data;
79
        }
80 8
    }
81
82
83
    /**
84
     * @codeCoverageIgnore
85
     */
86
    public function getParentId()
87
    {
88
        return $this->parentId;
89
    }
90
91
92 8
    public function setAccountId($accountId)
93
    {
94 8
        $data = (int) $accountId;
95
96 8
        if ($data > 0) {
97 3
            $this->accountId = $data;
98
        }
99 8
    }
100
101
102
    /**
103
     * @codeCoverageIgnore
104
     */
105
    public function getAccountId()
106
    {
107
        return $this->accountId;
108
    }
109
110
111
    /**
112
     * @codeCoverageIgnore
113
     */
114
    public function getType()
115
    {
116
        return $this->type;
117
    }
118
119
120 8
    public function setExpiresOn($expiresOn)
121
    {
122 8
        $data = (int) $expiresOn;
123
124 8
        if ($data > 0) {
125 3
            $this->expiresOn = $data;
126
        }
127 8
    }
128
129
130
    /**
131
     * @codeCoverageIgnore
132
     */
133
    public function getExpiresOn()
134
    {
135
        return $this->expiresOn;
136
    }
137
138
139 1
    public function setStatus($status)
140
    {
141 1
        if ($status !== $this->status) {
142 1
            $this->setStatusChangedOn(time());
143
        }
144
145 1
        $this->status = (int) $status;
146 1
    }
147
148
149
    /**
150
     * @codeCoverageIgnore
151
     */
152
    public function getStatus()
153
    {
154
        return $this->status;
155
    }
156
157
158 9
    public function setStatusChangedOn($timestamp)
159
    {
160 9
        $data = (int) $timestamp;
161
162 9
        if ($data > 0) {
163 4
            $this->statusChangedOn = $data;
164
        }
165 9
    }
166
167
168
    /**
169
     * @codeCoverageIgnore
170
     */
171
    public function getStatusChangedOn()
172
    {
173
        return $this->statusChangedOn;
174
    }
175
176
177 2
    public function setToken($token)
178
    {
179 2
        if ($token !== null && strlen($token) !== 2 * Identity::TOKEN_SIZE) {
180 1
            throw new InvalidToken;
181
        }
182
183 1
        $this->token = $token;
184 1
    }
185
186
187 2
    public function generateToken()
188
    {
189 2
        $this->token = bin2hex(random_bytes(Identity::TOKEN_SIZE));
190 2
    }
191
192
193
    /**
194
     * @codeCoverageIgnore
195
     */
196
    public function getToken()
197
    {
198
        return $this->token;
199
    }
200
201
202 2
    public function setTokenAction($tokenAction)
203
    {
204 2
        $data = (int) $tokenAction;
205
206 2
        if ($data > 0) {
207 2
            $this->tokenAction = $data;
208 2
            return;
209
        }
210
211 1
        $this->tokenAction = null;
212 1
    }
213
214
215
    /**
216
     * @codeCoverageIgnore
217
     */
218
    public function getTokenAction()
219
    {
220
        return $this->tokenAction;
221
    }
222
223
224 8
    public function setTokenEndOfLife($timestamp)
225
    {
226 8
        $data = (int) $timestamp;
227
228 8
        if ($data > 0) {
229 3
            $this->tokenExpiresOn = $data;
230
        }
231 8
    }
232
233
234
    /**
235
     * @codeCoverageIgnore
236
     */
237
    public function getTokenEndOfLife()
238
    {
239
        return $this->tokenExpiresOn;
240
    }
241
242
243 1
    public function clearToken()
244
    {
245 1
        $this->token = null;
246 1
        $this->tokenAction = Identity::ACTION_ANY;
247 1
        $this->tokenExpiresOn = null;
248 1
    }
249
250
251 8
    public function setLastUsed($timestamp)
252
    {
253 8
        $data = (int) $timestamp;
254
255 8
        if ($data > 0) {
256 3
            $this->usedOn = $data;
257
        }
258 8
    }
259
260
261
    /**
262
     * @codeCoverageIgnore
263
     */
264
    public function getLastUsed()
265
    {
266
        return $this->usedOn;
267
    }
268
}
269