Completed
Push — feature/ajax_skill_dec ( bfc311...8436b3 )
by Tristan
06:49
created

SkillsController::updateSkillDeclaration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 28
ccs 0
cts 16
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Models\Skill;
9
use App\Models\Lookup\SkillLevel;
10
use App\Models\Lookup\SkillStatus;
11
use App\Models\SkillDeclaration;
12
use App\Models\Applicant;
13
use App\Http\Controllers\Controller;
14
use App\Services\Validation\BulkSkillDeclarationValidator;
15
use App\Services\Validation\Rules\UniqueApplicantSkillRule;
16
use App\Services\Validation\Rules\ApplicantHasRelationRule;
17
use Illuminate\Validation\Rule;
18
19
class SkillsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillsController
Loading history...
20
{
21
22
    /**
23
     * Display the Skills page associated with the applicant.
24
     *
25
     * @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...
26
     * @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...
27
     */
28
    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

28
    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...
29
    {
30
        //
31
32
    }
33
34
    /**
35
     * Show the form for editing the applicant's skills
36
     *
37
     * @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...
38
     * @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...
39
     * @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...
40
     */
41
    public function edit(Request $request, Applicant $applicant)
42
    {
43
        $skills = Skill::all();
44
45
        return view('applicant/profile_03_skills', [
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('applicant/p..., 'skills' => $skills)) 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...
46
            'applicant' => $applicant,
47
            'profile' => Lang::get('applicant/profile_skills'),
48
            'form_submit_action' => route('profile.skills.update', $applicant),
0 ignored issues
show
Bug introduced by
$applicant of type App\Models\Applicant is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

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

48
            'form_submit_action' => route('profile.skills.update', /** @scrutinizer ignore-type */ $applicant),
Loading history...
49
            'skills' => $skills
50
        ]);
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...
51
    }
52
53
    /**
54
     * Update the applicant's profile in storage.
55
     *
56
     * @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...
57
     * @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...
58
     * @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...
59
     */
60
    public function updateAll(Request $request, Applicant $applicant)
61
    {
62
        $input = $request->input();
63
64
        $validator = new BulkSkillDeclarationValidator($applicant, $input);
65
        $validator->validate($input);
66
67
        $skillDeclarations = $request->input('skill_declarations');
68
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
69
70
        //Delete old skill declarations that weren't resubmitted
71
        //Note: this must be done before adding new ones, so we don't delete
72
        // them right after adding them
73
        foreach($applicant->skill_declarations as $oldDeclaration) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
74
            //Check if none were resubmitted, or if this specific one wasn't
75
            $type = $oldDeclaration->skill->skill_type->name;
76
            if (!isset($skillDeclarations['old']) ||
77
                !isset($skillDeclarations['old'][$type]) ||
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
78
                !isset($skillDeclarations['old'][$type][$oldDeclaration->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...
79
                $oldDeclaration->delete();
80
            }
81
        }
82
83
        //Save new skill declarartions
84
        if (isset($skillDeclarations['new'])) {
85
            foreach($skillDeclarations['new'] as $skillType => $typeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
86
                foreach($typeInput as $skillDeclarationInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
87
                    $skillDeclaration = new SkillDeclaration();
88
                    $skillDeclaration->applicant_id = $applicant->id;
89
                    $skillDeclaration->skill_id = $skillDeclarationInput['skill_id'];
90
                    $skillDeclaration->skill_status_id = $claimedStatusId;
91
                    $skillDeclaration->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...
92
                        'description' => $skillDeclarationInput['description'],
93
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
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
                    $skillDeclaration->save();
96
97
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
98
                    $skillDeclaration->references()->sync($referenceIds);
99
100
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
101
                    $skillDeclaration->work_samples()->sync($sampleIds);
102
                }
103
            }
104
        }
105
106
        //Update old declarations
107
        if (isset($skillDeclarations['old'])) {
108
            foreach($skillDeclarations['old'] as $skillType => $typeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
109
                foreach($typeInput as $id=>$skillDeclarationInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
110
                    //Ensure this declaration belongs to this applicant
111
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
112
                    if ($skillDeclaration != null) {
113
                        //skill_id and skill_status cannot be changed
114
                        $skillDeclaration->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
                            'description' => $skillDeclarationInput['description'],
116
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
117
                        ]);
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...
118
                        $skillDeclaration->save();
119
120
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
121
                        $skillDeclaration->references()->sync($referenceIds);
122
123
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
124
                        $skillDeclaration->work_samples()->sync($sampleIds);
125
                    } else {
126
                        Log::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id);
127
                    }
128
                }
129
            }
130
        }
131
132
133
        return redirect( route('profile.skills.edit', $applicant) );
1 ignored issue
show
Bug introduced by
$applicant of type App\Models\Applicant is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

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

133
        return redirect( route('profile.skills.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
Bug Best Practice introduced by
The expression return redirect(route('p...lls.edit', $applicant)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
134
    }
135
136
    /**
137
     * Create the particular skill declaration in storage.
138
     *
139
     * @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...
140
     * @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...
141
     */
142
    public function create(Request $request)
143
    {
144
        $this->authorize('create', SkillDeclaration::class);
145
146
        $user = $request->user();
147
        $applicant = $user->applicant;
148
149
        $skill_level_ids = SkillLevel::all()->pluck('id');
150
        $skill_ids = Skill::all()->pluck('id');
151
        $uniqueSkillRule = new UniqueApplicantSkillRule($applicant);
152
        $applicantHasSkillRule = new ApplicantHasRelationRule($applicant, 'skill_declarations');
0 ignored issues
show
Unused Code introduced by
The assignment to $applicantHasSkillRule is dead and can be removed.
Loading history...
153
154
        $request->validate([
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...
155
            'skill_id' => [
156
                'required',
157
                Rule::in($skill_ids->toArray()),
158
                $uniqueSkillRule,
159
            ],
160
            'skill_level_id' => [
161
                'required',
162
                Rule::in($skill_level_ids->toArray()),
163
            ],
164
            'description' => 'string|required',
165
            'fake' => 'string',
166
            'fake_2' => 'numeric|min:10'
167
        ]);
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...
168
169
        //Get the default claim status id
170
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
171
172
        // Create a new Skill Declaration
173
        $skillDeclaration = new SkillDeclaration();
174
        $skillDeclaration->applicant_id = $applicant->id;
175
        $skillDeclaration->skill_id = $request->input('skill_id');
176
        $skillDeclaration->skill_status_id = $claimedStatusId;
177
178
        //Update variable fields in skill declaration
179
        return $this->updateSkillDeclaration($request, $skillDeclaration);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->updateSkil...est, $skillDeclaration) returns the type Illuminate\Http\RedirectResponse|string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
180
    }
181
182
    /**
183
     * Update the particular skill declaration in storage.
184
     *
185
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 2 found
Loading history...
186
     * @param  \App\Models\SkillDeclaration  $skillDeclaration
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...
187
     * @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...
188
     */
189
    public function update(Request $request, SkillDeclaration $skillDeclaration)
190
    {
191
        $this->authorize('update', $skillDeclaration);
192
193
        $skill_level_ids = SkillLevel::all()->pluck('id');
194
195
        $request->validate([
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...
196
            'skill_level_id' => [
197
                'required',
198
                Rule::in($skill_level_ids->toArray()),
199
            ],
200
            'description' => 'string|required',
201
            'fake' => 'string',
202
            'fake_2' => 'numeric|min:10'
203
        ]);
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...
204
205
        return $this->updateSkillDeclaration($request, $skillDeclaration);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->updateSkil...est, $skillDeclaration) returns the type Illuminate\Http\RedirectResponse|string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
206
    }
207
208
    protected function updateSkillDeclaration(Request $request, SkillDeclaration $skillDeclaration)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function updateSkillDeclaration()
Loading history...
209
    {
210
        //Fill variable values
211
        $skillDeclaration->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...
212
            'description' => $request->input('description'),
213
            'skill_level_id' => $request->input('skill_level_id'),
214
        ]);
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...
215
216
        //Save this skill declaration
