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

setAttrFromInputOld()   B

Complexity

Conditions 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 8.4444
c 0
b 0
f 0
cc 8
crap 72
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\Degree;
9
use App\Models\Applicant;
10
use App\Models\Course;
11
use App\Models\WorkExperience;
12
use App\Models\Lookup\DegreeType;
13
use App\Models\Lookup\CourseStatus;
14
use App\Http\Controllers\Controller;
15
16
class ExperienceController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ExperienceController
Loading history...
17
{
18
19
    /**
20
     * Display the Experience 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
     * Show the form for editing the applicant's experience
32
     *
33
     * @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...
34
     * @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...
35
     * @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...
36
     */
37
    public function edit(Request $request, Applicant $applicant)
38
    {
39
        return view('applicant/profile_02_experience', [
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...
40
            'applicant' => $applicant,
41
            'profile' => Lang::get('applicant/profile_experience'),
42
            'degree_types' => DegreeType::all(),
43
            'course_status' => CourseStatus::all(),
44
            'degree_template' => Lang::get('common/degree'),
45
            'course_template' => Lang::get('common/course'),
46
            'work_template' => Lang::get('common/work_experience'),
47
            'form_submit_action' => route('profile.experience.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

47
            'form_submit_action' => route('profile.experience.update', /** @scrutinizer ignore-type */ $applicant)
Loading history...
48
        ]);
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...
49
    }
50
51
    /**
52
     * Update the applicant's profile in storage.
53
     *
54
     * @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...
55
     * @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...
56
     * @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...
57
     */
58
    public function update(Request $request, Applicant $applicant)
59
    {
60
        $input = $request->input();
61
62
        $degrees = $input['degrees'];
63
64
        //Delete old degrees that weren't resubmitted
65
        //Note: this must be done before adding new degrees, so we don't delete
66
        // them right after adding them
67
        foreach($applicant->degrees as $oldDegree) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
68
            //Check if no degrees were resubmitted, or if this specific one wasn't
69
            if (!isset($degrees['old']) ||
70
                !isset($degrees['old'][$oldDegree->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...
71
                $oldDegree->delete();
72
            }
73
        }
74
75
        //Save new degrees
76
        if (isset($degrees['new'])) {
77
            foreach($degrees['new'] as $degreeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
78
                $degree = new Degree();
79
                $degree->applicant_id = $applicant->id;
80
                $degree->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...
81
                    'degree_type_id' => $degreeInput['degree_type_id'],
82
                    'area_of_study' => $degreeInput['area_of_study'],
83
                    'institution' => $degreeInput['institution'],
84
                    'thesis' => $degreeInput['thesis'],
85
                    'start_date' => $degreeInput['start_date'],
86
                    'end_date' => $degreeInput['end_date']
87
                ]);
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...
88
                $degree->save();
89
            }
90
        }
91
92
        //Update old degrees
93
        if (isset($degrees['old'])) {
94
            foreach($degrees['old'] as $id=>$degreeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
95
                //Ensure this degree belongs to this applicant
96
                $degree = $applicant->degrees->firstWhere('id', $id);
97
                if ($degree != null) {
98
                    $degree->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
                        'degree_type_id' => $degreeInput['degree_type_id'],
100
                        'area_of_study' => $degreeInput['area_of_study'],
101
                        'institution' => $degreeInput['institution'],
102
                        'thesis' => $degreeInput['thesis'],
103
                        'start_date' => $degreeInput['start_date'],
104
                        'end_date' => $degreeInput['end_date']
105
                    ]);
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...
106
                    $degree->save();
107
                } else {
108
                    Debugbar::warning('Applicant '.$applicant->id.' attempted to update degree with invalid id '.$id);
109
                }
110
            }
111
        }
112
113
        $courses = $input['courses'];
114
115
        //Delete old courses that weren't resubmitted
116
        //Note: this must be done before adding new ones, so we don't delete
117
        // them right after adding them
118
        foreach($applicant->courses as $oldCourse) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
119
            //Check if no courses were resubmitted, or if this specific one wasn't
