Passed
Push — master ( 84c77a...d6297e )
by Yaakov
03:36
created

LogSuccessfulLogout::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Yadahan\AuthenticationLog\Listeners;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Carbon;
7
use Illuminate\Auth\Events\Logout;
8
use Yadahan\AuthenticationLog\AuthenticationLog;
9
10
class LogSuccessfulLogout
11
{
12
    /**
13
     * The request.
14
     *
15
     * @var \Illuminate\Http\Request
16
     */
17
    public $request;
18
19
    /**
20
     * Create the event listener.
21
     *
22
     * @param  \Illuminate\Http\Request  $request
23
     * @return void
24
     */
25
    public function __construct(Request $request)
26
    {
27
        $this->request = $request;
28
    }
29
30
    /**
31
     * Handle the event.
32
     *
33
     * @param  Logout  $event
34
     * @return void
35
     */
36
    public function handle(Logout $event)
37
    {
38
        if ($event->user) {
39
            $user = $event->user;
40
            $ip = $this->request->ip();
41
            $userAgent = $this->request->userAgent();
42
            $authenticationLog = $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
            $authenticationLog = $user->/** @scrutinizer ignore-call */ authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->first();
Loading history...
43
44
            if (! $authenticationLog) {
45
                $authenticationLog = new AuthenticationLog([
46
                    'ip_address' => $ip,
47
                    'user_agent' => $userAgent,
48
                ]);
49
            }
50
51
            $authenticationLog->logout_at = Carbon::now();
52
53
            $user->authentications()->save($authenticationLog);
54
        }
55
    }
56
}
57