217
        $skillDeclaration->save();
218
219
        //Attach relatives
220
        $referenceIds = $this->getRelativeIds($request->input(), 'references');
221
        $skillDeclaration->references()->sync($referenceIds);
222
223
        $sampleIds = $this->getRelativeIds($request->input(), 'samples');
224
        $skillDeclaration->work_samples()->sync($sampleIds);
225
226
        // If an ajax request, return the new object
227
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
228
            $skillDeclaration->load('references');
229
            $skillDeclaration->load('work_samples');
230
            $skillDeclaration->load('skill');
231
            $skillDeclaration->load('skill_status');
232
            return $skillDeclaration->toJson();
233
        }
234
235
        return redirect()->back();
236
    }
237
238
    /**
239
     * Delete the particular skill declaration in 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 5 spaces after parameter type; 2 found
Loading history...
242
     * @param  \App\Models\SkillDeclaration  $skillDeclaration
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...
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, SkillDeclaration $skillDeclaration)
246
    {
247
        $this->authorize('delete', $skillDeclaration);
248
        $skillDeclaration->delete();
249
250
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
251
            return ['message' => 'Skill deleted'];
1 ignored issue
show
Bug Best Practice introduced by
The expression return array('message' => 'Skill deleted') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
252
        }
253
254
        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...
255
    }
256
257
}
258