Completed
Push — task/update-README ( b08d22 )
by Xander
16s queued 10s
created

InitializeUser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 10
eloc 22
dl 0
loc 48
ccs 16
cts 22
cp 0.7272
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 38 10
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 30
    public function handle($request, Closure $next)
24
    {
25 30
        if (Auth::check()) {
26 16
            $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 16
            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 16
            if ($user->hasRole('applicant') ||
41 16
                    $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 8
                $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 8
                if ($applicantProfile === null) {
44 2
                    $applicantProfile = new Applicant();
45 2
                    $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 2
                    $applicantProfile->save();
47
                }
48
            }
49 16
            if ($user->hasRole('manager') ||
50 16
                    $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 8
                $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 8
                if ($managerProfile === null) {
53
                    $managerProfile = new Manager();
54
                    $managerProfile->user_id = $user->id;
55
                    $managerProfile->save();
56
                }
57
            }
58
        }
59
60 30
        return $next($request);
61
    }
62
}
63