120
            if (!isset($courses['old']) ||
121
                !isset($courses['old'][$oldCourse->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...
122
                $oldCourse->delete();
123
            }
124
        }
125
126
        //Save new courses
127
        if (isset($courses['new'])) {
128
            foreach($courses['new'] as $courseInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
129
                $course = new Course();
130
                $course->applicant_id = $applicant->id;
131
                $course->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...
132
                    'name' => $courseInput['name'],
133
                    'institution' => $courseInput['institution'],
134
                    'course_status_id' => $courseInput['course_status_id'],
135
                    'start_date' => $courseInput['start_date'],
136
                    'end_date' => $courseInput['end_date']
137
                ]);
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...
138
                $course->save();
139
            }
140
        }
141
142
        //Update old courses
143
        if (isset($courses['old'])) {
144
            foreach($courses['old'] as $id=>$courseInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
145
                //Ensure this course belongs to this applicant
146
                $course = $applicant->courses->firstWhere('id', $id);
147
                if ($course != null) {
148
                    $course->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...
149
                        'name' => $courseInput['name'],
150
                        'institution' => $courseInput['institution'],
151
                        'course_status_id' => $courseInput['course_status_id'],
152
                        'start_date' => $courseInput['start_date'],
153
                        'end_date' => $courseInput['end_date']
154
                    ]);
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...
155
                    $course->save();
156
                } else {
157
                    Debugbar::warning('Applicant '.$applicant->id.' attempted to update course with invalid id '.$id);
158
                }
159
            }
160
        }
161
162
        $work_experiences = $input['work_experiences'] ;
163
164
        //Delete old work_experiences that weren't resubmitted
165
        //Note: this must be done before adding new ones, so we don't delete
166
        // them right after adding them
167
        foreach($applicant->work_experiences as $oldWorkExperience) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
168
            //Check if no work_experiences were resubmitted, or if this specific one wasn't
169
            if (!isset($work_experiences['old']) ||
170
                !isset($work_experiences['old'][$oldWorkExperience->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...
171
                $oldWorkExperience->delete();
172
            }
173
        }
174
175
        //Save new work_experiences
176
        if (isset($work_experiences['new'])) {
177
            foreach($work_experiences['new'] as $workExperienceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
178
                $workExperience = new WorkExperience();
179
                $workExperience->applicant_id = $applicant->id;
180
                $workExperience->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...
181
                    'role' => $workExperienceInput['role'],
182
                    'company' => $workExperienceInput['company'],
183
                    'description' => $workExperienceInput['description'],
184
                    'start_date' => $workExperienceInput['start_date'],
185
                    'end_date' => $workExperienceInput['end_date']
186
                ]);
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...
187
                $workExperience->save();
188
            }
189
        }
190
191
        //Update old work_experiences
192
        if (isset($work_experiences['old'])) {
193
            foreach($work_experiences['old'] as $id=>$workExperienceInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
194
                //Ensure this work_experience belongs to this applicant
195
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
196
                if ($workExperience != null) {
197
                    $workExperience->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...
198
                        'role' => $workExperienceInput['role'],
199
                        'company' => $workExperienceInput['company'],
200
                        'description' => $workExperienceInput['description'],
201
                        'start_date' => $workExperienceInput['start_date'],
202
                        'end_date' => $workExperienceInput['end_date']
203
                    ]);
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...
204
                    $workExperience->save();
205
                } else {
206
                    Debugbar::warning('Applicant '.$applicant->id.' attempted to update work_experience with invalid id '.$id);
207
                }
208
            }
209
        }
210
211
        return redirect( route('profile.experience.edit', $applicant) );
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...nce.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

211
        return redirect( route('profile.experience.edit', /** @scrutinizer ignore-type */ $applicant) );
Loading history...
212
        // Debugbar::info($input);
213
        // return view('applicant/profile_02_experience', [
214
        //     'applicant' => $applicant->fresh(),
215
        //     'profile' => Lang::get('applicant/profile_experience'),
216
        //     'degree_types' => DegreeType::all(),
217
        //     'course_status' => CourseStatus::all(),
218
        //     'degree_template' => Lang::get('common/degree'),
219
        //     'course_template' => Lang::get('common/course'),
220
        //     'work_template' => Lang::get('common/work_experience'),
221
        //     'form_submit_action' => route('profile.experience.update', $applicant)
222
        // ]);
223
    }
224
225
}
226