Passed
Push — bugfix/skills-not-displaying-p... ( cc8641 )
by Chris
08:41
created

InitializeUser::handle()   B

Complexity

Conditions 10
Paths 19

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 12.0279

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 38
ccs 16
cts 22
cp 0.7272
rs 7.6666
c 0
b 0
f 0
cc 10
nc 19
nop 2
crap 12.0279

How to fix   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
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use App\Models\UserRole;
7
use App\Models\Applicant;
8
use App\Models\Manager;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\App;
11
use Illuminate\Support\Facades\Config;
12
13
class InitializeUser
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class InitializeUser
Loading history...
14
{
15
    /**
16
     * Ensure that a logged in user is initialized correctly according to its UserRole
17
     * (ie has an associated Applicant or Manager profile)
18
     *
19
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
20
     * @param  \Closure  $next
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter type; 2 found
Loading history...
21
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
22
     */
23 17
    public function handle($request, Closure $next)
24
    {
25 17
        if (Auth::check()) {
26 10
            $user = Auth::user();
27
28
            //If running in a local environment, and FORCE_ADMIN is true,
29
            //automatically set any logged in user to (temporarilly) be an admin
30 10
            if (App::environment() == 'local' && Config::get('app.force_admin')) {
31
                $adminRole = UserRole::where('name', 'admin')->firstOrFail();
32
                $user->user_role_id = $adminRole->id;
0 ignored issues
show
Bug introduced by
Accessing user_role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
                // $user->user_role = $adminRole;
34
                $user->save();
35
            }
36
37
            //Ensure the user has a proper profile associated with it
38
            //If no profile exists yet create one.
39
            //Admins should be given an applicant and manager profile
40 10
            if ($user->hasRole('applicant') ||
41 10
                    $user->hasRole('admin') ) {
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 16 spaces but found 20
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
42 3
                $applicantProfile = $user->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
43 3
                if ($applicantProfile === null) {
44
                    $applicantProfile = new Applicant();
45
                    $applicantProfile->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...
46
                    $applicantProfile->save();
47
                }
48
            }
49 10
            if ($user->hasRole('manager') ||
50 10
                    $user->hasRole('admin')) {
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 16 spaces but found 20
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
51 7
                $managerProfile = $user->manager;
0 ignored issues
show
Bug introduced by
Accessing manager on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52 7
                if ($managerProfile === null) {
53 1
                    $managerProfile = new Manager();
54 1
                    $managerProfile->user_id = $user->id;
55 1
                    $managerProfile->save();
56
                }
57
            }
58
        }
59
60 17
        return $next($request);
61
    }
62
}
63