NativeSessionGuard::user()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 24
ccs 10
cts 11
cp 0.9091
rs 9.9332
cc 4
nc 4
nop 0
crap 4.0119
1
<?php declare(strict_types=1);
2
3
namespace CustomerGauge\Session;
4
5
use Illuminate\Auth\Events\Authenticated;
6
use Illuminate\Auth\GuardHelpers;
7
use Illuminate\Contracts\Auth\Guard;
8
use Illuminate\Contracts\Events\Dispatcher;
9
10
final class NativeSessionGuard implements Guard
11
{
12
    use GuardHelpers;
13
14
    private $session;
15
16
    private $dispatcher;
17
18 2
    public function __construct(NativeSessionUserProvider $provider, SessionRetriever $session, Dispatcher $dispatcher)
19
    {
20 2
        $this->provider = $provider;
21 2
        $this->session = $session;
22 2
        $this->dispatcher = $dispatcher;
23
    }
24
25 2
    public function user()
26
    {
27 2
        if (! is_null($this->user)) {
28
            return $this->user;
29
        }
30
31 2
        $session = $this->session->retrieve();
32
33
        // Laravel implements a Chain of Responsibility on the Authentication process.
34
        // If this Guard cannot authenticate, we must return null to give room for
35
        // other Guards to attempt to authenticate the current request.
36 2
        if (! $session) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $session of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37 1
            return null;
38
        }
39
40 1
        $user = $this->provider->retrieveByCredentials($session);
41
42 1
        if ($user) {
43 1
            $this->dispatcher->dispatch(new Authenticated('php-native-session', $user));
44
45 1
            $this->user = $user;
46
        }
47
48 1
        return $this->user;
49
    }
50
51
    public function validate(array $credentials = [])
52
    {
53
        return (bool) $this->provider->retrieveByCredentials($_SESSION);
54
    }
55
}
56