Passed
Push — allow-managers-to-edit-job-pos... ( df5250...dee288 )
by
unknown
31:30 queued 23:35
created

JobController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\Support\Facades\Auth;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use Carbon\Carbon;
10
use App\Models\JobPoster;
11
use App\Models\Lookup\JobTerm;
12
use App\Models\Lookup\Province;
13
use App\Models\Lookup\SecurityClearance;
14
use App\Models\Lookup\LanguageRequirement;
15
use App\Models\Lookup\CitizenshipDeclaration;
16
use App\Models\Lookup\Department;
17
use App\Models\Lookup\SkillLevel;
18
use App\Models\Lookup\CriteriaType;
19
use App\Models\Lookup\VeteranStatus;
20
use App\Models\JobApplication;
21
use App\Models\Criteria;
22
use App\Models\Skill;
23
use App\Models\JobPosterQuestion;
24
use App\Models\JobPosterKeyTask;
25
use App\Services\Validation\JobPosterValidator;
26
use Jenssegers\Date\Date;
27
28
class JobController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JobController
Loading history...
29
{
30
    /**
31
     * Display a listing of JobPosters.
32
     *
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function index()
36
    {
37
        $now = Carbon::now();
38
        //Find published jobs that are currently open for applications
39
        $jobs = JobPoster::where('open_date_time', '<=', $now)
40
            ->where('close_date_time', '>=', $now)
41
            ->where('published', true)
42
            ->get();
43
        $jobs->load('manager.work_environment');
44
        return view('applicant/job_index', ['job_index' => Lang::get('applicant/job_index'),
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('applicant/j...ex'), 'jobs' => $jobs)) 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...
45
            'jobs' => $jobs]);
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...
46
    }
47
48
    /**
49
     * Display a listing of a manager's JobPosters.
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function managerIndex()
54
    {
55
        $manager = Auth::user()->manager;
0 ignored issues
show
Bug introduced by
Accessing manager on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56
57
        $veteran_applications = [];
58
        $citizen_applications = [];
59
        $other_applications = [];
60
61
        foreach($manager->job_posters as $job) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
62
            $job->submitted_applications->load(['veteran_status', 'citizenship_declaration']);
63
            $veteran_applications[$job->id] = $job->submitted_applications->filter(function($application) {
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...
64
                return $application->veteran_status->name !== "none" &&
65
                        $application->citizenship_declaration->name === "citizen";
66
            });
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...
67
            $citizen_applications[$job->id] = $job->submitted_applications->filter(function($application) {
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...
68
                return $application->veteran_status->name === "none" &&
69
                        $application->citizenship_declaration->name === "citizen";
70
            });
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...
71
            $other_applications[$job->id] = $job->submitted_applications->filter(function($application) {
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...
72
                return $application->citizenship_declaration->name !== "citizen";
73
            });
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...
74
        }
75
76
        return view('manager/job_index', [
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('manager/job...> $other_applications)) 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...
77
            "manager_job_index" => [
78
                "title" => "My Job Posts"
79
            ],
80
            'jobs' => $manager->job_posters,
81
            'veteran_applications' => $veteran_applications,
82
            'citizen_applications' => $citizen_applications,
83
            'other_applications' => $other_applications,
84
        ]);
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...
85
    }
86
87
    /**
88
     * Display the specified job poster.
89
     *
90
     * @param \Illuminate\Http\Request $request   Incoming request object
91
     * @param \App\Models\JobPoster    $jobPoster Job Poster object
92
     *
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function show(Request $request, JobPoster $jobPoster)
96
    {
97
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
98
        $workplacePhotos = [];
99
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
100
            $workplacePhotos[] = [
101
                'description' => $photoCaption->description,
102
                'url' => '/images/user.png'
103
            ];
104
        }
105
106
        //TODO: replace route('manager.show',manager.id) in templates with link using slug
107
108
        $criteria = [
109
            'essential' => $jobPoster->criteria->filter(
110
                function ($value, $key) {
111
                    return $value->criteria_type->name == 'essential';
112
                }
113
            ),
114
            'asset' => $jobPoster->criteria->filter(
115
                function ($value, $key) {
116
                    return $value->criteria_type->name == 'asset';
117
                }
118
            ),
119
        ];
120
        return view(
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('applicant/j...:get('common/skills'))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
121
            'applicant/job_post',
122
            [
123
                'job_post' => Lang::get('applicant/job_post'),
124
                'manager' => $jobPoster->manager,
125
                'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
126
                'team_culture' => $jobPoster->manager->team_culture,
127
                'work_environment' => $jobPoster->manager->work_environment,
128
                'workplace_photos' => $workplacePhotos,
129
                'job' => $jobPoster,
130
                'criteria' => $criteria,
131
                'skill_template' => Lang::get('common/skills'),
132
            ]
133
        );
134
    }
135
136
    /**
137
     * Display the form for creating a new Job Poster
138
     *
139
     * @param \Illuminate\Http\Request $request Incoming request object
140
     *
141
     * @return \Illuminate\Http\Response A view
142
     */
143
    public function create(Request $request)
144
    {
145
        return $this->populateCreateView($request);
146
    }
147
148
    /**
149
     * Display the form for editing an existing Job Poster
150
     *
151
     * @param \Illuminate\Http\Request $request   Incoming request object
152
     * @param \App\Models\JobPoster    $jobPoster Job Poster object
153
     *
154
     * @return \Illuminate\Http\Response A view
155
     */
156
    public function edit(Request $request, JobPoster $jobPoster)
157
    {
158
        return $this->populateCreateView($request, $jobPoster);
159
    }
160
161
    /**
162
     * Get the manager from the request object and check if creating or editing
163
     *
164
     * @param \Illuminate\Http\Request $request   Incoming request object
165
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object
166
     *
167
     * @return \Illuminate\Http\Response A view
168
     */
169
    public function populateCreateView(Request $request, JobPoster $jobPoster = null)
170
    {
171
        $manager = $request->user() ? $request->user()->manager : null;
172
        if (isset($jobPoster)) {
173
            $job = $jobPoster;
174
            $route = ['manager.jobs.store', $jobPoster];
175
            $jobHeading = 'manager/job_edit';
176
        } else {
177
            $job = [];
178
            $route = ['manager.jobs.store'];
179
            $jobHeading = 'manager/job_create';
180
        }
181
182
        return view(
1 ignored issue
show
Bug Best Practice introduced by
The expression return view('manager/job...:get('common/skills'))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
183
            'manager/job_create',
184
            [
185
                'job_heading' => Lang::get($jobHeading),
186
                'manager' => $manager,
187
                'provinces' => Province::all(),
188
                'departments' => Department::all(),
189
                'language_requirments' => LanguageRequirement::all(),
190
                'security_clearances' => SecurityClearance::all(),
191
                'job' => $job,
192
                'form_action_url' => route(...$route),
0 ignored issues
show
Bug introduced by
$route is expanded, but the parameter $name of route() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

192
                'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route),
Loading history...
193
                'skills' => Skill::all(),
194
                'skill_levels' => SkillLevel::all(),
195
                'skill_template' => Lang::get('common/skills'),
196
            ]
197
        );
198
    }
199
200
    /**
201
     * Create a new resource in storage
202
     *
203
     * @param \Illuminate\Http\Request $request   Incoming request object
204
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object
205
     *
206
     * @return \Illuminate\Http\Response A redirect
207
     */
208
    public function store(Request $request, JobPoster $jobPoster = null)
209
    {
210
        // Don't allow edits for published Job Posters
211
        if (isset($jobPoster)) {
212
            JobPosterValidator::validateUnpublished($jobPoster);
213
        }
214
215
        $input = $request->input();
216
217
        $job = (isset($jobPoster) ? $jobPoster : new JobPoster());
218
219
        $job->manager_id = $request->user()->manager->id;
220
        $job->published = ($input['submit'] == 'publish');
221
        $job->fill(
222
            [
223
                'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id,
224
                'term_qty' => $input['term_qty'],
225
                'open_date_time' => new Date($input['open_date'] . $input['open_time']),
226
                'close_date_time' => new Date($input['close_date'] . $input['close_time']),
227
                'start_date_time' => new Date($input['start_date_time']),
228
                'department_id' => $input['department'],
229
                'province_id' => $input['province'],
230
                'salary_min' => $input['salary_min'],
231
                'salary_max' => $input['salary_max'],
232
                'noc' => $input['noc'],
233
                'classification' => $input['classification'],
234
                'security_clearance_id' => $input['security_clearance'],
235
                'language_requirement_id' => $input['language_requirement'],
236
                'remote_work_allowed' => $request->input('remote_work_allowed', false),
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array|null|string expected by parameter $default of Illuminate\Http\Request::input(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
                'remote_work_allowed' => $request->input('remote_work_allowed', /** @scrutinizer ignore-type */ false),
Loading history...
237
                'en' => [
238
                    'city' => $input['city'],
239
                    'title' => $input['title']['en'],
240
                    'impact' => $input['impact']['en'],
241
                    'branch' => $input['branch']['en'],
242
                    'division' => $input['division']['en'],
243
                    'education' => $input['education']['en'],
244
                ],
245
                'fr' => [
246
                    'city' => $input['city'],
247
                    'title' => $input['title']['fr'],
248
                    'impact' => $input['impact']['fr'],
249
                    'branch' => $input['branch']['fr'],
250
                    'division' => $input['division']['fr'],
251
                    'education' => $input['education']['fr'],
252
                ],
253
            ]
254
        );
255
        $job->save();
256
257
        if (isset($jobPoster)) {
258
            $jobPoster->job_poster_key_tasks()->delete();
259
        }
260
261
        if (isset($input['task'])) {
262
            foreach ($input['task'] as $task) {
263
                $jobPosterTask = new JobPosterKeyTask();
264
                $jobPosterTask->job_poster_id = $job->id;
265
                $jobPosterTask->fill(
266
                    [
267
                        'en' => [
268
                            'description' => $task['en']
269
                        ],
270
                        'fr' => [
271
                            'description' => $task['fr']
272
                        ]
273
                    ]
274
                );
275
                $jobPosterTask->save();
276
            }
277
        }
278
279
        if (isset($jobPoster)) {
280
            $jobPoster->job_poster_questions()->delete();
281
        }
282
283
        if (isset($input['question'])) {
284
            foreach ($input['question'] as $question) {
285
                $jobQuestion = new JobPosterQuestion();
286
                $jobQuestion->job_poster_id = $job->id;
287
                $jobQuestion->fill(
288
                    [
289
                        'en' => [
290
                            'question' => $question['question']['en'],
291
                            'description' => $question['description']['en']
292
                        ],
293
                        'fr' => [
294
                            'question' => $question['question']['fr'],
295
                            'description' => $question['description']['fr']
296
                        ]
297
                    ]
298
                );
299
                $jobQuestion->save();
300
            }
301
        }
302
303
        if (isset($jobPoster)) {
304
            $jobPoster->criteria()->delete();
305
        }
306
307
        $criteria = $input['criteria'];
308
309
        //Save new criteria
310
        if (isset($criteria['new'])) {
311
            foreach ($criteria['new'] as $criteriaType => $criteriaTypeInput) {
312
                foreach ($criteriaTypeInput as $skillType => $skillTypeInput) {
313
                    foreach ($skillTypeInput as $criteriaInput) {
314
                        $criteria = new Criteria();
315
                        $criteria->job_poster_id = $job->id;
316
                        $criteria->fill(
317
                            [
318
                                'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id,
319
                                'skill_id' => $criteriaInput['skill_id'],
320
                                'skill_level_id' => $criteriaInput['skill_level_id'],
321
                                'en' => [
322
                                    'description' => $criteriaInput['description']['en'],
323
                                ],
324
                                'fr' => [
325
                                    'description' => $criteriaInput['description']['fr'],
326
                                ],
327
                            ]
328
                        );
329
                        $criteria->save();
330
                    }
331
                }
332
            }
333
        }
334
335
        return redirect(route('manager.jobs.index'));
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('manager.jobs.index')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
336
    }
337
}
338