UpdateUsersTimezone   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 23
eloc 51
c 2
b 0
f 0
dl 0
loc 139
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 41 7
B notify() 0 42 9
A lookup() 0 12 3
A getFromLookup() 0 17 4
1
<?php
2
3
namespace JamesMills\LaravelTimezone\Listeners\Auth;
4
5
use Illuminate\Auth\Events\Login;
6
use Illuminate\Support\Facades\Auth;
7
use Laravel\Passport\Events\AccessTokenCreated;
0 ignored issues
show
Bug introduced by
The type Laravel\Passport\Events\AccessTokenCreated 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...
8
use Torann\GeoIP\Location;
9
10
class UpdateUsersTimezone
11
{
12
    /**
13
     * Handle the event.
14
     *
15
     * @return void
16
     */
17
    public function handle($event)
18
    {
19
        $user = null;
20
21
        /**
22
         * If the event is AccessTokenCreated,
23
         * we logged the user and return,
24
         * stopping the execution.
25
         *
26
         * The Auth::loginUsingId dispatches a Login event,
27
         * making this listener be called again.
28
         */
29
        if ($event instanceof AccessTokenCreated) {
30
            Auth::loginUsingId($event->userId);
31
32
            return;
33
        }
34
35
        /**
36
         * If the event is Login, we get the user from the web guard.
37
         */
38
        if ($event instanceof Login) {
39
            $user = Auth::user();
40
        }
41
42
        /**
43
         * If no user is found, we just return. Nothing to do here.
44
         */
45
        if (is_null($user)) {
46
            return;
47
        }
48
49
        $ip = $this->getFromLookup();
50
        $geoip_info = geoip()->getLocation($ip);
51
52
        if ($user->timezone != $geoip_info['timezone']) {
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
            if (config('timezone.overwrite') == true || $user->timezone == null) {
54
                $user->timezone = $geoip_info['timezone'] ?? $geoip_info->time_zone['name'];
55
                $user->save();
56
57
                $this->notify($geoip_info);
58
            }
59
        }
60
    }
61
62
    /**
63
     * @param  Location  $geoip_info
64
     */
65
    private function notify(Location $geoip_info)
66
    {
67
        if (request()->hasSession() && config('timezone.flash') == 'off') {
68
            return;
69
        }
70
71
        $message = sprintf(config('timezone.message', 'We have set your timezone to %s'), $geoip_info['timezone']);
72
73
        if (config('timezone.flash') == 'laravel') {
74
            request()->session()->flash('success', $message);
75
76
            return;
77
        }
78
79
        if (config('timezone.flash') == 'laracasts') {
80
            flash()->success($message);
0 ignored issues
show
Bug introduced by
The function flash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

80
            /** @scrutinizer ignore-call */ 
81
            flash()->success($message);
Loading history...
81
82
            return;
83
        }
84
85
        if (config('timezone.flash') == 'mercuryseries') {
86
            flashy()->success($message);
0 ignored issues
show
Bug introduced by
The function flashy was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
            /** @scrutinizer ignore-call */ 
87
            flashy()->success($message);
Loading history...
87
88
            return;
89
        }
90
91
        if (config('timezone.flash') == 'spatie') {
92
            flash()->success($message);
93
94
            return;
95
        }
96
97
        if (config('timezone.flash') == 'mckenziearts') {
98
            notify()->success($message);
0 ignored issues
show
Bug introduced by
The function notify was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

98
            /** @scrutinizer ignore-call */ 
99
            notify()->success($message);
Loading history...
99
100
            return;
101
        }
102
103
        if (config('timezone.flash') == 'tall-toasts') {
104
            toast()->success($message)->pushOnNextPage();
0 ignored issues
show
Bug introduced by
The function toast was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

104
            /** @scrutinizer ignore-call */ 
105
            toast()->success($message)->pushOnNextPage();
Loading history...
105
106
            return;
107
        }
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113
    private function getFromLookup()
114
    {
115
        $result = null;
116
117
        foreach (config('timezone.lookup') as $type => $keys) {
118
            if (empty($keys)) {
119
                continue;
120
            }
121
122
            $result = $this->lookup($type, $keys);
123
124
            if (is_null($result)) {
125
                continue;
126
            }
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * @param $type
134
     * @param $keys
135
     * @return string|null
136
     */
137
    private function lookup($type, $keys)
138
    {
139
        $value = null;
140
141
        foreach ($keys as $key) {
142
            if (! request()->$type->has($key)) {
143
                continue;
144
            }
145
            $value = request()->$type->get($key);
146
        }
147
148
        return $value;
149
    }
150
}
151