Passed
Push — bugfix/job_translation_fields ( dcec43...2ad85b )
by Tristan
14:10
created

SkillsController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 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 Barryvdh\Debugbar\Facade as Debugbar;
9
use App\Models\Skill;
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
16
class SkillsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillsController
Loading history...
17
{
18
19
    /**
20
     * Display the Skills page associated with the applicant.
21
     *
22
     * @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...
23
     * @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...
24
     */
25
    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

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

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

130
        return redirect( route('profile.skills.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
131
    }
132
133
    /**
134
     * Delete the particular skill declaration in storage.
135
     *
136
     * @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...
137
     * @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...
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 destroy(Request $request, SkillDeclaration $skillDeclaration)
141
    {
142
        $this->authorize('delete', $skillDeclaration);
143
        $skillDeclaration->delete();
144
145
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
146
            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...
147
        }
148
149
        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...
150
    }
151
152
}
153