Completed
Push — feature/delete_items ( 8d7f82 )
by Tristan
31:05 queued 19:23
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
ccs 0
cts 6
cp 0
rs 10
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\Http\Request;
7
use Barryvdh\Debugbar\Facade as Debugbar;
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
14
class SkillsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillsController
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 skills
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
        $skills = Skill::all();
39
        return view('applicant/profile_03_skills', [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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...
40
            'applicant' => $applicant,
41
            'profile' => Lang::get('applicant/profile_skills'),
42
            '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

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

127
        return redirect( route('profile.skills.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
128
    }
129
130
    /**
131
     * Delete the particular skill declaration in storage.
132
     *
133
     * @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...
134
     * @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...
135
     * @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...
136
     */
137
    public function destroy(Request $request, SkillDeclaration $skillDeclaration)
138
    {
139
        $skillDeclaration->delete();
140
        debugbar()->info('deleted skill declaration '.$skillDeclaration->id);
141
142
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
143
            return 'Skill deleted';
1 ignored issue
show
Bug Best Practice introduced by
The expression return 'Skill deleted' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
144
        }
145
146
        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...
147
    }
148
149
}
150