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

JobController::store()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 93
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 93
ccs 0
cts 61
cp 0
rs 7.1862
c 0
b 0
f 0
cc 9
nc 8
nop 1
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Barryvdh\Debugbar\Facade as Debugbar;
9
use App\Http\Controllers\Controller;
10
use Carbon\Carbon;
11
use App\Models\JobPoster;
12
use App\Models\Lookup\JobTerm;
13
use App\Models\Lookup\Province;
14
use App\Models\Lookup\SecurityClearance;
15
use App\Models\Lookup\LanguageRequirement;
16
use App\Models\Lookup\Department;
17
use App\Models\Lookup\SkillLevel;
18
use App\Models\Lookup\CriteriaType;
19
use App\Models\Criteria;
20
use App\Models\Skill;
21
use App\Models\JobPosterQuestion;
22
use App\Models\JobPosterKeyTask;
23
use Jenssegers\Date\Date;
24
25
class JobController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JobController
Loading history...
26
{
27
    /**
28
     * Display a listing of JobPosters.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function index()
33
    {
34
        $now = Carbon::now();
35
        //Find published jobs that are currently open for applications
36
        $jobs = JobPoster::where('open_date_time', '<=', $now)
37
            ->where('close_date_time', '>=', $now)
38
            ->where('published', true)
39
            ->get();
40
        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...
41
            '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...
42
    }
43
44
    /**
45
     * Display the specified job poster.
46
     *
47
     * @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...
48
     * @param  \App\Models\JobPoster  $jobPoster
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
     * @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...
50
     */
51
    public function show(Request $request, JobPoster $jobPoster)
52
    {
53
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
54
        $workplacePhotos = [];
55
        foreach($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
56
            $workplacePhotos[] = [
57
                'description' => $photoCaption->description,
58
                'url' => '/images/user.png'
59
            ];
60
        }
61
62
        //TODO: replace route('manager.show',manager.id) in templates with link using slug
63
64
        $criteria = [
65
            'essential' => $jobPoster->criteria->filter(function($value, $key) {
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...
66
                return $value->criteria_type->name == 'essential';
67
            }),
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...
68
            'asset' => $jobPoster->criteria->filter(function($value, $key) {
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...
69
                return $value->criteria_type->name == 'asset';
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
        ];
72
        Debugbar::info(Criteria::all());
73
        return view('applicant/job_post', [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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...
74
            'job_post' =>Lang::get('applicant/job_post'),
75
            'manager' => $jobPoster->manager,
76
            'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
77
            'team_culture' => $jobPoster->manager->team_culture,
78
            'work_environment' => $jobPoster->manager->work_environment,
79
            'workplace_photos' => $workplacePhotos,
80
            'job' => $jobPoster,
81
            'criteria' => $criteria,
82
            'skill_template' => Lang::get('common/skills'),
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
86
    /**
87
     * Display the form for creating a new Job Poster
88
     * @param  Request $request [description]
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
89
     * @return \Illuminate\Http\Response           A view
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
90
     */
91
    public function create(Request $request) {
92
        $manager = $request->user() ? $request->user()->manager : null;
93
94
        //No job details exist yet because we're creating a new one
95
        $job = [];
96
97
        return view('manager/job_create', [
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...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
98
            'job_create' => Lang::get('manager/job_create'),
99
            'manager' => $manager,
100
            'provinces' => Province::all(),
101
            'departments' => Department::all(),
102
            'language_requirments' => LanguageRequirement::all(),
103
            'security_clearances' => SecurityClearance::all(),
104
            'job' => $job,
105
            'form_action_url' => route('manager.jobs.store'),
106
            'skills' => Skill::all(),
107
            'skill_levels' => SkillLevel::all(),
108
            'skill_template' => Lang::get('common/skills'),
109
        ]);
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...
110
    }
111
112
    /**
113
     * Create a new resource in storage
114
     * @param  Request $request
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
115
     * @return \Illuminate\Http\Response           A redirect
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
116
     */
117
    public function store(Request $request) {
118
119
        $input = $request->input();
120
121
        $job = new JobPoster();
122
        $job->manager_id = $request->user()->manager->id;
123
        $job->published = ($input['submit'] == 'publish');
124
        $job->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...
125
            'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id,
126
            'term_qty' => $input['term_qty'],
127
            'open_date_time' => new Date($input['open_date_time']),
128
            'close_date_time' => new Date($input['close_date_time']),
129
            'start_date_time' => new Date($input['start_date_time']),
130
            'department_id' => $input['department'],
131
            'province_id' => $input['province'],
132
            'salary_min' => $input['salary_min'],
133
            'salary_max' => $input['salary_max'],
134
            'noc' => $input['noc'],
135
            'classification' => $input['classification'],
136
            'security_clearance_id' => $input['security_clearance'],
137
            'language_requirement_id' => $input['language_requirement'],
138
            'en' => [
139
                'city' => $input['city'],
140
                'title' => $input['title']['en'],
141
                'impact' => $input['impact']['en'],
142
                'branch' => $input['branch']['en'],
143
                'division' => $input['division']['en']
144
            ],
145
            'fr' => [
146
                'city' => $input['city'],
147
                'title' => $input['title']['fr'],
148
                'impact' => $input['impact']['fr'],
149
                'branch' => $input['branch']['fr'],
150
                'division' => $input['division']['fr']
151
            ],
152
        ]);
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...
153
        $job->save();
154
155
        if (isset($input['task'])) {
156
            foreach($input['task'] as $task) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
157
                $jobPosterTask = new JobPosterKeyTask();
158
                $jobPosterTask->job_poster_id =  $job->id;
159
                $jobPosterTask->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...
160
                    'en' => [
161
                        'description' => $task['en']
162
                    ],
163
                    'fr' => [
164
                        'description' => $task['fr']
165
                    ]
166
                ]);
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...
167
                $jobPosterTask->save();
168
            }
169
        }
170
171
        if (isset($input['question'])) {
172
            foreach($input['question'] as $question) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
173
                $jobQuestion = new JobPosterQuestion();
174
                $jobQuestion->job_poster_id = $job->id;
175
                $jobQuestion->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...
176
                    'en'=> [
177
                        'question' => $question['question']['en'],
178
                        'description' => $question['description']['en']
179
                    ],
180
                    'fr'=> [
181
                        'question' => $question['question']['fr'],
182
                        'description' => $question['description']['fr']
183
                    ]
184
                ]);
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...
185
                $jobQuestion->save();
186
            }
187
        }
188
189
        $criteria = $input['criteria'];
190
191
        //Save new criteria
192
        if (isset($criteria['new'])) {
193
            foreach($criteria['new'] as $criteriaType => $criteriaTypeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
194
                foreach($criteriaTypeInput as $skillType => $skillTypeInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
195
                    foreach($skillTypeInput as $criteriaInput) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
196
                        $criteria = new Criteria();
197
                        $criteria->job_poster_id = $job->id;
198
                        $criteria->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...
199
                            'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id,
200
                            'skill_id' => $criteriaInput['skill_id'],
201
                            'skill_level_id' => $criteriaInput['skill_level_id'],
202
                        ]);
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...
203
                        $criteria->save();
204
                    }
205
                }
206
            }
207
        }
208
209
        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...
210
    }
211
}
212