Completed
Pull Request — dev (#319)
by Tristan
07:50
created

WorkSamplesController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
eloc 42
dl 0
loc 102
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 2 1
A edit() 0 8 1
B update() 0 62 11
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
use App\Models\Lookup\FileType;
13
14
class WorkSamplesController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class WorkSamplesController
Loading history...
15
{
16
17
    /**
18
     * Display the Work Samples 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 work samples
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
        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...
39
            'applicant' => $applicant,
40
            'profile' => Lang::get('applicant/profile_work_samples'), //TODO
41
            'relative_template' => Lang::get('common/relatives'),
42
            'file_types' => FileType::all(),
43
            '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

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

115
        return redirect( route('profile.work_samples.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
116
        // Debugbar::info($input);
117
        // return view('applicant/profile_04_workSamples', [
118
        //     'applicant' => $applicant,
119
        //     'profile' => Lang::get('applicant/profile_workSamples'),
120
        //     'relative_template' => Lang::get('common/relatives'),
121
        //     'skills' => Skill::all(),
122
        //     'relationships' => Relationship::all(),
123
        //     'form_submit_action' => route('profile.workSamples.update', $applicant),
124
        // ]);
125
    }
126
127
}
128