Passed
Pull Request — dev (#313)
by Tristan
06:51
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)
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
        $newDegrees = [];
63
        $newCourses = [];
64
        $newWorkExperiences = [];
65
66
        $oldDegrees = [];
67
        $oldCourses = [];
68
        $oldWorkExperiences = [];
69
70
        /**
0 ignored issues
show
Coding Style introduced by
Parameter $applicant should have a doc-comment as per coding-style.
Loading history...
71
         * If an obj exists at $array[$key], return it.
72
         * Otherwise, set $array[$key] to be a new instance of $class,
73
         * set applicant_id, and return it.
74
         *
75
         * @param  [type] $array [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
76
         * @param  [type] $key   [description]
77
         * @param  [type] $class [description]
78
         * @return [type]        [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
79
         */
80
        function getModelFromArrayOrSetNew(&$array, $key, $class, $applicant) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
81
            if (isset($array[$key]) && $array[$key] != null ) {
82
                $obj = $array[$key];
83
            } else {
84
                $obj = new $class;
85
                $obj->applicant_id = $applicant->id;
86
                $array[$key] = $obj;
87
            }
88
            return $obj;
89
        }
90
91
        /**
0 ignored issues
show
Coding Style introduced by
Parameter $applicant should have a doc-comment as per coding-style.
Loading history...
92
         * For each item in an array, create a new model instance and set the
93
         * specified attribute with the value in the array.
94
         *
95
         * If an instance alread exists in the $instances array that matches
96
         * the $inputArray key, then set the attribute on that instance instead
97
         * of creating a new instance.
98
         *
99
         * @param array $inputArray An array of input key-values
0 ignored issues
show
Coding Style introduced by
Expected 37 spaces after parameter type; 1 found
Loading history...
100
         * @param array $instances    Array of previously created Model instances.
0 ignored issues
show
Coding Style introduced by
Expected 37 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
101
         * @param string $model      Fully qualified class name.
0 ignored issues
show
Coding Style introduced by
Expected 36 spaces after parameter type; 1 found
Loading history...
102
         * @param string $attribute  Name of attribute to set in model.
0 ignored issues
show
Coding Style introduced by
Expected 36 spaces after parameter type; 1 found
Loading history...
103
         * @param \App\Models\Applicant Owner of the object
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Owner was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Documentation introduced by
Missing parameter name
Loading history...
104
         */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
105
        function setAttrFromInputNew($inputArray, &$instances, $model, $attribute, $applicant) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
Opening brace should be on a new line
Loading history...
106
            if (isset($inputArray) && is_array($inputArray)) {
107
                foreach($inputArray as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
108
                    $obj = getModelFromArrayOrSetNew($instances, $key, $model, $applicant);
109
                    $obj->setAttribute($attribute, $value);
110
                    Debugbar::info($instances);
111
                }
112
            }
113
        }
114
115
        /**
0 ignored issues
show
Coding Style introduced by
Parameter $input should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $inputField should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $applicant should have a doc-comment as per coding-style.
Loading history...
116
         * For each item in an input array, retrieve the modelName instance with
117
         * id matching input key, and belonging to $applicant, and set $attribute
118
         * on that model instance with the inputArray value.
119
         *
120
         * Retrieved instances are cached in $instances array, so they don't
121
         * have to be retrieved from database multiple times.
122
         *
123
         * @param [type] $inputArray [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
Coding Style introduced by
Doc comment for parameter $inputArray does not match actual variable name $input
Loading history...
124
         * @param [type] $instances  [description]
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $instances does not match actual variable name $inputField
Loading history...
125
         * @param [type] $modelName  [description]
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $modelName does not match actual variable name $instances
Loading history...
126
         * @param [type] $attribute  [description]
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $attribute does not match actual variable name $modelName
Loading history...
127
         */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
128
        function setAttrFromInputOld($input, $inputField, &$instances, $modelName, $attribute, $applicant) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
Opening brace should be on a new line
Loading history...
129
            if (isset($input[$inputField]) &&
130
                is_array($input[$inputField]) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
131
                isset($input[$inputField]['old']) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
132
                is_array($input[$inputField]['old']) ) {
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...
133
134
                $inputArray = $input[$inputField]['old'];
135
                Debugbar::info($inputArray);
136
                foreach($inputArray as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
137
                    if (isset($instances[$key])) {
138
                        $obj = $instances[$key];
139
                    } else {
140
                        $model = new $modelName;
141
                        $obj = $model->newQuery()->where('applicant_id', $applicant->id)
142
                            ->where('id', $key)->first();
143
                        $instances[$key] = $obj;
144
                    }
145
                    if ($obj != null) {
146
                        $obj->setAttribute($attribute, $value);
147
                    }
148
                }
149
            }
150
        }
151
152
        function get(&$var, $default=null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
Missing doc comment for function get()
Loading history...
Coding Style introduced by
Opening brace should be on a new line
Loading history...
153
            return isset($var) ? $var : $default;
154
        }
155
156
        // New Entitities
157
        setAttrFromInputNew(get($input['degree_type']['new']), $newDegrees,'App\Models\Degree', 'degree_type_id', $applicant);
158
        setAttrFromInputNew(get($input['degree_area']['new']), $newDegrees,'App\Models\Degree', 'area_of_study', $applicant);
159
        setAttrFromInputNew(get($input['degree_institution']['new']), $newDegrees,'App\Models\Degree', 'institution', $applicant);
160
        setAttrFromInputNew(get($input['degree_thesis']['new']), $newDegrees,'App\Models\Degree', 'thesis', $applicant);
161
        setAttrFromInputNew(get($input['degree_start_date']['new']), $newDegrees,'App\Models\Degree', 'start_date', $applicant);
