ActivateController   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 120
c 1
b 0
f 0
dl 0
loc 224
rs 10
wmc 24

11 Methods

Rating   Name   Duplication   Size   Complexity  
A exceeded() 0 25 2
A getAdminHomeRoute() 0 3 1
A initial() 0 17 2
A resend() 0 35 3
A activate() 0 45 5
A __construct() 0 3 1
A getUserHomeRoute() 0 3 1
A activationRequired() 0 36 4
A getActivationView() 0 3 1
A getActivationRoute() 0 3 1
A activeRedirect() 0 17 3
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Activation;
7
use App\Models\Profile;
8
use App\Traits\ActivationTrait;
9
use App\Traits\CaptureIpTrait;
10
use Auth;
11
use Carbon\Carbon;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Route;
14
use jeremykenedy\LaravelRoles\Models\Role;
15
16
class ActivateController extends Controller
17
{
18
    use ActivationTrait;
0 ignored issues
show
Bug introduced by
The trait App\Traits\ActivationTrait requires the property $email which is not provided by App\Http\Controllers\Auth\ActivateController.
Loading history...
19
20
    private static $userHomeRoute = 'public.home';
21
    private static $adminHomeRoute = 'public.home';
22
    private static $activationView = 'auth.activation';
23
    private static $activationRoute = 'activation-required';
24
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        $this->middleware('auth');
33
    }
34
35
    public static function getUserHomeRoute()
36
    {
37
        return self::$userHomeRoute;
38
    }
39
40
    public static function getAdminHomeRoute()
41
    {
42
        return self::$adminHomeRoute;
43
    }
44
45
    public static function getActivationView()
46
    {
47
        return self::$activationView;
48
    }
49
50
    public static function getActivationRoute()
51
    {
52
        return self::$activationRoute;
53
    }
54
55
    public static function activeRedirect($user, $currentRoute)
56
    {
57
        if ($user->activated) {
58
            Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]);
59
60
            if ($user->isAdmin()) {
61
                return redirect()->route(self::getAdminHomeRoute())
62
                ->with('status', 'info')
63
                ->with('message', trans('auth.alreadyActivated'));
64
            }
65
66
            return redirect()->route(self::getUserHomeRoute())
67
                ->with('status', 'info')
68
                ->with('message', trans('auth.alreadyActivated'));
69
        }
70
71
        return false;
72
    }
73
74
    public function initial()
75
    {
76
        $user = Auth::user();
77
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
        $currentRoute = Route::currentRouteName();
79
80
        $rCheck = $this->activeRedirect($user, $currentRoute);
81
        if ($rCheck) {
82
            return $rCheck;
83
        }
84
85
        $data = [
86
            'email' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
87
            'date'  => $lastActivation->created_at->format('m/d/Y'),
88
        ];
89
90
        return view($this->getActivationView())->with($data);
91
    }
92
93
    public function activationRequired()
94
    {
95
        $user = Auth::user();
96
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
97
        $currentRoute = Route::currentRouteName();
98
99
        $rCheck = $this->activeRedirect($user, $currentRoute);
100
        if ($rCheck) {
101
            return $rCheck;
102
        }
103
104
        if ($user->activated == false) {
0 ignored issues
show
Bug introduced by
Accessing activated on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
105
            $activationsCount = Activation::where('user_id', $user->id)
106
                ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
107
                ->count();
108
109
            if ($activationsCount > config('settings.timePeriod')) {
110
                Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]);
111
112
                $data = [
113
                    'email' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
114
                    'hours' => config('settings.timePeriod'),
115
                ];
116
117
                return view('auth.exceeded')->with($data);
118
            }
119
        }
120
121
        Log::info('Registered attempted to navigate while unactivate. '.$currentRoute.'. ', [$user]);
122
123
        $data = [
124
            'email' => $user->email,
125
            'date'  => $lastActivation->created_at->format('m/d/Y'), //
126
        ];
127
128
        return view($this->getActivationView())->with($data);
129
    }
130
131
    public function activate($token)
