Completed
Push — master ( d6b1d0...65a0d7 )
by Marceau
02:17
created

AuthCheckerSubscriber::onUserLoginFailed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Lab404\AuthChecker\Subscribers;
4
5
use Illuminate\Auth\Events\Failed;
6
use Illuminate\Auth\Events\Lockout;
7
use Illuminate\Auth\Events\Login;
8
use Illuminate\Events\Dispatcher;
9
use Lab404\AuthChecker\Services\AuthChecker;
10
11
class AuthCheckerSubscriber
12
{
13
    /**
14
     * @param   Dispatcher $events
15
     * @return  void
16
     */
17
    public function subscribe($events)
18
    {
19
        $events->listen(Login::class, [$this, 'onUserLogin']);
20
        $events->listen(Failed::class, [$this, 'onUserLoginFailed']);
21
        $events->listen(Lockout::class, [$this, 'onUserLoginLockout']);
22
    }
23
24
    /**
25
     * @param   Login $event
26
     * @return  void
27
     */
28
    public function onUserLogin(Login $event)
29
    {
30
        /** @var AuthChecker $manager */
31
        $manager = app('authchecker');
32
        $manager->handleLogin($event->user);
33
    }
34
35
    /**
36
     * @param   Failed $event
37
     * @return  void
38
     */
39
    public function onUserLoginFailed(Failed $event)
40
    {
41
        $user = $event->user;
42
43
        if (!is_null($user)) {
44
            /** @var AuthChecker $manager */
45
            $manager = app('authchecker');
46
            $manager->handleFailed($event->user);
0 ignored issues
show
Bug introduced by
It seems like $event->user can be null; however, handleFailed() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
47
        }
48
    }
49
50
    /**
51
     * @param   Lockout $event
52
     * @return  void
53
     */
54
    public function onUserLoginLockout(Lockout $event)
55
    {
56
        $payload = $event->request->all();
57
58
        if (!empty($payload)) {
59
            /** @var AuthChecker $manager */
60
            $manager = app('authchecker');
61
            $manager->handleLockout($payload);
62
        }
63
    }
64
}
65