Passed
Push — master ( 6ceee8...d4902a )
by Tristan
20:51 queued 08:57
created

FirstVisitController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 29
c 0
b 0
f 0
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A showFirstVisitManagerForm() 0 12 1
A finishManagerRegistration() 0 31 4
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
     * @param Request $request
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function showFirstVisitManagerForm()
21
    {
22
        $routes = [
23
            'return' => route('home'),
24
            'continue' => route('manager.finish_registration'),
25
        ];
26
27
        return view('auth.first_visit_manager', [
28
            'routes' => $routes,
29
            'first_visit' => Lang::get('common/auth/first_manager_visit'),
30
            'departments' => Department::all(),
31
            'not_in_gov_option' => ['value' => 0, 'name' => Lang::get('common/auth/register.not_in_gov')],
32
        ]);
33
    }
34
35
    /**
36
     * Process the final data required for Managers.
37
     *
38
     * @param Request $request
39
     * @return void
40
     */
41
    public function finishManagerRegistration(Request $request)
42
    {
43
        $data = $request->all();
44
        $validator = RegistrationValidator::finalizeManagerValidator($data);
45
        $validator->validate();
46
47
        $user = $request->user();
48
49
        // Save manager specific fields to user
50
        $managerDepartment = Department::find($data['department']);
51
        $inGovernment = ($managerDepartment !== null);
52
        $user->not_in_gov = !$inGovernment;
53
        $user->gov_email = $inGovernment ? $data['gov_email'] : null;
54
        $user->save();
55
        $user->refresh();
56
57
        // Add (or update) manager profile
58
        // NOTE: modifying a field in $user, and saving it, appears to create Manager object. I don't know how. -- Tristan
59
        // That means that after setting not_in_gov or gov_email, a manager already exists here. Adding a new one will throw an exception.
60
        $department_id = $inGovernment ? $managerDepartment->id : null;
61
        if ($user->manager === null) {
62
            $user->applicant()->save(new Manager());
63
            $user->refresh();
64
        }
65
        $user->manager->department_id = $department_id;
66
        $user->manager->save();
67
68
        $user->refresh();
69
        $expectedUrl = session()->remove('url.expected');
70
        session()->remove('url.expected');
71
        return redirect($expectedUrl);
72
    }
73
}
74