Passed
Push — dependabot/npm_and_yarn/dev/st... ( 917c39...79f3f4 )
by
unknown
12:32 queued 07:14
created

FirstVisitController::showFirstVisitHrForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Auth\AuthController;
6
use Illuminate\Support\Facades\Lang;
7
use Illuminate\Http\Request;
8
use App\Models\Manager;
9
use App\Models\HrAdvisor;
10
use App\Models\Lookup\Department;
11
use App\Services\Validation\RegistrationValidator;
12
13
class FirstVisitController extends AuthController
14
{
15
    /**
16
     * Show the form for completing Manager registration on first visit.
17
     *
18
     * @param Request $request
19
     * @return \Illuminate\Http\Response
20
     */
21
    public function showFirstVisitManagerForm()
22
    {
23
        $routes = [
24
            'return' => route('home'),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

24
            'return' => /** @scrutinizer ignore-call */ route('home'),
Loading history...
25
            'continue' => route('manager.finish_registration'),
26
        ];
27
28
        return view('auth.first_visit_manager', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

28
        return /** @scrutinizer ignore-call */ view('auth.first_visit_manager', [
Loading history...
29
            'routes' => $routes,
30
            'first_visit' => Lang::get('common/auth/first_manager_visit'),
31
            'departments' => Department::all(),
32
            'not_in_gov_option' => ['value' => 0, 'name' => Lang::get('common/auth/register.not_in_gov')],
33
        ]);
34
    }
35
36
    /**
37
     * Process the final data required for Managers.
38
     *
39
     * @param Request $request
40
     * @return void
41
     */
42
    public function finishManagerRegistration(Request $request)
43
    {
44
        $data = $request->all();
45
        $validator = RegistrationValidator::finalizeManagerValidator($data);
46
        $validator->validate();
47
48
        $user = $request->user();
49
50
        // Save manager specific fields to user
51
        $managerDepartment = Department::find($data['department']);
52
        $inGovernment = ($managerDepartment !== null);
53
        $user->not_in_gov = !$inGovernment;
54
        $user->gov_email = $inGovernment ? $data['gov_email'] : null;
55
        $user->save();
56
        $user->refresh();
57
58
        // Add (or update) manager profile
59
        // NOTE: modifying a field in $user, and saving it, appears to create Manager object. I don't know how. -- Tristan
60
        // That means that after setting not_in_gov or gov_email, a manager already exists here. Adding a new one will throw an exception.
61
        $department_id = $inGovernment ? $managerDepartment->id : null;
62
        if ($user->manager === null) {
63
            $user->applicant()->save(new Manager());
64
            $user->refresh();
65
        }
66
        $user->manager->department_id = $department_id;
67
        $user->manager->save();
68
69
        $user->refresh();
70
        $expectedUrl = session()->remove('url.expected');
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

70
        $expectedUrl = /** @scrutinizer ignore-call */ session()->remove('url.expected');
Loading history...
71
        session()->remove('url.expected');
72
        return redirect($expectedUrl);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
        return /** @scrutinizer ignore-call */ redirect($expectedUrl);
Loading history...
73
    }
74
75
    /**
76
     * Show the form for completing HR Advisor registration on first visit.
77
     *
78
     * @return \Illuminate\Http\Response
79
     */
80
    public function showFirstVisitHrForm()
81
    {
82
        $routes = [
83
            'return' => route('hr_advisor.home'),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

83
            'return' => /** @scrutinizer ignore-call */ route('hr_advisor.home'),
Loading history...
84
            'continue' => route('hr_advisor.finish_registration'),
85
        ];
86
87
        return view('auth.first_visit_manager', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

87
        return /** @scrutinizer ignore-call */ view('auth.first_visit_manager', [
Loading history...
88
            'routes' => $routes,
89
            'first_visit' => Lang::get('common/auth/first_hr_visit'),
90
            'departments' => Department::all(),
91
        ]);
92
    }
93
94
    /**
95
     * Process the final data required for Managers.
96
     *
97
     * @param Request $request
98
     * @return void
99
     */
100
    public function finishHrRegistration(Request $request)
101
    {
102
        $data = $request->all();
103
        $validator = RegistrationValidator::finalizeManagerValidator($data);
104
        $validator->validate();
105
106
        $user = $request->user();
107
108
        // Save manager specific fields to user
109
        $hrDepartment = Department::find($data['department']);
110
        $inGovernment = ($hrDepartment !== null);
111
        $user->gov_email = $inGovernment ? $data['gov_email'] : null;
112
113
        $user->save();
114
        $user->refresh();
115
116
        $department_id = $inGovernment ? $hrDepartment->id : null;
117
        if ($user->hr_advisor === null) {
118
            $user->hr_advisor()->save(new HrAdvisor());
119
            $user->refresh();
120
        }
121
        $user->hr_advisor->department_id = $department_id;
122
        $user->hr_advisor->user_id === $user->id;
123
        $user->hr_advisor->save();
124
125
        $user->refresh();
126
        return redirect(route('hr_advisor.home'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
        return /** @scrutinizer ignore-call */ redirect(route('hr_advisor.home'));
Loading history...
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
        return redirect(/** @scrutinizer ignore-call */ route('hr_advisor.home'));
Loading history...
127
    }
128
}
129