Completed
Push — feature/ajax_skill_dec ( 475068 )
by Tristan
11:36
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\SkillStatus;
10
use App\Models\SkillDeclaration;
11
use App\Models\Applicant;
12
use App\Http\Controllers\Controller;
13
use App\Services\Validation\BulkSkillDeclarationValidator;
14
15
class SkillsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillsController
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 skills
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
        $skills = Skill::all();
40
41
        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...
42
            'applicant' => $applicant,
43
            'profile' => Lang::get('applicant/profile_skills'),
44
            '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

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

129
        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...
130
    }
131
132
    /**
133
     * Create the particular skill declaration in storage.
134
     *
135
     * @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...
136
     * @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...
137
     */
138
    public function create(Request $request)
139
    {
140
        $user = $request->user();
141
        $applicant = $user->applicant;
142
143
        debugbar()->debug($request->input());
144
145
        //Get the default claim status id
146
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
147
148
        // Create a new Skill Declaration
149
        $skillDeclaration = new SkillDeclaration();
150
        $skillDeclaration->applicant_id = $applicant->id;
151
        $skillDeclaration->skill_id = $request->input('skill_id');
152
        $skillDeclaration->skill_status_id = $claimedStatusId;
153
154
        //Fill variable values
155
        $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...
156
            'description' => $request->input('description'),
157
            'skill_level_id' => $request->input('skill_level_id'),
158
        ]);
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...
159
160
        //Save this skill declaration
161
        $skillDeclaration->save();
162
163
        //Attach relatives
164
        $referenceIds = $this->getRelativeIds($request->input(), 'references');
165
        $skillDeclaration->references()->sync($referenceIds);
166
167
        $sampleIds = $this->getRelativeIds($request->input(), 'samples');
168
        $skillDeclaration->work_samples()->sync($sampleIds);
169
170
        // If an ajax request, return the new object
171
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
172
            $skillDeclaration->load('references');
173
            $skillDeclaration->load('work_samples');
174
            return $skillDeclaration->toArray();
1 ignored issue
show
Bug Best Practice introduced by
The expression return $skillDeclaration->toArray() returns the type array which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
175
        }
176
177
        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...
178
    }
179
180
    /**
181
     * Update the particular skill declaration in storage.
182
     *
183
     * @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...
184
     * @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...
185
     * @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...
186
     */
187
    public function update(Request $request, SkillDeclaration $skillDeclaration)
188
    {
189
        $this->authorize('update', $skillDeclaration);
190
191
        //Fill variable values
192
        $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...
193
            'description' => $request->input('description'),
194
            'skill_level_id' => $request->input('skill_level_id'),
195
        ]);
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...
196
197
        //Save this skill declaration
198
        $skillDeclaration->save();
199
200
        //Attach relatives
201
        $referenceIds = $this->getRelativeIds($request->input(), 'references');
202
        $skillDeclaration->references()->sync($referenceIds);
203
204
        $sampleIds = $this->getRelativeIds($request->input(), 'samples');
205
        $skillDeclaration->work_samples()->sync($sampleIds);
206
207
        // If an ajax request, return the new object
208
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
209
            $skillDeclaration->load('references');
210
            $skillDeclaration->load('work_samples');
211
            return $skillDeclaration->toArray();
1 ignored issue
show
Bug Best Practice introduced by
The expression return $skillDeclaration->toArray() returns the type array which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
212
        }
213
214
        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...
215
    }
216
217
    /**
218
     * Delete the particular skill declaration in storage.
219
     *
220
     * @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...
221
     * @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...
222
     * @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...
223
     */
224
    public function destroy(Request $request, SkillDeclaration $skillDeclaration)
225
    {
226
        $this->authorize('delete', $skillDeclaration);
227
        $skillDeclaration->delete();
228
229
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
230
            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...
231
        }
232
233
        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...
234
    }
235
236
}
237