Total Complexity | 9 |
Total Lines | 117 |
Duplicated Lines | 0 % |
Coverage | 0% |
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 | 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 |
||
37 | */ |
||
38 | public function edit(Request $request, Applicant $applicant): \Illuminate\View\View |
||
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 | public function update(Request $request, ?Reference $reference = null) |
||
57 | { |
||
58 | $validator = new UpdateReferenceValidator(); |
||
59 | $validator->validate($request->input()); |
||
60 | |||
61 | if ($reference === null) { |
||
62 | $reference = new Reference(); |
||
63 | $reference->applicant_id = $request->user()->applicant->id; |
||
64 | } |
||
65 | $reference->fill([ |
||
66 | 'name' => $request->input('name'), |
||
67 | 'email' => $request->input('email'), |
||
68 | 'relationship_id' => $request->input('relationship_id'), |
||
69 | 'description' => $request->input('description'), |
||
70 | ]); |
||
71 | $reference->save(); |
||
72 | |||
73 | $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 | $reference->projects()->delete(); |
||
78 | |||
79 | $newProjects = []; |
||
80 | if ($request->input('projects')) { |
||
81 | foreach ($request->input('projects') as $projectInput) { |
||
82 | $project = new Project(); |
||
83 | $project->applicant_id = $reference->applicant_id; |
||
84 | $project->fill([ |
||
85 | 'name' => $projectInput['name'], |
||
86 | 'start_date' => $projectInput['start_date'], |
||
87 | 'end_date' => $projectInput['end_date'], |
||
88 | ]); |
||
89 | $project->save(); |
||
90 | $newProjects[] = $project->id; |
||
91 | // $reference->projects()->attach($project); |
||
92 | } |
||
93 | } |
||
94 | $reference->projects()->sync($newProjects); |
||
95 | |||
96 | //Attach relatives |
||
97 | $skillIds = $this->getRelativeIds($request->input(), 'skills'); |
||
98 | $reference->skill_declarations()->sync($skillIds); |
||
99 | |||
100 | // if an ajax request, return the new object |
||
101 | if ($request->ajax()) { |
||
102 | $reference->load('relationship'); |
||
103 | $reference->load('projects'); |
||
104 | return $reference->toJson(); |
||
105 | } else { |
||
106 | 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 | public function destroy(Request $request, Reference $reference) |
||
133 | } |
||
134 | } |
||
135 |