Completed
Push — master ( 52970f...c6d773 )
by Tristan
24:57 queued 10:40
created

ReferencesController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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\Services\Validation\Requests\UpdateReferenceValidator;
14
15
class ReferencesController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ReferencesController
Loading history...
16
{
17
18
    /**
19
     * Display the Skills page associated with the applicant.
20
     *
21
     * @param  \App\Models\Applicant  $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
22
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
23
     */
24
    public function show(Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $applicant is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
    public function show(/** @scrutinizer ignore-unused */ Applicant $applicant)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        //
27
28
    }
29
30
    /**
31
     * Show the form for editing the applicant's references
32
     *
33
     * @param  Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 15 spaces after parameter type; 2 found
Loading history...
34
     * @param  \App\Models\Applicant  $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
35
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
36
     */
37
    public function edit(Request $request, Applicant $applicant)
38
    {
39
        return view('applicant/profile_04_references', [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Bug Best Practice introduced by
The expression return view('applicant/p....update', $applicant))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
40
            'applicant' => $applicant,
41
            'profile' => Lang::get('applicant/profile_references'),
42
            'form_submit_action' => route('profile.references.update', $applicant),
43
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
44
    }
45
46
    /**
47
     * Update all the applicant's references in storage.
48
     *
49
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
50
     * @param  \App\Models\Applicant  $applicant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 2 found
Loading history...
51
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
52
     */
53
    public function updateAll(Request $request, Applicant $applicant)
54
    {
55
        $input = $request->input();
56
57
        $references = $input['references'];
58
59
        //Delete old references that weren't resubmitted
60
        //Note: this must be done before adding new references, so we don't delete
61
        // them right after adding them
62
        foreach($applicant->references as $oldReference) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
63
            //Check if no references were resubmitted, or if this specific one wasn't
64
            if (!isset($references['old']) ||
65
                !isset($references['old'][$oldReference->id])) {
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
66
                $oldReference->delete();
67
            }
68
        }
69
70
        //Save new references
71
        if (isset($references['new'])) {
72
            foreach($references['new'] as $referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
73
                $reference = new Reference();
74
                $reference->applicant_id = $applicant->id;
75
                $reference->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
76
                    'name' => $referenceInput['name'],
77
                    'email' => $referenceInput['email'],
78
                    'relationship_id' => $referenceInput['relationship_id'],
79
                    'description' => $referenceInput['description'],
80
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
81
82
                $reference->save();
83
84
                $projectIds = [];
85
                $projects = $referenceInput['projects'];
86
                if (isset($projects['new'])) {
87
                    foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
88
                        $project = new Project();
89
                        $project->applicant_id = $applicant->id;
90
                        $project->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
91
                            'name' => $projectInput['name'],
92
                            'start_date' => $projectInput['start_date'],
93
                            'end_date' => $projectInput['end_date'],
94
                        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
95
                        $project->save();
96
                        $projectIds[] = $project->id;
97
                    }
98
                }
99
                //Sync attaches the specified ids, and detaches all others
100
                $reference->projects()->sync($projectIds);
101
102
103
                $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
104
                $reference->skill_declarations()->sync($skillDeclarationIds);
105
            }
106
        }
107
108
        //Update old references
109
        if (isset($references['old'])) {
110
            foreach($references['old'] as $id=>$referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
111
                //Ensure this reference belongs to this applicant
112
                $reference = $applicant->references->firstWhere('id', $id);
113
                if ($reference != null) {
114
                    $reference->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
115
                        'name' => $referenceInput['name'],
116
                        'email' => $referenceInput['email'],
117
                        'relationship_id' => $referenceInput['relationship_id'],
118
                        'description' => $referenceInput['description'],
119
                    ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
120
                    $reference->save();
121
122
                    $projectIds = [];
123
                    $projects = $referenceInput['projects'];
124
                    if (isset($projects['new'])) {
125
                        foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
126
                            $project = new Project();
127
                            $project->applicant_id = $applicant->id;
128
                            $project->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
129
                                'name' => $projectInput['name'],
130
                                'start_date' => $projectInput['start_date'],
131
                                'end_date' => $projectInput['end_date'],
132
                            ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
133
                            $project->save();
134
                            $projectIds[] = $project->id;
135
                        }
136
                    }
137
                    if (isset($projects['old'])) {
138
                        foreach($projects['old'] as $projectId=>$projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
139
                            //Ensure this project belongs to this applicant
140
                            $project = $applicant->projects->firstWhere('id', $projectId);
141
                            if ($project != null) {
142
                                $project->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
143
                                    'name' => $projectInput['name'],
144
                                    'start_date' => $projectInput['start_date'],
145
                                    'end_date' => $projectInput['end_date'],
146
                                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
147
                                $project->save();
148
                                $projectIds[] = $project->id;
149
                            }
150
                        }
151
                    }
152
                    //TODO: when projects exists independpently on profile, don't delete them Here
153
                    // Delete projects that will be detached from this reference
154
                    foreach($reference->projects as $project) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
155
                        if (!in_array($project->id, $projectIds)) {
156
                            $project->delete();
157
                        }
158
                    }
159
160
                    //Sync attaches the specified ids, and detaches all others
161
                    $reference->projects()->sync($projectIds);
162
163
                    $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
164
                    $reference->skill_declarations()->sync($skillDeclarationIds);
165
                } else {
166
                    Log::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id);
167
                }
