Passed
Push — task/laravel-boot-performance ( 8151a7 )
by Tristan
09:15
created

ReferencesController::editAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Models\Skill;
10
use App\Models\Applicant;
11
use App\Models\Reference;
12
use App\Models\Project;
13
use App\Models\Lookup\Relationship;
14
use App\Services\Validation\Requests\UpdateReferenceValidator;
15
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
23
     * @return \Illuminate\Http\RedirectResponse
24
     */
25
    public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse
26
    {
27
        $applicant = $request->user()->applicant;
28
        return redirect(route('profile.references.edit', $applicant));
29
    }
30
31
    /**
32
     * Show the form for editing the applicant's references
33
     *
34
     * @param  Request               $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
35
     * @param  \App\Models\Applicant $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
36
     * @return \Illuminate\View\View
37
     */
38
    public function edit(Request $request, Applicant $applicant): \Illuminate\View\View
39
    {
40
        return view('applicant/profile_04_references', [
41
            'applicant' => $applicant,
42
            'profile' => Lang::get('applicant/profile_references'),
43
            'reference_template' => Lang::get('common/references'),
44
            'relationships' => Relationship::all(),
45
            'form_submit_action' => route('profile.references.update', $applicant),
46
        ]);
47
    }
48
49
    /**
50
     * Update all the applicant's references in storage.
51
     *
52
     * @param  \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
53
     * @param  \App\Models\Applicant    $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
54
     * @return \Illuminate\Http\Response
55
     */
56
    public function updateAll(Request $request, Applicant $applicant)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\ReferencesController::updateAll() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Http\Response".
Loading history...
57
    {
58
        $input = $request->input();
59
60
        $references = $input['references'];
61
62
        //Delete old references that weren't resubmitted
63
        //Note: this must be done before adding new references, so we don't delete
64
        // them right after adding them
65
        foreach ($applicant->references as $oldReference) {
66
            //Check if no references were resubmitted, or if this specific one wasn't
67
            if (!isset($references['old']) ||
68
                !isset($references['old'][$oldReference->id])) {
69
                $oldReference->delete();
70
            }
71
        }
72
73
        //Save new references
74
        if (isset($references['new'])) {
75
            foreach ($references['new'] as $referenceInput) {
76
                $reference = new Reference();
77
                $reference->applicant_id = $applicant->id;
78
                $reference->fill([
79
                    'name' => $referenceInput['name'],
80
                    'email' => $referenceInput['email'],
81
                    'relationship_id' => $referenceInput['relationship_id'],
82
                    'description' => $referenceInput['description'],
83
                ]);
84
85
                $reference->save();
86
87
                $projectIds = [];
88
                $projects = $referenceInput['projects'];
89
                if (isset($projects['new'])) {
90
                    foreach ($projects['new'] as $projectInput) {
91
                        $project = new Project();
92
                        $project->applicant_id = $applicant->id;
93
                        $project->fill([
94
                            'name' => $projectInput['name'],
95
                            'start_date' => $projectInput['start_date'],
96
                            'end_date' => $projectInput['end_date'],
97
                        ]);
98
                        $project->save();
99
                        $projectIds[] = $project->id;
100
                    }
101
                }
102
                //Sync attaches the specified ids, and detaches all others
103
                $reference->projects()->sync($projectIds);
104
105
106
                $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
107
                $reference->skill_declarations()->sync($skillDeclarationIds);
108
            }
109
        }
110
111
        //Update old references
112
        if (isset($references['old'])) {
113
            foreach ($references['old'] as $id => $referenceInput) {
114
                //Ensure this reference belongs to this applicant
115
                $reference = $applicant->references->firstWhere('id', $id);
116
                if ($reference != null) {
117
                    $reference->fill([
118
                        'name' => $referenceInput['name'],
119
                        'email' => $referenceInput['email'],
120
                        'relationship_id' => $referenceInput['relationship_id'],
121
                        'description' => $referenceInput['description'],
122
                    ]);
123
                    $reference->save();
124
125
                    $projectIds = [];
126
                    $projects = $referenceInput['projects'];
127
                    if (isset($projects['new'])) {
128
                        foreach ($projects['new'] as $projectInput) {
129
                            $project = new Project();
130
                            $project->applicant_id = $applicant->id;
131
                            $project->fill([
132
                                'name' => $projectInput['name'],
133
                                'start_date' => $projectInput['start_date'],
134
                                'end_date' => $projectInput['end_date'],
135
                            ]);
136
                            $project->save();
137
                            $projectIds[] = $project->id;
138
                        }
139
                    }
140
                    if (isset($projects['old'])) {
141
                        foreach ($projects['old'] as $projectId => $projectInput) {
142
                            //Ensure this project belongs to this applicant
143
                            $project = $applicant->projects->firstWhere('id', $projectId);
144
                            if ($project != null) {
145
                                $project->fill([
146
                                    'name' => $projectInput['name'],
147
                                    'start_date' => $projectInput['start_date'],
148
                                    'end_date' => $projectInput['end_date'],
149
                                ]);
150
                                $project->save();
151
                                $projectIds[] = $project->id;
152
                            }
153
                        }
154
                    }
155
                    //TODO: when projects exists independpently on profile, don't delete them Here
156
                    // Delete projects that will be detached from this reference
157
                    foreach ($reference->projects as $project) {
158
                        if (!in_array($project->id, $projectIds)) {
159
                            $project->delete();
160
                        }
161
                    }
162
163
                    //Sync attaches the specified ids, and detaches all others
164
                    $reference->projects()->sync($projectIds);
165
166
                    $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
167
                    $reference->skill_declarations()->sync($skillDeclarationIds);
168
                } else {
169
                    Log::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id);
170
                }
171
            }
172
        }
