Passed
Pull Request — master (#31)
by Moinuddin
06:21
created

LogOtherDevicesLogout   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 5
A __construct() 0 3 1
1
<?php
2
3
namespace Yadahan\AuthenticationLog\Listeners;
4
5
use Illuminate\Auth\Events\OtherDeviceLogout;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\Events\OtherDeviceLogout was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Carbon;
8
use Yadahan\AuthenticationLog\AuthenticationLog;
9
10
class LogOtherDevicesLogout
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
0 ignored issues
show
Bug introduced by
The type Yadahan\AuthenticationLog\Listeners\Logout was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     * @return void
35
     */
36
    public function handle(OtherDeviceLogout $event)
37
    {
38
        if ($event->user) {
39
            $currentUser = $event->user;
40
            $currentUserIp = $this->request->ip();
41
            $currentUserAgent = $this->request->userAgent();
42
            $currentAuthLog = $currentUser->authentications()->whereIpAddress($currentUserIp)->whereUserAgent($currentUserAgent)->first();
43
44
            if (!$currentAuthLog) {
45
                $currentAuthLog = new AuthenticationLog([
46
                    'ip_address' => $currentUserIp,
47
                    'user_agent' => $currentUserAgent,
48
                ]);
49
            }
50
51
            foreach ($currentUser->authentications as $authLog) {
52
                if ($authLog->id != $currentAuthLog->id) {
53
                    $authLog->logout_at = Carbon::now();
54
                    $authLog->save();
55
                }
56
            }
57
        }
58
    }
59
}
60