132
    {
133
        $user = Auth::user();
134
        $currentRoute = Route::currentRouteName();
135
        $ipAddress = new CaptureIpTrait();
136
        $role = Role::where('name', '=', 'user')->first();
137
        $profile = new Profile();
138
139
        $rCheck = $this->activeRedirect($user, $currentRoute);
140
        if ($rCheck) {
141
            return $rCheck;
142
        }
143
144
        $activation = Activation::where('token', $token)->get()
145
            ->where('user_id', $user->id)
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
146
            ->first();
147
148
        if (empty($activation)) {
149
            Log::info('Registered user attempted to activate with an invalid token: '.$currentRoute.'. ', [$user]);
150
151
            return redirect()->route(self::getActivationRoute())
152
                ->with('error', trans('auth.invalidToken'));
153
        }
154
155
        $user->activated = true;
0 ignored issues
show
Bug introduced by
Accessing activated on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
156
        $user->detachAllRoles();
0 ignored issues
show
Bug introduced by
The method detachAllRoles() 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

156
        $user->/** @scrutinizer ignore-call */ 
157
               detachAllRoles();
Loading history...
157
        $user->attachRole($role);
0 ignored issues
show
Bug introduced by
The method attachRole() 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

157
        $user->/** @scrutinizer ignore-call */ 
158
               attachRole($role);
Loading history...
158
        $user->signup_confirmation_ip_address = $ipAddress->getClientIp();
0 ignored issues
show
Bug introduced by
Accessing signup_confirmation_ip_address on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
159
        $user->profile()->save($profile);
0 ignored issues
show
Bug introduced by
The method profile() 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

159
        $user->/** @scrutinizer ignore-call */ 
160
               profile()->save($profile);
Loading history...
160
        $user->save();
161
162
        $allActivations = Activation::where('user_id', $user->id)->get();
163
        foreach ($allActivations as $anActivation) {
164
            $anActivation->delete();
165
        }
166
167
        Log::info('Registered user successfully activated. '.$currentRoute.'. ', [$user]);
168
169
        if ($user->isAdmin()) {
0 ignored issues
show
Bug introduced by
The method isAdmin() 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

169
        if ($user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
170
            return redirect()->route(self::$getAdminHomeRoute())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $getAdminHomeRoute seems to be never defined.
Loading history...
171
            ->with('success', trans('auth.successActivated'));
172
        }
173
174
        return redirect()->route(self::getUserHomeRoute())
175
            ->with('success', trans('auth.successActivated'));
176
    }
177
178
    public function resend()
179
    {
180
        $user = Auth::user();
181
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Unused Code introduced by
The assignment to $lastActivation is dead and can be removed.
Loading history...
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
182
        $currentRoute = Route::currentRouteName();
183
184
        if ($user->activated == false) {
0 ignored issues
show
Bug introduced by
Accessing activated on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
185
            $activationsCount = Activation::where('user_id', $user->id)
186
                ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
187
                ->count();
188
189
            if ($activationsCount >= config('settings.maxAttempts')) {
190
                Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]);
191
192
                $data = [
193
                    'email' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
194
                    'hours' => config('settings.timePeriod'),
195
                ];
196
197
                return view('auth.exceeded')->with($data);
198
            }
199
200
            $sendEmail = $this->initiateEmailActivation($user);
0 ignored issues
show
Unused Code introduced by
The assignment to $sendEmail is dead and can be removed.
Loading history...
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Http\Controllers\Aut...itiateEmailActivation() does only seem to accept App\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

200
            $sendEmail = $this->initiateEmailActivation(/** @scrutinizer ignore-type */ $user);
Loading history...
201
202
            Log::info('Activation resent to registered user. '.$currentRoute.'. ', [$user]);
203
204
            return redirect()->route(self::getActivationRoute())
205
                ->with('success', trans('auth.activationSent'));
206
        }
207
208
        Log::info('Activated user attempte to navigate to '.$currentRoute.'. ', [$user]);
209
210
        return $this->activeRedirect($user, $currentRoute)
211
            ->with('status', 'info')
212
            ->with('message', trans('auth.alreadyActivated'));
213
    }
214
215
    public function exceeded()
216
    {
217
        $user = Auth::user();
218
        $currentRoute = Route::currentRouteName();
219
        $timePeriod = config('settings.timePeriod');
220
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
221
        $activationsCount = Activation::where('user_id', $user->id)
222
            ->where('created_at', '>=', Carbon::now()->subHours($timePeriod))
223
            ->count();
224
225
        if ($activationsCount >= config('settings.maxAttempts')) {
226
            Log::info('Locked non-activated user attempted to visit '.$currentRoute.'. ', [$user]);
227
228
            $data = [
229
                'hours'     => config('settings.timePeriod'),
230
                'email'     => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
231
                'lastDate'  => $lastActivation->created_at->format('m/d/Y'),
232
            ];
233
234
            return view('auth.exceeded')->with($data);
235
        }
236
237
        return $this->activeRedirect($user, $currentRoute)
238
            ->with('status', 'info')
239
            ->with('message', trans('auth.alreadyActivated'));
240
    }
241
}
242