Passed
Push — dev ( 03a84b...1c649f )
by Chris
06:48 queued 10s
created

FirstVisitController::showFirstVisitManagerForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 1
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\Lookup\Department;
10
use App\Services\Validation\RegistrationValidator;
11
12
class FirstVisitController extends AuthController
13
{
14
    /**
15
     * Show the form for completing Manager registration on first visit.
16
     *
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function showFirstVisitManagerForm()
20
    {
21
        $routes = [
22
            'return' => route('home'),
23
            'continue' => route('manager.finish_registration'),
24
        ];
25
26
        return view('auth.first_visit_manager', [
27
            'routes' => $routes,
28
            'first_visit' => Lang::get('common/auth/first_manager_visit'),
29
            'departments' => Department::all(),
30
            'not_in_gov_option' => ['value' => 0, 'name' => Lang::get('common/auth/register.not_in_gov')],
31
        ]);
32
    }
33
34
    /**
35
     * Process the final data required for Managers.
36
     *
37
     * @param Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
38
     * @return void
1 ignored issue
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
39
     */
40
    public function finishManagerRegistration(Request $request)
41
    {
42
        $data = $request->all();
43
        $validator = RegistrationValidator::finalizeManagerValidator($data);
44
        $validator->validate();
45
46
        $user = $request->user();
47
48
        // Save manager specific fields to user
49
        $managerDepartment = Department::find($data['department']);
50
        $inGovernment = ($managerDepartment !== null);
51
        $user->not_in_gov = !$inGovernment;
52
        $user->gov_email = $inGovernment ? $data['gov_email'] : null;
53
        $user->save();
54
        $user->refresh();
55
56
        // Add (or update) manager profile
57
        // NOTE: modifying a field in $user, and saving it, appears to create Manager object. I don't know how. -- Tristan
58
        // That means that after setting not_in_gov or gov_email, a manager already exists here. Adding a new one will throw an exception.
59
        $department_id = $inGovernment ? $managerDepartment->id : null;
60
        if ($user->manager === null) {
61
            $user->applicant()->save(new Manager());
62
            $user->refresh();
63
        }
64
        $user->manager->department_id = $department_id;
65
        $user->manager->save();
66
67
        $user->refresh();
68
        return redirect(route('manager.home'));
69
    }
70
}
71