Completed
Push — 2.x ( 68e3b1...f83060 )
by Hari
8s
created

Auth::setUserData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 1
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Auth;
10
11
use Aura\Auth\Session\SegmentInterface;
12
use Aura\Auth\Session\SessionInterface;
13
use Aura\Auth\Session\Timer;
14
15
/**
16
 *
17
 * The current user (authenticated or otherwise).
18
 *
19
 * @package Aura.Auth
20
 *
21
 */
22
class Auth
23
{
24
    /**
25
     *
26
     * Session data.
27
     *
28
     * @var SegmentInterface
29
     *
30
     */
31
    protected $segment;
32
33
    /**
34
     *
35
     * Constructor.
36
     *
37
     * @param SegmentInterface $segment A session data store.
38
     *
39
     */
40 14
    public function __construct(SegmentInterface $segment)
41
    {
42 14
        $this->segment = $segment;
43 14
    }
44
45
    /**
46
     *
47
     * Sets the authentication values.
48
     *
49
     * @param string $status The authentication status.
50
     *
51
     * @param int $first_active First active at this Unix time.
52
     *
53
     * @param int $last_active Last active at this Unix time.
54
     *
55
     * @param string $username The username.
56
     *
57
     * @param array $userdata Arbitrary user data.
58
     *
59
     * @return null
60
     *
61
     * @see Status for constants and their values.
62
     *
63
     */
64 8
    public function set(
65
        $status,
66
        $first_active,
67
        $last_active,
68
        $username,
69
        array $userdata
70
    ) {
71 8
        $this->setStatus($status);
72 8
        $this->setFirstActive($first_active);
73 8
        $this->setLastActive($last_active);
74 8
        $this->setUserName($username);
75 8
        $this->setUserData($userdata);
76 8
    }
77
78
    /**
79
     *
80
     * Is the user authenticated?
81
     *
82
     * @return bool
83
     *
84
     */
85 6
    public function isValid()
86
    {
87 6
        return $this->getStatus() == Status::VALID;
88
    }
89
90
    /**
91
     *
92
     * Is the user anonymous?
93
     *
94
     * @return bool
95
     *
96
     */
97 8
    public function isAnon()
98
    {
99 8
        return $this->getStatus() == Status::ANON;
100
    }
101
102
    /**
103
     *
104
     * Has the user been idle for too long?
105
     *
106
     * @return bool
107
     *
108
     */
109 1
    public function isIdle()
110
    {
111 1
        return $this->getStatus() == Status::IDLE;
112
    }
113
114
    /**
115
     *
116
     * Has the authentication time expired?
117
     *
118
     * @return bool
119
     *
120
     */
121 1
    public function isExpired()
122
    {
123 1
        return $this->getStatus() == Status::EXPIRED;
124
    }
125
126
    /**
127
     *
128
     * Sets the current authentication status.
129
     *
130
     * @param string $status The authentication status.
131
     *
132
     * @return null
133
     *
134
     */
135 8
    public function setStatus($status)
136
    {
137 8
        $this->segment->set('status', $status);
138 8
    }
139
140
    /**
141
     *
142
     * Gets the current authentication status.
143
     *
144
     * @return string
145
     *
146
     */
147 10
    public function getStatus()
148
    {
149 10
        return $this->segment->get('status', Status::ANON);
150
    }
151
152
    /**
153
     *
154
     * Sets the initial authentication time.
155
     *
156
     * @param int $first_active The initial authentication Unix time.
157
     *
158
     * @return null
159
     *
160
     */
161 8
    public function setFirstActive($first_active)
162
    {
163 8
        $this->segment->set('first_active', $first_active);
164 8
    }
165
166
    /**
167
     *
168
     * Gets the initial authentication time.
169
     *
170
     * @return int
171
     *
172
     */
173 4
    public function getFirstActive()
174
    {
175 4
        return $this->segment->get('first_active');
176
    }
177
178
    /**
179
     *
180
     * Sets the last active time.
181
     *
182
     * @param int $last_active The last active Unix time.
183
     *
184
     * @return null
185
     *
186
     */
187 9
    public function setLastActive($last_active)
188
    {
189 9
        $this->segment->set('last_active', $last_active);
190 9
    }
191
192
    /**
193
     *
194
     * Gets the last active time.
195
     *
196
     * @return int
197
     *
198
     */
199 4
    public function getLastActive()
200
    {
201 4
        return $this->segment->get('last_active');
202
    }
203
204
    /**
205
     *
206
     * Sets the current user name.
207
     *
208
     * @param string $username The username.
209
     *
210
     * @return null
211
     *
212
     */
213 8
    public function setUserName($username)
214
    {
215 8
        $this->segment->set('username', $username);
216 8
    }
217
218
    /**
219
     *
220
     * Gets the current user name.
221
     *
222
     * @return string
223
     *
224
     */
225 6
    public function getUserName()
226
    {
227 6
        return $this->segment->get('username');
228
    }
229
230
    /**
231
     *
232
     * Sets the current user data.
233
     *
234
     * @param array $userdata The user data.
235
     *
236
     * @return null
237
     *
238
     */
239 8
    public function setUserData(array $userdata)
240
    {
241 8
        $this->segment->set('userdata', $userdata);
242 8
    }
243
244
    /**
245
     *
246
     * Gets the current user data.
247
     *
248
     * @return array
249
     *
250
     */
251 2
    public function getUserData()
252
    {
253 2
        return $this->segment->get('userdata', array());
254
    }
255
}
256