173
174
        return redirect(route('profile.references.edit', $applicant));
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...ces.edit', $applicant)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
175
    }
176
177
    /**
178
     * Update or create a reference with the supplied data.
179
     *
180
     * @param \Illuminate\Http\Request   $request   The incoming request object.
181
     * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
182
     *
183
     * @return
0 ignored issues
show
Coding Style introduced by
Return type missing for @return tag in function comment
Loading history...
184
     */
185
    public function update(Request $request, ?Reference $reference = null)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\ReferencesController::update() does not have return type hint nor @return annotation for its return value.
Loading history...
186
    {
187
        $validator = new UpdateReferenceValidator();
188
        $validator->validate($request->input());
189
190
        if ($reference === null) {
191
            $reference = new Reference();
192
            $reference->applicant_id = $request->user()->applicant->id;
193
        }
194
        $reference->fill([
195
            'name' => $request->input('name'),
196
            'email' => $request->input('email'),
197
            'relationship_id' => $request->input('relationship_id'),
198
            'description' => $request->input('description'),
199
        ]);
200
        $reference->save();
201
202
        $reference->load('projects');
203
204
        //TODO: As soon as you can interact with projects outside of references,
205
        //  this will become a dangerous operation
206
        foreach ($reference->projects as $project) {
207
            $project->delete();
208
        }
209
210
        $newProjects = [];
211
        if ($request->input('projects')) {
212
            foreach ($request->input('projects') as $projectInput) {
213
                $project = new Project();
214
                $project->applicant_id = $reference->applicant_id;
215
                $project->fill([
216
                    'name' => $projectInput['name'],
217
                    'start_date' => $projectInput['start_date'],
218
                    'end_date' => $projectInput['end_date'],
219
                ]);
220
                $project->save();
221
                $newProjects[] = $project->id;
222
                // $reference->projects()->attach($project);
223
            }
224
        }
225
        $reference->projects()->sync($newProjects);
226
227
        //Attach relatives
228
        $skillIds = $this->getRelativeIds($request->input(), 'skills');
229
        $reference->skill_declarations()->sync($skillIds);
230
231
        // if an ajax request, return the new object
232
        if ($request->ajax()) {
233
            $reference->load('relationship');
234
            $reference->load('projects');
235
            return $reference->toJson();
236
        } else {
237
            return redirect()->back();
238
        }
239
    }
240
241
    /**
242
     * Delete the particular reference from storage.
243
     *
244
     * @param  \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
245
     * @param  \App\Models\Reference    $reference
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
246
     * @return \Illuminate\Http\Response
247
     */
248
    public function destroy(Request $request, Reference $reference)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\ReferencesController::destroy() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Http\Response".
Loading history...
249
    {
250
        $this->authorize('delete', $reference);
251
252
        //TODO: when projects exist independently on profile, delete seperatley
253
        foreach ($reference->projects as $project) {
254
            $project->delete();
255
        }
256
257
        $reference->delete();
258
259
        if ($request->ajax()) {
260
            return [
1 ignored issue
show
Bug Best Practice introduced by
The expression return array('message' => 'Reference deleted') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
261
                "message" => 'Reference deleted'
262
            ];
263
        }
264
265
        return redirect()->back();
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
266
    }
267
}
268