Total Complexity | 9 |
Total Lines | 117 |
Duplicated Lines | 0 % |
Coverage | 91.84% |
Changes | 0 |
1 | <?php |
||
16 | class ReferencesController extends Controller |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Show the form for editing the logged-in applicant's references |
||
21 | * |
||
22 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
23 | * @return \Illuminate\Http\RedirectResponse |
||
24 | */ |
||
25 | 1 | public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * Show the form for editing the applicant's references |
||
33 | * |
||
34 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
35 | * @param \App\Models\Applicant $applicant |
||
1 ignored issue
–
show
|
|||
36 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
37 | */ |
||
38 | 1 | public function edit(Request $request, Applicant $applicant) |
|
45 | ]); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Update or create a reference with the supplied data. |
||
50 | * |
||
51 | * @param \Illuminate\Http\Request $request The incoming request object. |
||
52 | * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created. |
||
53 | * |
||
54 | * @return |
||
55 | */ |
||
56 | 2 | public function update(Request $request, ?Reference $reference = null) |
|
57 | { |
||
58 | 2 | $validator = new UpdateReferenceValidator(); |
|
59 | 2 | $validator->validate($request->input()); |
|
60 | |||
61 | 2 | if ($reference === null) { |
|
62 | 1 | $reference = new Reference(); |
|
63 | 1 | $reference->applicant_id = $request->user()->applicant->id; |
|
64 | } |
||
65 | 2 | $reference->fill([ |
|
66 | 2 | 'name' => $request->input('name'), |
|
67 | 2 | 'email' => $request->input('email'), |
|
68 | 2 | 'relationship_id' => $request->input('relationship_id'), |
|
69 | 2 | 'description' => $request->input('description'), |
|
70 | ]); |
||
71 | 2 | $reference->save(); |
|
72 | |||
73 | 2 | $reference->load('projects'); |
|
74 | |||
75 | //TODO: As soon as you can interact with projects outside of references, |
||
76 | // this will become a dangerous operation |
||
77 | 2 | $reference->projects()->delete(); |
|
78 | |||
79 | 2 | $newProjects = []; |
|
80 | 2 | if ($request->input('projects')) { |
|
81 | 2 | foreach ($request->input('projects') as $projectInput) { |
|
82 | 2 | $project = new Project(); |
|
83 | 2 | $project->applicant_id = $reference->applicant_id; |
|
84 | 2 | $project->fill([ |
|
85 | 2 | 'name' => $projectInput['name'], |
|
86 | 2 | 'start_date' => $projectInput['start_date'], |
|
87 | 2 | 'end_date' => $projectInput['end_date'], |
|
88 | ]); |
||
89 | 2 | $project->save(); |
|
90 | 2 | $newProjects[] = $project->id; |
|
91 | // $reference->projects()->attach($project); |
||
92 | } |
||
93 | } |
||
94 | 2 | $reference->projects()->sync($newProjects); |
|
95 | |||
96 | //Attach relatives |
||
97 | 2 | $skillIds = $this->getRelativeIds($request->input(), 'skills'); |
|
98 | 2 | $reference->skill_declarations()->sync($skillIds); |
|
99 | |||
100 | // if an ajax request, return the new object |
||
101 | 2 | if ($request->ajax()) { |
|
102 | $reference->load('relationship'); |
||
103 | $reference->load('projects'); |
||
104 | return $reference->toJson(); |
||
105 | } else { |
||
106 | 2 | return redirect()->back(); |
|
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Delete the particular reference from storage. |
||
112 | * |
||
113 | * @param \Illuminate\Http\Request $request |
||
1 ignored issue
–
show
|
|||
114 | * @param \App\Models\Reference $reference |
||
1 ignored issue
–
show
|
|||
115 | * @return \Illuminate\Http\Response |
||
116 | */ |
||
117 | 1 | public function destroy(Request $request, Reference $reference) |
|
133 | } |
||
134 | } |
||
135 |