Completed
Branch master (53fdb9)
by Mārtiņš
04:03 queued 01:53
created

Identity::getUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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