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

WorkSamplesController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
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\Http\Request;
7
use Barryvdh\Debugbar\Facade as Debugbar;
8
use App\Http\Controllers\Controller;
9
use App\Models\Skill;
10
use App\Models\Applicant;
11
use App\Models\WorkSample;
12
13
class WorkSamplesController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class WorkSamplesController
Loading history...
14
{
15
16
    /**
17
     * Display the Work Samples associated with the applicant.
18
     *
19
     * @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...
20
     * @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...
21
     */
22
    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

22
    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...
23
    {
24
        //
25
26
    }
27
28
    /**
29
     * Show the form for editing the applicant's work samples
30
     *
31
     * @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...
32
     * @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...
33
     * @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...
34
     */
35
    public function edit(Request $request, Applicant $applicant)
36
    {
37
        return view('applicant/profile_05_portfolio', [
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('applicant/p....update', $applicant))) 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...
38
            'applicant' => $applicant,
39
            'profile' => Lang::get('applicant/profile_work_samples'),
40
            'form_submit_action' => route('profile.work_samples.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

40
            'form_submit_action' => route('profile.work_samples.update', /** @scrutinizer ignore-type */ $applicant),
Loading history...
41
        ]);
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...
42
    }
43
44
    /**
45
     * Update the applicant's workSamples in storage.
46
     *
47
     * @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...
48
     * @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...
49
     * @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...
50
     */
51
    public function update(Request $request, Applicant $applicant)
52
    {
53
54
        $input = $request->input();
55
56
        $workSamples = $input['work_samples'];
57
58
        //Delete old workSamples that weren't resubmitted
59
        //Note: this must be done before adding new workSamples, so we don't delete
60
        // them right after adding them
61
        foreach($applicant->work_samples as $oldWorkSample) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
62
            //Check if no workSamples were resubmitted, or if this specific one wasn't
63
            if (!isset($workSamples['old']) ||
64
                !isset($workSamples['old'][$oldWorkSample->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...
65
                $oldWorkSample->delete();
66
            }
67
        }
68
69
        //Save new workSamples
70
        if (isset($workSamples['new'])) {
71
            foreach($workSamples['new'] as $workSampleInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
72
                $workSample = new WorkSample();
73
                $workSample->applicant_id = $applicant->id;
74
                $workSample->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...
75
                    'name' => $workSampleInput['name'],
76
                    'date_created' => isset($workSampleInput['date_created']) ? $workSampleInput['date_created'] : null,
77
                    'file_type_id' => $workSampleInput['file_type_id'],
78
                    'url' => $workSampleInput['url'],
79
                    'description' => $workSampleInput['description'],
80
                ]);
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...
81
82
                $workSample->save();
83
84
                $skillDeclarationIds =$this->getRelativeIds($workSampleInput, 'skills');
85
                $workSample->skill_declarations()->sync($skillDeclarationIds);
86
            }
87
        }
88
89
        //Update old workSamples
90
        if (isset($workSamples['old'])) {
91
            foreach($workSamples['old'] as $id=>$workSampleInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
92
                //Ensure this workSample belongs to this applicant
93
                $workSample = $applicant->work_samples->firstWhere('id', $id);
94
                if ($workSample != null) {
95
                    $workSample->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...
96
                        'name' => $workSampleInput['name'],
97
                        'date_created' => isset($workSampleInput['date_created']) ? $workSampleInput['date_created'] : null,
98
                        'file_type_id' => $workSampleInput['file_type_id'],
99
                        'url' => $workSampleInput['url'],
100
                        'description' => $workSampleInput['description'],
101
                    ]);
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...
102
                    $workSample->save();
103
104
                    $skillDeclarationIds =$this->getRelativeIds($workSampleInput, 'skills');
105
                    $workSample->skill_declarations()->sync($skillDeclarationIds);
106
                } else {
107
                    Debugbar::warning('Applicant '.$applicant->id.' attempted to update workSample with invalid id '.$id);
108
                }
109
            }
110
        }
111
112
        return redirect( route('profile.work_samples.edit', $applicant) );
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...les.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

112
        return redirect( route('profile.work_samples.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
113
    }
114
115
    /**
116
     * Delete the particular work sample from storage.
117
     *
118
     * @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...
119
     * @param  \App\Models\WorkSample  $workSample
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 2 found
Loading history...
120
     * @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...
121
     */
122
    public function destroy(Request $request, WorkSample $workSample)
123
    {
124
        $this->authorize('delete', $workSample);
125
126
        $workSample->delete();
127
128
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
129
            return [
1 ignored issue
show
Bug Best Practice introduced by
The expression return array('message' => 'Work sample deleted') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
130
                "message" => 'Work sample deleted'
131
            ];
132
        }
133
134
        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...
135
    }
136
137
}
138