Passed
Push — task/test_manager_publish ( e673c0...a1df01 )
by Tristan
07:03
created

InitializeUser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 38 10
1
<?php
2
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
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
2 ignored issues
show
Coding Style Documentation 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
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter type; 2 found
Loading history...
21
     * @return mixed
22
     */
23 19
    public function handle($request, Closure $next)
1 ignored issue
show
introduced by
Method \App\Http\Middleware\InitializeUser::handle() does not have parameter type hint for its parameter $request but it should be possible to add it based on @param annotation "\Illuminate\Http\Request".
Loading history...
Coding Style introduced by
Type hint "\Illuminate\Http\Request" missing for $request
Loading history...
24
    {
25 19
        if (Auth::check()) {
26 11
            $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 11
            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 11
            if ($user->hasRole('applicant') ||
41 11
                    $user->hasRole('admin') ) {
42 4
                $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 4
                if ($applicantProfile === null) {
44 1
                    $applicantProfile = new Applicant();
45 1
                    $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 1
                    $applicantProfile->save();
47
                }
48
            }
49 11
            if ($user->hasRole('manager') ||
50 11
                    $user->hasRole('admin')) {
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 1
                    $managerProfile = new Manager();
54 1
                    $managerProfile->user_id = $user->id;
55 1
                    $managerProfile->save();
56
                }
57
            }
58
        }
59
60 19
        return $next($request);
61
    }
62
}
63