168
            }
169
        }
170
171
        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...
172
    }
173
174
    /**
175
     * Update or create a reference with the supplied data.
176
     *
177
     * @param \Illuminate\Http\Request   $request   The incoming request object.
178
     * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
179
     *
180
     * @return
0 ignored issues
show
Coding Style introduced by
Return type missing for @return tag in function comment
Loading history...
181
     */
182
    public function update(Request $request, ?Reference $reference = null)
183
    {
184
        $validator = new UpdateReferenceValidator();
185
        $validator->validate($request->input());
186
187
        if ($reference === null) {
188
            $reference = new Reference();
189
            $reference->applicant_id = $request->user()->applicant->id;
190
        }
191
        $reference->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
192
            'name' => $request->input('name'),
193
            'email' => $request->input('email'),
194
            'relationship_id' => $request->input('relationship_id'),
195
            'description' => $request->input('description'),
196
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
197
        $reference->save();
198
199
        $reference->load('projects');
200
201
        //TODO: As soon as you can interact with projects outside of references,
202
        //  this will become a dangerous operation
203
        foreach ($reference->projects as $project) {
204
            $project->delete();
205
        }
206
207
        $newProjects = [];
208
        if ($request->input('projects')) {
209
            foreach ($request->input('projects') as $projectInput) {
210
                $project = new Project();
211
                $project->applicant_id = $reference->applicant_id;
212
                $project->fill([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
213
                    'name' => $projectInput['name'],
214
                    'start_date' => $projectInput['start_date'],
215
                    'end_date' => $projectInput['end_date'],
216
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
217
                $project->save();
218
                $newProjects[] = $project->id;
219
                // $reference->projects()->attach($project);
220
            }
221
        }
222
        $reference->projects()->sync($newProjects);
223
224
        //Attach relatives
225
        $skillIds = $this->getRelativeIds($request->input(), 'skills');
226
        $reference->skill_declarations()->sync($skillIds);
227
228
        // if an ajax request, return the new object
229
        if ($request->ajax()) {
230
            $reference->load('relationship');
231
            $reference->load('projects');
232
            return $reference->toJson();
233
        } else {
234
            return redirect()->back();
235
        }
236
    }
237
238
    /**
239
     * Delete the particular reference from storage.
240
     *
241
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
242
     * @param  \App\Models\Reference  $reference
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 2 found
Loading history...
243
     * @return \Illuminate\Http\Response
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
244
     */
245
    public function destroy(Request $request, Reference $reference)
246
    {
247
        $this->authorize('delete', $reference);
248
249
        //TODO: when projects exist independently on profile, delete seperatley
250
        foreach($reference->projects as $project) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
251
            $project->delete();
252
        }
253
254
        $reference->delete();
255
256
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
257
            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...
258
                "message" => 'Reference deleted'
259
            ];
260
        }
261
262
        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...
263
    }
264
265
}
266