Completed
Pull Request — dev (#515)
by Tristan
15:51 queued 07:35
created

SkillsController::updateAll()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 74
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 74
ccs 0
cts 42
cp 0
rs 6.2666
c 0
b 0
f 0
cc 14
nc 12
nop 2
crap 210

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SkillDeclarationValidator;
16
17
class SkillsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillsController
Loading history...
18
{
19
20
    /**
21
     * Display the Skills page associated with the applicant.
22
     *
23
     * @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...
24
     * @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...
25
     */
26
    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

26
    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...
27
    {
28
        //
29
30
    }
31
32
    /**
33
     * Show the form for editing the applicant's skills
34
     *
35
     * @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...
36
     * @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...
37
     * @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...
38
     */
39
    public function edit(Request $request, Applicant $applicant)
40
    {
41
        $skills = Skill::all();
42
43
        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...
44
            'applicant' => $applicant,
45
            'profile' => Lang::get('applicant/profile_skills'),
46
            '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

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

131
        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...
132
    }
133
134
    /**
135
     * Create the particular skill declaration in storage.
136
     *
137
     * @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...
138
     * @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...
139
     */
140
    public function create(Request $request)
141
    {
142
        $this->authorize('create', SkillDeclaration::class);
143
144
        $user = $request->user();
145
        $applicant = $user->applicant;
146
147
        //Get the default claim status id
148
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
149
150
        // Create a new Skill Declaration
151
        // But don't save, as it hasn't been validated yet
152
        $skillDeclaration = new SkillDeclaration();
153
        $skillDeclaration->applicant_id = $applicant->id;
154
        $skillDeclaration->skill_id = $request->input('skill_id');
155
        $skillDeclaration->skill_status_id = $claimedStatusId;
156
157
        //Update variable fields in skill declaration
158
        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...
159
    }
160
161
    /**
162
     * Update the particular skill declaration in storage.
163
     *
164
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
165
     * @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...
166
     * @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...
167
     */
168
    public function update(Request $request, SkillDeclaration $skillDeclaration)
169
    {
170
        $this->authorize('update', $skillDeclaration);
171
172
        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...
173
    }
174
175
    protected function updateSkillDeclaration(Request $request, SkillDeclaration $skillDeclaration)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function updateSkillDeclaration()
Loading history...
176
    {
177
        //Fill variable values
178
        $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...
179
            'description' => $request->input('description'),
180
            'skill_level_id' => $request->input('skill_level_id'),
181
        ]);
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...
182
183
        //Validate before saving
184
        $validator = new SkillDeclarationValidator($request->user()->applicant);
185
        $validator->validate($skillDeclaration);
186
187
        //Save this skill declaration
188
        $skillDeclaration->save();
189
190
        //Attach relatives
191
        $referenceIds = $this->getRelativeIds($request->input(), 'references');
192
        $skillDeclaration->references()->sync($referenceIds);
193
194
        $sampleIds = $this->getRelativeIds($request->input(), 'samples');
195
        $skillDeclaration->work_samples()->sync($sampleIds);
196
197
        // If an ajax request, return the new object
198
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
199
            $skillDeclaration->load('references');
200
            $skillDeclaration->load('work_samples');
201
            $skillDeclaration->load('skill');
202
            $skillDeclaration->load('skill_status');
203
            return $skillDeclaration->toJson();
204
        }
205
206
        return redirect()->back();
207
    }
208
209
    /**
210
     * Delete the particular skill declaration in storage.
211
     *
212
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
213
     * @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...
214
     * @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...
215
     */
216
    public function destroy(Request $request, SkillDeclaration $skillDeclaration)
217
    {
218
        $this->authorize('delete', $skillDeclaration);
219
        $skillDeclaration->delete();
220
221
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
222
            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...
223
        }
224
225
        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...
226
    }
227
228
}
229