Passed
Push — master ( 5d7f9f...66d93b )
by Yaakov
01:43 queued 10s
created

LogSuccessfulLogin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Yadahan\AuthenticationLog\Listeners;
4
5
use Illuminate\Auth\Events\Login;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Carbon;
8
use Yadahan\AuthenticationLog\AuthenticationLog;
9
use Yadahan\AuthenticationLog\Notifications\NewDevice;
10
11
class LogSuccessfulLogin
12
{
13
    /**
14
     * The request.
15
     *
16
     * @var \Illuminate\Http\Request
17
     */
18
    public $request;
19
20
    /**
21
     * Create the event listener.
22
     *
23
     * @param  \Illuminate\Http\Request  $request
24
     * @return void
25
     */
26
    public function __construct(Request $request)
27
    {
28
        $this->request = $request;
29
    }
30
31
    /**
32
     * Handle the event.
33
     *
34
     * @param  Login  $event
35
     * @return void
36
     */
37
    public function handle(Login $event)
38
    {
39
        $user = $event->user;
40
        $ip = $this->request->ip();
41
        $userAgent = $this->request->userAgent();
42
        $known = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->first();
0 ignored issues
show
Bug introduced by
The method authentications() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $known = $user->/** @scrutinizer ignore-call */ authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->first();
Loading history...
43
        $newUser = $user->created_at->diffInMinutes(Carbon::now()) < 1;
0 ignored issues
show
Bug introduced by
Accessing created_at on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
44
45
        $authenticationLog = new AuthenticationLog([
46
            'ip_address' => $ip,
47
            'user_agent' => $userAgent,
48
            'login_at' => Carbon::now(),
49
        ]);
50
51
        $user->authentications()->save($authenticationLog);
52
53
        if (! $known && ! $newUser && config('authentication-log.notify')) {
54
            $user->notify(new NewDevice($authenticationLog));
0 ignored issues
show
Bug introduced by
The method notify() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $user->/** @scrutinizer ignore-call */ 
55
                   notify(new NewDevice($authenticationLog));
Loading history...
55
        }
56
    }
57
}
58