Passed
Pull Request — master (#47)
by Lloric Mayuga
03:15
created

UpdateUsersTimezone   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 121
rs 10
wmc 20

4 Methods

Rating   Name   Duplication   Size   Complexity  
B notify() 0 36 7
A handle() 0 27 6
A getFromLookup() 0 17 4
A lookup() 0 12 3
1
<?php
2
3
namespace JamesMills\LaravelTimezone\Listeners\Auth;
4
5
use Illuminate\Auth\Events\Login;
6
use Torann\GeoIP\Location;
7
8
class UpdateUsersTimezone
9
{
10
11
    /**
12
     * Handle the event.
13
     *
14
     * @return void
15
     */
16
    public function handle($event)
17
    {
18
        $user = null;
19
20
        /**
21
         * If the event is Login, we get the user from the web guard.
22
         */
23
        if ($event instanceof Login) {
24
            $user = app('auth')->user();
25
        }
26
27
        /**
28
         * If no user is found, we just return. Nothing to do here.
29
         */
30
        if (is_null($user)) {
31
            return;
32
        }
33
34
        $ip = $this->getFromLookup();
35
        $geoip_info = geoip()->getLocation($ip);
36
37
        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...
38
            if (config('timezone.overwrite') == true || $user->timezone == null) {
39
                $user->timezone = $geoip_info['timezone'];
40
                $user->save();
41
42
                $this->notify($geoip_info);
43
            }
44
        }
45
    }
46
47
    /**
48
     * @param  Location  $geoip_info
49
     */
50
    private function notify(Location $geoip_info)
51
    {
52
        if (config('timezone.flash') == 'off') {
53
            return;
54
        }
55
56
        $message = 'We have set your timezone to '.$geoip_info['timezone'];
57
58
        if (config('timezone.flash') == 'laravel') {
59
            request()->session()->flash('success', $message);
60
61
            return;
62
        }
63
64
        if (config('timezone.flash') == 'laracasts') {
65
            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

65
            /** @scrutinizer ignore-call */ 
66
            flash()->success($message);
Loading history...
66
67
            return;
68
        }
69
70
        if (config('timezone.flash') == 'mercuryseries') {
71
            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

71
            /** @scrutinizer ignore-call */ 
72
            flashy()->success($message);
Loading history...
72
73
            return;
74
        }
75
76
        if (config('timezone.flash') == 'spatie') {
77
            flash()->success($message);
78
79
            return;
80
        }
81
82
        if (config('timezone.flash') == 'mckenziearts') {
83
            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

83
            /** @scrutinizer ignore-call */ 
84
            notify()->success($message);
Loading history...
84
85
            return;
86
        }
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    private function getFromLookup()
93
    {
94
        $result = null;
95
96
        foreach (config('timezone.lookup') as $type => $keys) {
97
            if (empty($keys)) {
98
                continue;
99
            }
100
101
            $result = $this->lookup($type, $keys);
102
103
            if (is_null($result)) {
104
                continue;
105
            }
106
        }
107
108
        return $result;
109
    }
110
111
    /**
112
     * @param $type
113
     * @param $keys
114
     *
115
     * @return string|null
116
     */
117
    private function lookup($type, $keys)
118
    {
119
        $value = null;
120
121
        foreach ($keys as $key) {
122
            if (!request()->$type->has($key)) {
123
                continue;
124
            }
125
            $value = request()->$type->get($key);
126
        }
127
128
        return $value;
129
    }
130
}
131