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

ReferencesController::destroy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
nc 4
nop 2
crap 12
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\Reference;
12
use App\Models\Project;
13
14
class ReferencesController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ReferencesController
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 references
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_04_references', [
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_references'),
41
            'form_submit_action' => route('profile.references.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

41
            'form_submit_action' => route('profile.references.update', /** @scrutinizer ignore-type */ $applicant),
Loading history...
42
        ]);
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...
43
    }
44
45
    /**
46
     * Update the applicant's references in storage.
47
     *
48
     * @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...
49
     * @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...
50
     * @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...
51
     */
52
    public function update(Request $request, Applicant $applicant)
53
    {
54
        $input = $request->input();
55
56
        Debugbar::info($input);
57
58
        $references = $input['references'];
59
60
        //Delete old references that weren't resubmitted
61
        //Note: this must be done before adding new references, so we don't delete
62
        // them right after adding them
63
        foreach($applicant->references as $oldReference) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
64
            //Check if no references were resubmitted, or if this specific one wasn't
65
            if (!isset($references['old']) ||
66
                !isset($references['old'][$oldReference->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...
67
                $oldReference->delete();
68
            }
69
        }
70
71
        //Save new references
72
        if (isset($references['new'])) {
73
            foreach($references['new'] as $referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
74
                $reference = new Reference();
75
                $reference->applicant_id = $applicant->id;
76
                $reference->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...
77
                    'name' => $referenceInput['name'],
78
                    'email' => $referenceInput['email'],
79
                    'relationship_id' => $referenceInput['relationship_id'],
80
                    'description' => $referenceInput['description'],
81
                ]);
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...
82
83
                $reference->save();
84
85
                $projectIds = [];
86
                $projects = $referenceInput['projects'];
87
                if (isset($projects['new'])) {
88
                    foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
89
                        $project = new Project();
90
                        $project->applicant_id = $applicant->id;
91
                        $project->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...
92
                            'name' => $projectInput['name'],
93
                            'start_date' => $projectInput['start_date'],
94
                            'end_date' => $projectInput['end_date'],
95
                        ]);
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...
96
                        $project->save();
97
                        $projectIds[] = $project->id;
98
                    }
99
                }
100
                //Sync attaches the specified ids, and detaches all others
101
                $reference->projects()->sync($projectIds);
102
103
104
                $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
105
                $reference->skill_declarations()->sync($skillDeclarationIds);
106
            }
107
        }
108
109
        //Update old references
110
        if (isset($references['old'])) {
111
            foreach($references['old'] as $id=>$referenceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
112
                //Ensure this reference belongs to this applicant
113
                $reference = $applicant->references->firstWhere('id', $id);
114
                if ($reference != null) {
115
                    $reference->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...
116
                        'name' => $referenceInput['name'],
117
                        'email' => $referenceInput['email'],
118
                        'relationship_id' => $referenceInput['relationship_id'],
119
                        'description' => $referenceInput['description'],
120
                    ]);
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...
121
                    $reference->save();
122
123
                    $projectIds = [];
124
                    $projects = $referenceInput['projects'];
125
                    if (isset($projects['new'])) {
126
                        foreach($projects['new'] as $projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
127
                            $project = new Project();
128
                            $project->applicant_id = $applicant->id;
129
                            $project->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...
130
                                'name' => $projectInput['name'],
131
                                'start_date' => $projectInput['start_date'],
132
                                'end_date' => $projectInput['end_date'],
133
                            ]);
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...
134
                            $project->save();
135
                            $projectIds[] = $project->id;
136
                        }
137
                    }
138
                    if (isset($projects['old'])) {
139
                        foreach($projects['old'] as $projectId=>$projectInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
140
                            //Ensure this project belongs to this applicant
141
                            $project = $applicant->projects->firstWhere('id', $projectId);
142
                            if ($project != null) {
143
                                $project->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...
144
                                    'name' => $projectInput['name'],
145
                                    'start_date' => $projectInput['start_date'],
146
                                    'end_date' => $projectInput['end_date'],
147
                                ]);
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...
148
                                $project->save();
149
                                $projectIds[] = $project->id;
150
                            }
151
                        }
152
                    }
153
                    //TODO: when projects exists independpently on profile, don't delete them Here
154
                    // Delete projects that will be detached from this reference
155
                    foreach($reference->projects as $project) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
156
                        if (!in_array($project->id, $projectIds)) {
157
                            $project->delete();
158
                        }
159
                    }
160
161
                    //Sync attaches the specified ids, and detaches all others
162
                    $reference->projects()->sync($projectIds);
163
164
                    $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills');
165
                    $reference->skill_declarations()->sync($skillDeclarationIds);
166
                } else {
167
                    Debugbar::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id);
168
                }
169
            }
170
        }
171
172
        return redirect( route('profile.references.edit', $applicant) );
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...ces.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

172
        return redirect( route('profile.references.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
173
    }
174
175
    /**
176
     * Delete the particular reference from storage.
177
     *
178
     * @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...
179
     * @param  \App\Models\Reference  $reference
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...
180
     * @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...
181
     */
182
    public function destroy(Request $request, Reference $reference)
183
    {
184
        $this->authorize('delete', $reference);
185
186
        //TODO: when projects exist independently on profile, delete seperatley
187
        foreach($reference->projects as $project) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
188
            $project->delete();
189
        }
190
191
        $reference->delete();
192
193
        if($request->ajax()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
194
            return [
1 ignored issue
show
Bug Best Practice introduced by
The expression return array('message' => 'Reference deleted') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
195
                "message" => 'Reference deleted'
196
            ];
197
        }
198
199
        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...
200
    }
201
202
}
203