LogSuccessfulLogin   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 44
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 18 4
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 Yaakov Dahan
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 = Carbon::parse($user->{$user->getCreatedAtColumn()})->diffInMinutes(Carbon::now()) < 1;
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 Yaakov Dahan
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