Total Complexity | 8 |
Total Lines | 131 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | <?php |
||
17 | class SkillDeclarationController extends Controller |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Show the form for editing the logged-in applicant's skills |
||
22 | * |
||
23 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
24 | * @return \Illuminate\Http\RedirectResponse |
||
25 | */ |
||
26 | public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse |
||
27 | { |
||
28 | $applicant = $request->user()->applicant; |
||
29 | return redirect(route('profile.skills.edit', $applicant)); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Show the form for editing the applicant's skills |
||
34 | * |
||
35 | * @param Request $request Incoming request object. |
||
36 | * @param Applicant $applicant Applicant object. |
||
37 | * |
||
38 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
39 | */ |
||
40 | public function edit(Request $request, Applicant $applicant) |
||
41 | { |
||
42 | $applicant->load([ |
||
43 | 'skill_declarations.skill.skill_type', |
||
44 | 'skill_declarations.skill_status', |
||
45 | 'skill_declarations.skill_level', |
||
46 | ]); |
||
47 | |||
48 | return view('applicant/profile_03_skills', [ |
||
49 | 'applicant' => $applicant, |
||
50 | 'profile' => Lang::get('applicant/profile_skills'), |
||
51 | ]); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Create the particular skill declaration in storage. |
||
56 | * |
||
57 | * @param \Illuminate\Http\Request $request |
||
1 ignored issue
–
show
|
|||
58 | * @return \Illuminate\Http\Response |
||
59 | */ |
||
60 | public function create(Request $request): \Illuminate\Http\Response |
||
61 | { |
||
62 | $this->authorize('create', SkillDeclaration::class); |
||
63 | |||
64 | $user = $request->user(); |
||
65 | $applicant = $user->applicant; |
||
66 | |||
67 | //Get the default claim status id |
||
68 | $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
||
69 | |||
70 | // Create a new Skill Declaration |
||
71 | // But don't save, as it hasn't been validated yet |
||
72 | $skillDeclaration = new SkillDeclaration(); |
||
73 | $skillDeclaration->applicant_id = $applicant->id; |
||
74 | $skillDeclaration->skill_id = $request->input('skill_id'); |
||
75 | $skillDeclaration->skill_status_id = $claimedStatusId; |
||
76 | |||
77 | //Update variable fields in skill declaration |
||
78 | return $this->updateSkillDeclaration($request, $skillDeclaration); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Update the particular skill declaration in storage. |
||
83 | * |
||
84 | * @param \Illuminate\Http\Request $request |
||
1 ignored issue
–
show
|
|||
85 | * @param \App\Models\SkillDeclaration $skillDeclaration |
||
1 ignored issue
–
show
|
|||
86 | * @return \Illuminate\Http\Response |
||
87 | */ |
||
88 | public function update(Request $request, SkillDeclaration $skillDeclaration): \Illuminate\Http\Response |
||
89 | { |
||
90 | $this->authorize('update', $skillDeclaration); |
||
91 | |||
92 | return $this->updateSkillDeclaration($request, $skillDeclaration); |
||
93 | } |
||
94 | |||
95 | protected function updateSkillDeclaration(Request $request, SkillDeclaration $skillDeclaration) |
||
1 ignored issue
–
show
|
|||
96 | { |
||
97 | //Fill variable values |
||
98 | $skillDeclaration->fill([ |
||
99 | 'description' => $request->input('description'), |
||
100 | 'skill_level_id' => $request->input('skill_level_id'), |
||
101 | ]); |
||
102 | |||
103 | //Validate before saving |
||
104 | $validator = new SkillDeclarationValidator($request->user()->applicant); |
||
105 | $validator->validate($skillDeclaration); |
||
106 | |||
107 | //Save this skill declaration |
||
108 | $skillDeclaration->save(); |
||
109 | |||
110 | //Attach relatives |
||
111 | $referenceIds = $this->getRelativeIds($request->input(), 'references'); |
||
112 | $skillDeclaration->references()->sync($referenceIds); |
||
113 | |||
114 | $sampleIds = $this->getRelativeIds($request->input(), 'samples'); |
||
115 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
116 | |||
117 | // $skillDeclaration->save(); |
||
118 | |||
119 | // If an ajax request, return the new object |
||
120 | if ($request->ajax()) { |
||
121 | $skillDeclaration->load('references'); |
||
122 | $skillDeclaration->load('work_samples'); |
||
123 | $skillDeclaration->load('skill'); |
||
124 | $skillDeclaration->load('skill_status'); |
||
125 | return $skillDeclaration->toJson(); |
||
126 | } |
||
127 | |||
128 | return redirect()->back(); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Delete the particular skill declaration in storage. |
||
133 | * |
||
134 | * @param \Illuminate\Http\Request $request |
||
1 ignored issue
–
show
|
|||
135 | * @param \App\Models\SkillDeclaration $skillDeclaration |
||
1 ignored issue
–
show
|
|||
136 | * @return \Illuminate\Http\Response |
||
137 | */ |
||
138 | public function destroy(Request $request, SkillDeclaration $skillDeclaration): \Illuminate\Http\Response |
||
148 | } |
||
149 | } |
||
150 |