Manager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
67
        return $user;
68
    }
69
70
    /**
71
     * Logout the current session
72
     *
73
     * @author Ronan Chilvers <[email protected]>
74
     */
75
    public function logout()
76
    {
77
        $this->session->delete(
78
            static::SESSION_KEY
79
        );
80
    }
81
82
    /**
83
     * Is a user logged in?
84
     *
85
     * @return boolean
86
     * @author Ronan Chilvers <[email protected]>
87
     */
88
    public function hasLogin()
89
    {
90
        return $this->session->has(
91
            static::SESSION_KEY
92
        );
93
    }
94
95
    /**
96
     * Refresh the session data
97
     *
98
     * @param \App\Model\User $user
99
     * @author Ronan Chilvers <[email protected]>
100
     */
101
    public function refresh(User $user)
102
    {
103
        if (!$this->hasLogin()) {
104
            return false;
105
        }
106
        $session = $this->session->get(
107
            static::SESSION_KEY
108
        );
109
        if ($user->id !== $session['id']) {
110
            return false;
111
        }
112
        $session['name'] = $user->name;
113
        $session['email'] = $user->email;
114
        $this->session->set(
115
            static::SESSION_KEY,
116
            $session
117
        );
118
119
        return true;
120
    }
121
122
    /**
123
     * Get the current user id
124
     *
125
     * @return integer
126
     * @author Ronan Chilvers <[email protected]>
127
     */
128
    public function id()
129
    {
130
        if (!$this->hasLogin()) {
131
            return null;
132
        }
133
        $session = $this->session->get(
134
            static::SESSION_KEY
135
        );
136
137
        return $session['id'];
138
    }
139
140
    /**
141
     * Get the current logger in email
142
     *
143
     * @return null|string
144
     * @author Ronan Chilvers <[email protected]>
145
     */
146
    public function name()
147
    {
148
        if (!$this->hasLogin()) {
149
            return null;
150
        }
151
        $session = $this->session->get(
152
            static::SESSION_KEY
153
        );
154
155
        return $session['name'];
156
    }
157
158
    /**
159
     * Get the current logger in email
160
     *
161
     * @return null|string
162
     * @author Ronan Chilvers <[email protected]>
163
     */
164
    public function email()
165
    {
166
        if (!$this->hasLogin()) {
167
            return null;
168
        }
169
        $session = $this->session->get(
170
            static::SESSION_KEY
171
        );
172
173
        return $session['email'];
174
    }
175
176
    /**
177
     * Get the currently logged in user
178
     *
179
     * @return null|\App\Model\User
180
     * @author Ronan Chilvers <[email protected]>
181
     */
182
    public function user()
183
    {
184
        if ($this->user instanceof User) {
185
            return $this->user;
186
        }
187
        if (!$this->hasLogin()) {
188
            return null;
189
        }
190
        $session = $this->session->get(
191
            static::SESSION_KEY
192
        );
193
        $user = Orm::finder(User::class)->one(
194
            $session['id']
195
        );
196
        if ($user instanceof User) {
197
            $this->user = $user;
198
199
            return $user;
200
        }
201
202
        return null;
203
    }
204
}
205