Completed
Push — feature/add_relatives_from_ski... ( 1e8e28 )
by Tristan
10:01
created

ReferencesController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
14
class ReferencesController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ReferencesController
Loading history...
15
{
16
17
    /**
18
     * Display the Skills page associated with the applicant.
19
     *
20
     * @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...
21
     * @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...
22
     */
23
    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

23
    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...
24
    {
25
        //
26
27
    }
28
29
    /**
30
     * Show the form for editing the applicant's references
31
     *
32
     * @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...
33
     * @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...
34
     * @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...
35
     */
36
    public function edit(Request $request, Applicant $applicant)
37
    {
38
        return view('applicant/profile_04_references', [
1 ignored issue
show
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...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
39
            'applicant' => $applicant,
40
            'profile' => Lang::get('applicant/profile_references'),
41
            'form_submit_action' => route('profile.references.update', $applicant),
42
        ]);
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...
43
    }
44
45
    /**
46
     * Update all the applicant's references in storage.
47
     *
48
     * @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...
49
     * @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...
50
     * @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...
51
     */
52
    public function updateAll(Request $request, Applicant $applicant)
53
    {
54
        $input = $request->input();
55
56
        $references = $input['references'];
57
58
        //Delete old references that weren't resubmitted
59
        //Note: this must be done before adding new references, so we don't delete
60
        // them right after adding them
61
        foreach($applicant->references as $oldReference) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
62
            //Check if no references were resubmitted, or if this specific one wasn't
63
            if (!isset($references['old']) ||
64
                !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...
65
                $oldReference->delete();
66
            }
67
        }
68
69
        //Save new references
70
        if (isset($references['new'])) {
71
            foreach($references['new'] as $referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
72
                $reference = new Reference();
73
                $reference->applicant_id = $applicant->id;
74
                $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...
75
                    'name' => $referenceInput['name'],
76
                    'email' => $referenceInput['email'],
77
                    'relationship_id' => $referenceInput['relationship_id'],
78
                    'description' => $referenceInput['description'],
79
                ]);
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...
80
81
                $reference->save();
82
83
                $projectIds = [];
84
                $projects = $referenceInput['projects'];
85
                if (isset($projects['new'])) {
86
                    foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
87
                        $project = new Project();
88
                        $project->applicant_id = $applicant->id;
89
                        $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...
90
                            'name' => $projectInput['name'],
91
                            'start_date' => $projectInput['start_date'],
92
                            'end_date' => $projectInput['end_date'],
93
                        ]);
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...
94
                        $project->save();
95
                        $projectIds[] = $project->id;
96
                    }
97
                }
98
                //Sync attaches the specified ids, and detaches all others
99
                $reference->projects()->sync($projectIds);
100
101
102
                $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
103
                $reference->skill_declarations()->sync($skillDeclarationIds);
104
            }
105
        }
106
107
        //Update old references
108
        if (isset($references['old'])) {
109
            foreach($references['old'] as $id=>$referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
110
                //Ensure this reference belongs to this applicant
111
                $reference = $applicant->references->firstWhere('id', $id);
112
                if ($reference != null) {
113
                    $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...
114
                        'name' => $referenceInput['name'],
115
                        'email' => $referenceInput['email'],
116
                        'relationship_id' => $referenceInput['relationship_id'],
117
                        'description' => $referenceInput['description'],
118
                    ]);
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...
119
                    $reference->save();
120
121
                    $projectIds = [];
122
                    $projects = $referenceInput['projects'];
123
                    if (isset($projects['new'])) {
124
                        foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
125
                            $project = new Project();
126
                            $project->applicant_id = $applicant->id;
127
                            $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...
128
                                'name' => $projectInput['name'],
129
                                'start_date' => $projectInput['start_date'],
130
                                'end_date' => $projectInput['end_date'],
131
                            ]);
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...
132
                            $project->save();
133
                            $projectIds[] = $project->id;
134
                        }
135
                    }
136
                    if (isset($projects['old'])) {
137
                        foreach($projects['old'] as $projectId=>$projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
138
                            //Ensure this project belongs to this applicant
139
                            $project = $applicant->projects->firstWhere('id', $projectId);
140
                            if ($project != null) {
141
                                $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...
142
                                    'name' => $projectInput['name'],
143
                                    'start_date' => $projectInput['start_date'],
144
                                    'end_date' => $projectInput['end_date'],
145
                                ]);
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...
146
                                $project->save();
147
                                $projectIds[] = $project->id;
148
                            }
149
                        }
150
                    }
151
                    //TODO: when projects exists independpently on profile, don't delete them Here
152
                    // Delete projects that will be detached from this reference
153
                    foreach($reference->projects as $project) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
154
                        if (!in_array($project->id, $projectIds)) {
155
                            $project->delete();
156
                        }
157
                    }
158
159
                    //Sync attaches the specified ids, and detaches all others
160
                    $reference->projects()->sync($projectIds);
161
162
                    $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
163
                    $reference->skill_declarations()->sync($skillDeclarationIds);
164
                } else {
165
                    Log::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id);
166
                }
167
            }
168
        }
169
170
        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...
171
    }
172
173
    /**
174
     * Update or create a reference with the supplied data.
175
     *
176
     * @param \Illuminate\Http\Request   $request   The incoming request object.
177
     * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
178
     *
179
     * @return
0 ignored issues
show
Coding Style introduced by
Return type missing for @return tag in function comment
Loading history...
180
     */
181
    public function update(Request $request, ?Reference $reference = null)
182
    {
183
        if ($reference === null) {
184
            $reference = new Reference();
185
            $reference->applicant_id = $request->user()->applicant->id;
186
        }
187
        $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...
188
            'name' => $request->input('name'),
189
            'email' => $request->input('email'),
190
            'relationship_id' => $request->input('relationship_id'),
191
            'description' => $request->input('description'),
192
        ]);
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...
193
        $reference->save();
194
195
        $reference->load('projects');
196
197
        //TODO: As soon as you can interact with projects outside of references,
198
        //  this will become a dangerous operation
199
        foreach ($reference->projects as $project) {
200
            $project->delete();
201
        }
202
203
        $newProjects = [];
204
        if ($request->input('projects')) {
205
            foreach ($request->input('projects') as $projectInput) {
206
                $project = new Project();
207
                $project->applicant_id = $reference->applicant_id;
208
                $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...
209
                    'name' => $projectInput['name'],
210
                    'start_date' => $projectInput['start_date'],
211
                    'end_date' => $projectInput['end_date'],
212
                ]);
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...
213
                $project->save();
214
                $newProjects[] = $project->id;
215
                // $reference->projects()->attach($project);
216
            }
217
        }
218
        $reference->projects()->sync($newProjects);
219
        $reference->save();
220
221
        // if an ajax request, return the new object
222
        if ($request->ajax()) {
223
            return $reference->toJson();
224
        } else {
225
            return redirect()->back();
226
        }
227
    }
228
229
    /**
230
     * Delete the particular reference from storage.
231
     *
232
     * @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...
233
     * @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...
234
     * @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...
235
     */
236
    public function destroy(Request $request, Reference $reference)
237
    {
238
        $this->authorize('delete', $reference);
239
240
        //TODO: when projects exist independently on profile, delete seperatley
241
        foreach($reference->projects as $project) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
242
            $project->delete();
243
        }
244
245
        $reference->delete();
246
247
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
248
            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...
249
                "message" => 'Reference deleted'
250
            ];
251
        }
252
253
        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...
254
    }
255
256
}
257