GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CheckIsUserActivated::handle()   C
last analyzed

Complexity

Conditions 13
Paths 24

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 36
nc 24
nop 2
dl 0
loc 59
rs 6.4268
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Models\Activation;
6
use Auth;
7
use Carbon\Carbon;
8
use Closure;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Route;
12
13
class CheckIsUserActivated
14
{
15
    /**
16
     * Handle an incoming request.
17
     *
18
     * @param \Illuminate\Http\Request $request
19
     * @param \Closure                 $next
20
     *
21
     * @return mixed
22
     */
23
    public function handle($request, Closure $next)
24
    {
25
        if (config('settings.activation')) {
26
            $user = Auth::user();
27
            $currentRoute = Route::currentRouteName();
28
            $routesAllowed = [
29
                'activation-required',
30
                'activate/{token}',
31
                'activate',
32
                'activation',
33
                'exceeded',
34
                'authenticated.activate',
35
                'authenticated.activation-resend',
36
                'logout',
37
                'welcome',
38
            ];
39
40
            if (!in_array($currentRoute, $routesAllowed)) {
41
                if ($user && $user->activated != 1) {
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...
42
                    Log::info('Non-activated user attempted to visit '.$currentRoute.'. ', [$user]);
43
44
                    return redirect()->route('activation-required')
45
                        ->with([
46
                            'message' => 'Activation is required. ',
47
                            'status'  => 'danger',
48
                        ]);
49
                }
50
            }
51
52
            if ($user && $user->activated != 1) {
53
                $activationsCount = Activation::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...
54
                    ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
55
                    ->count();
56
57
                if ($activationsCount >= config('settings.maxAttempts')) {
58
                    return redirect()->route('exceeded');
59
                }
60
            }
61
62
            if (in_array($currentRoute, $routesAllowed)) {
63
                if ($user && $user->activated == 1) {
64
                    Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]);
65
66
                    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

66
                    if ($user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
67
                        return redirect('home');
68
                    }
69
70
                    return redirect('home');
71
                }
72
73
                if (!$user) {
74
                    Log::info('Non registered visit to '.$currentRoute.'. ');
75
76
                    return redirect()->route('welcome');
77
                }
78
            }
79
        }
80
81
        return $next($request);
82
    }
83
}
84