Passed
Push — feature/job-summary-hr ( 67ecfb )
by Grant
13:42
created

FirstVisitController::finishHrRegistration()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 28
rs 9.6333
cc 4
nc 8
nop 1
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'),
25
            'continue' => route('manager.finish_registration'),
26
        ];
27
28
        return view('auth.first_visit_manager', [
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');
71
        session()->remove('url.expected');
72
        return redirect($expectedUrl);
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'),
84
            'continue' => route('hr_advisor.finish_registration'),
85
        ];
86
87
        return view('auth.first_visit_manager', [
88
            'routes' => $routes,
89
            'first_visit' => Lang::get('common/auth/first_hr_visit'),
90
            'departments' => Department::all(),
91
            'not_in_gov_option' => ['value' => 0, 'name' => Lang::get('common/auth/register.not_in_gov')],
92
        ]);
93
    }
94
95
    /**
96
     * Process the final data required for Managers.
97
     *
98
     * @param Request $request
99
     * @return void
100
     */
101
    public function finishHrRegistration(Request $request)
102
    {
103
        $data = $request->all();
104
        $validator = RegistrationValidator::finalizeManagerValidator($data);
105
        $validator->validate();
106
107
        $user = $request->user();
108
109
        // Save manager specific fields to user
110
        $hrDepartment = Department::find($data['department']);
111
        $inGovernment = ($hrDepartment !== null);
112
        $user->not_in_gov = !$inGovernment;
113
        $user->gov_email = $inGovernment ? $data['gov_email'] : null;
114
115
        $user->save();
116
        $user->refresh();
117
118
        $department_id = $inGovernment ? $hrDepartment->id : null;
119
        if ($user->hr_advisor === null) {
120
            $user->hr_advisor()->save(new HrAdvisor());
121
            $user->refresh();
122
        }
123
        $user->hr_advisor->department_id = $department_id;
124
        $user->hr_advisor->user_id === $user->id;
125
        $user->hr_advisor->save();
126
127
        $user->refresh();
128
        return redirect(route('hr_advisor.home'));
129
    }
130
}
131