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

src/Listeners/LogSuccessfulLogin.php (1 issue)

Labels
Severity
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();
43
        $newUser = $user->created_at->diffInMinutes(Carbon::now()) < 1;
0 ignored issues
show
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));
55
        }
56
    }
57
}
58