162
        setAttrFromInputNew(get($input['degree_end_date']['new']), $newDegrees,'App\Models\Degree', 'end_date', $applicant);
163
164
        setAttrFromInputNew(get($input['course_name']['new']), $newCourses,'App\Models\Course', 'name', $applicant);
165
        setAttrFromInputNew(get($input['course_institution']['new']), $newCourses,'App\Models\Course', 'institution', $applicant);
166
        setAttrFromInputNew(get($input['course_status']['new']), $newCourses,'App\Models\Course', 'course_status_id', $applicant);
167
        setAttrFromInputNew(get($input['course_start_date']['new']), $newCourses,'App\Models\Course', 'start_date', $applicant);
168
        setAttrFromInputNew(get($input['course_end_date']['new']), $newCourses,'App\Models\Course', 'end_date', $applicant);
169
170
        setAttrFromInputNew(get($input['work_role']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'role', $applicant);
171
        setAttrFromInputNew(get($input['work_company']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'company', $applicant);
172
        setAttrFromInputNew(get($input['work_description']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'description', $applicant);
173
        setAttrFromInputNew(get($input['work_start_date']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'start_date', $applicant);
174
        setAttrFromInputNew(get($input['work_end_date']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'end_date', $applicant);
175
176
        // PreExisting entities
177
        setAttrFromInputOld($input, 'degree_type', $oldDegrees,'App\Models\Degree', 'degree_type_id', $applicant);
178
        setAttrFromInputOld($input, 'degree_area', $oldDegrees,'App\Models\Degree', 'area_of_study', $applicant);
179
        setAttrFromInputOld($input, 'degree_institution', $oldDegrees,'App\Models\Degree', 'institution', $applicant);
180
        setAttrFromInputOld($input, 'degree_thesis', $oldDegrees,'App\Models\Degree', 'thesis', $applicant);
181
        setAttrFromInputOld($input, 'degree_start_date', $oldDegrees,'App\Models\Degree', 'start_date', $applicant);
182
        setAttrFromInputOld($input, 'degree_end_date', $oldDegrees,'App\Models\Degree', 'end_date', $applicant);
183
184
        setAttrFromInputOld($input, 'course_name', $oldCourses,'App\Models\Course', 'name', $applicant);
185
        setAttrFromInputOld($input, 'course_institution', $oldCourses,'App\Models\Course', 'institution', $applicant);
186
        setAttrFromInputOld($input, 'course_status', $oldCourses,'App\Models\Course', 'course_status_id', $applicant);
187
        setAttrFromInputOld($input, 'course_start_date', $oldCourses,'App\Models\Course', 'start_date', $applicant);
188
        setAttrFromInputOld($input, 'course_end_date', $oldCourses,'App\Models\Course', 'end_date', $applicant);
189
190
        setAttrFromInputOld($input, 'work_role', $oldWorkExperiences,'App\Models\WorkExperience', 'role', $applicant);
191
        setAttrFromInputOld($input, 'work_company', $oldWorkExperiences,'App\Models\WorkExperience', 'company', $applicant);
192
        setAttrFromInputOld($input, 'work_description', $oldWorkExperiences,'App\Models\WorkExperience', 'description', $applicant);
193
        setAttrFromInputOld($input, 'work_start_date', $oldWorkExperiences,'App\Models\WorkExperience', 'start_date', $applicant);
194
        setAttrFromInputOld($input, 'work_end_date', $oldWorkExperiences,'App\Models\WorkExperience', 'end_date', $applicant);
195
196
        //Delete old entities that weren't resubmitted
197
        foreach($applicant->degrees as $degree) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
198
            Debugbar::info($oldCourses);
199
            Debugbar::info($degree);
200
            if (!isset($oldDegrees[$degree->id]) || $oldDegrees[$degree->id] === null) {
201
                Debugbar::info('Deleting degree ' . $degree->id);
202
                $degree->delete();
203
            }
204
        }
205
        foreach($applicant->courses as $course) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
206
            if (!isset($oldCourses[$course->id]) || $oldCourses[$course->id] === null) {
207
                $course->delete();
208
            }
209
        }
210
        foreach($applicant->work_experiences as $work) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
211
            if (!isset($oldWorkExperiences[$work->id]) || $oldWorkExperiences[$work->id] === null) {
212
                $work->delete();
213
            }
214
        }
215
216
        //Save new entities
217
        foreach($newDegrees as $entity) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
218
            $entity->save();
219
        }
220
        foreach($newCourses as $entity) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
221
            $entity->save();
222
        }
223
        foreach($newWorkExperiences as $entity) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
224
            $entity->save();
225
        }
226
227
        //Save old entities
228
        foreach($oldDegrees as $entity) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
229
            $entity->save();
230
        }
231
        foreach($oldCourses as $entity) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
232
            $entity->save();
233
        }
234
        foreach($oldWorkExperiences as $entity) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
235
            $entity->save();
236
        }
237
238
        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...
239
        //Debugbar::info($oldDegrees);
240
        // return view('applicant/profile_02_experience', [
241
        //     'applicant' => $applicant,
242
        //     'profile' => Lang::get('applicant/profile_experience'),
243
        //     'degree_types' => DegreeType::all(),
244
        //     'course_status' => CourseStatus::all(),
245
        //     'degree_template' => Lang::get('common/degree'),
246
        //     'course_template' => Lang::get('common/course'),
247
        //     'work_template' => Lang::get('common/work_experience'),
248
        //     'form_submit_action' => route('profile.experience.update', $applicant),
249
        // ]);
250
    }
251
252
}
253