Passed
Pull Request — master (#50)
by Ronan
03:54
created

Manager::name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace App\Security;
4
5
use App\Model\User;
6
use Ronanchilvers\Orm\Orm;
7
use Ronanchilvers\Sessions\Session;
8
9
/**
10
 * Manager responsible for managing security / login data
11
 *
12
 * @author Ronan Chilvers <[email protected]>
13
 */
14
class Manager
15
{
16
    const SESSION_KEY = 'security.session';
17
18
    /**
19
     * @var \Ronanchilvers\Sessions\Session
20
     */
21
    protected $session;
22
23
    /**
24
     * @var null|\App\Model\User
25
     */
26
    protected $user;
27
28
    /**
29
     * Class constructor
30
     *
31
     * @author Ronan Chilvers <[email protected]>
32
     */
33
    public function __construct(Session $session)
34
    {
35
        $this->session = $session;
36
    }
37
38
    /**
39
     * Log a user in using an email address and password
40
     *
41
     * @param string $email
42
     * @param string $password
43
     * @return boolean|\App\Model\User $user
44
     * @author Ronan Chilvers <[email protected]>
45
     */
46
    public function login($email, $password)
47
    {
48
        $user = Orm::finder(User::class)->select()
49
            ->where(User::prefix('email'), $email)
50
            ->where(User::prefix('status'), User::STATUS_ACTIVE)
51
            ->one();
52
        if (!$user instanceof User) {
53
            return false;
54
        }
55
        if (!$user->verify($password)) {
56
            return false;
57
        }
58
        $this->session->set(
59
            static::SESSION_KEY,
60
            [
61
                'id' => $user->id,
62
                'name' => $user->name,
63
                'email' => $user->email,
64
            ]
65
        );
66
        $user->recordLogin();
67
68
        return $user;
69
    }
70
71
    /**
72
     * Logout the current session
73
     *
74
     * @author Ronan Chilvers <[email protected]>
75
     */
76
    public function logout()
77
    {
78
        $this->session->delete(
79
            static::SESSION_KEY
80
        );
81
    }
82
83
    /**
84
     * Is a user logged in?
85
     *
86
     * @return boolean
87
     * @author Ronan Chilvers <[email protected]>
88
     */
89
    public function hasLogin()
90
    {
91
        return $this->session->has(
92
            static::SESSION_KEY
93
        );
94
    }
95
96
    /**
97
     * Refresh the session data
98
     *
99
     * @param \App\Model\User $user
100
     * @author Ronan Chilvers <[email protected]>
101
     */
102
    public function refresh(User $user)
103
    {
104
        if (!$this->hasLogin()) {
105
            return false;
106
        }
107
        $session = $this->session->get(
108
            static::SESSION_KEY
109
        );
110
        if ($user->id !== $session['id']) {
111
            return false;
112
        }
113
        $session['name'] = $user->name;
114
        $session['email'] = $user->email;
115
        $this->session->set(
116
            static::SESSION_KEY,
117
            $session
118
        );
119
120
        return true;
121
    }
122
123
    /**
124
     * Get the current user id
125
     *
126
     * @return integer
127
     * @author Ronan Chilvers <[email protected]>
128
     */
129
    public function id()
130
    {
131
        if (!$this->hasLogin()) {
132
            return null;
133
        }
134
        $session = $this->session->get(
135
            static::SESSION_KEY
136
        );
137
138
        return $session['id'];
139
    }
140
141
    /**
142
     * Get the current logger in email
143
     *
144
     * @return null|string
145
     * @author Ronan Chilvers <[email protected]>
146
     */
147
    public function name()
148
    {
149
        if (!$this->hasLogin()) {
150
            return null;
151
        }
152
        $session = $this->session->get(
153
            static::SESSION_KEY
154
        );
155
156
        return $session['name'];
157
    }
158
159
    /**
160
     * Get the current logger in email
161
     *
162
     * @return null|string
163
     * @author Ronan Chilvers <[email protected]>
164
     */
165
    public function email()
166
    {
167
        if (!$this->hasLogin()) {
168
            return null;
169
        }
170
        $session = $this->session->get(
171
            static::SESSION_KEY
172
        );
173
174
        return $session['email'];
175
    }
176
177
    /**
178
     * Get the currently logged in user
179
     *
180
     * @return null|\App\Model\User
181
     * @author Ronan Chilvers <[email protected]>
182
     */
183
    public function user()
184
    {
185
        if ($this->user instanceof User) {
186
            return $this->user;
187
        }
188
        if (!$this->hasLogin()) {
189
            return null;
190
        }
191
        $session = $this->session->get(
192
            static::SESSION_KEY
193
        );
194
        $user = Orm::finder(User::class)->one(
195
            $session['id']
196
        );
197
        if ($user instanceof User) {
198
            $this->user = $user;
199
200
            return $user;
201
        }
202
203
        return null;
204
    }
205
206
    /**
207
     * Is a given user currently logged in?
208
     *
209
     * @param \App\Model\User $user
210
     * @return bool
211
     * @author Ronan Chilvers <[email protected]>
212
     */
213
    public function isCurrent(User $user): bool
214
    {
215
        return $user->id == $this->id();
216
    }
217
}
218