Passed
Push — feature/backpack ( e1a886...673f22 )
by Tristan
08:37
created

JobPoster   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
wmc 25
eloc 98
dl 0
loc 235
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A job_poster_translations() 0 3 1
A province() 0 3 1
A job_applications() 0 3 1
A language_requirement() 0 3 1
A submitted_applications_count() 0 3 1
A job_term() 0 3 1
A timeRemaining() 0 26 4
A department() 0 3 1
A applyBy() 0 14 2
A manager() 0 3 1
A criteria() 0 3 1
A isOpen() 0 5 3
A submitted_applications() 0 4 1
A job_poster_questions() 0 3 1
A security_clearance() 0 3 1
A screening_plans() 0 3 1
A status() 0 3 2
A job_poster_key_tasks() 0 3 1
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Thu, 12 Jul 2018 22:39:27 +0000.
6
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
7
8
namespace App\Models;
9
10
use App\Events\JobSaved;
11
use App\Models\JobApplication;
12
use Illuminate\Notifications\Notifiable;
13
use Illuminate\Support\Facades\App;
14
use Illuminate\Support\Facades\Lang;
15
use Jenssegers\Date\Date;
16
use \Backpack\CRUD\CrudTrait;
17
18
/**
19
 * Class JobPoster
20
 *
21
 * @property int $id
22
 * @property int $job_term_id
23
 * @property int $term_qty
24
 * @property \Jenssegers\Date\Date $open_date_time
25
 * @property \Jenssegers\Date\Date $close_date_time
26
 * @property \Jenssegers\Date\Date $start_date_time
27
 * @property int $department_id
28
 * @property int $province_id
29
 * @property int $salary_min
30
 * @property int $salary_max
31
 * @property int $noc
32
 * @property string $classification
33
 * @property int $security_clearance_id
34
 * @property int $language_requirement_id
35
 * @property boolean $remote_work_allowed
36
 * @property int $manager_id
37
 * @property boolean $published
38
 * @property \Jenssegers\Date\Date $created_at
39
 * @property \Jenssegers\Date\Date $updated_at
40
 *
41
 * @property int $submitted_applications_count
42
 *
43
 * @property \App\Models\Lookup\Department $department
44
 * @property \App\Models\Lookup\JobTerm $job_term
45
 * @property \App\Models\Lookup\LanguageRequirement $language_requirement
46
 * @property \App\Models\Manager $manager
47
 * @property \App\Models\Lookup\Province $province
48
 * @property \App\Models\Lookup\SecurityClearance $security_clearance
49
 * @property \Illuminate\Database\Eloquent\Collection $criteria
50
 * @property \Illuminate\Database\Eloquent\Collection $job_applications
51
 * @property \Illuminate\Database\Eloquent\Collection $job_poster_key_tasks
52
 * @property \Illuminate\Database\Eloquent\Collection $job_poster_questions
53
 * @property \Illuminate\Database\Eloquent\Collection $job_poster_translations
54
 * @property \Illuminate\Database\Eloquent\Collection $submitted_applications
55
 * @property \Illuminate\Database\Eloquent\Collection[ScreeningPlan] $screening_plans
56
 *
57
 * Localized Properties:
58
 * @property string $city
59
 * @property string $title
60
 * @property string $impact
61
 * @property string $branch
62
 * @property string $division
63
 * @property string $education
64
 *
65
 * Methods
66
 * @method boolean isOpen()
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 1
Loading history...
67
 * @method string timeRemaining()
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 1
Loading history...
68
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment \Illuminate\Database\Elo...llection[ScreeningPlan] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
69
class JobPoster extends BaseModel
70
{
71
72
    use CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\CrudTrait requires some properties which are not provided by App\Models\JobPoster: $Type, $fakeColumns
Loading history...
73
    use \Dimsav\Translatable\Translatable;
0 ignored issues
show
introduced by
The trait Dimsav\Translatable\Translatable requires some properties which are not provided by App\Models\JobPoster: $translations, $useTranslationFallback, $translationModel, $localeKey, $translationForeignKey
Loading history...
74
    use Notifiable;
0 ignored issues
show
introduced by
The trait Illuminate\Notifications\Notifiable requires some properties which are not provided by App\Models\JobPoster: $email, $phone_number
Loading history...
75
76
    const DATE_FORMAT = [
77
        'en' => 'M jS, Y',
78
        'fr' => 'd M Y',
79
    ];
80
    const TIME_FORMAT = [
81
        'en' => 'h:i A T',
82
        'fr' => 'H \h i T',
83
    ];
84
    const TIMEZONE = 'America/Toronto';
85
86
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
87
     * @var string[] $translatedAttributes
88
     */
89
    public $translatedAttributes = [
90
        'city',
91
        'title',
92
        'impact',
93
        'branch',
94
        'division',
95
        'education'
96
    ];
97
98
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
99
     * @var string[] $casts
100
     */
101
    protected $casts = [
102
        'job_term_id' => 'int',
103
        'department_id' => 'int',
104
        'province_id' => 'int',
105
        'salary_min' => 'int',
106
        'salary_max' => 'int',
107
        'noc' => 'int',
108
        'security_clearance_id' => 'int',
109
        'language_requirement_id' => 'int',
110
        'remote_work_allowed' => 'boolean',
111
        'manager_id' => 'int',
112
        'published' => 'boolean'
113
    ];
114
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
116
     * @var string[] $dates
117
     */
118
    protected $dates = [
119
        'open_date_time',
120
        'close_date_time',
121
        'start_date_time'
122
    ];
123
124
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
125
     * @var string[] $fillable
126
     */
127
    protected $fillable = [
128
        'job_term_id',
129
        'term_qty',
130
        'open_date_time',
131
        'close_date_time',
132
        'start_date_time',
133
        'department_id',
134
        'province_id',
135
        'salary_min',
136
        'salary_max',
137
        'noc',
138
        'classification',
139
        'security_clearance_id',
140
        'language_requirement_id',
141
        'remote_work_allowed',
142
        'published'
143
    ];
144
    // protected $withCount = ['submitted_applications'];
145
146
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
147
     * @var mixed[] $dispatchesEvents
148
     */
149
    protected $dispatchesEvents = [
150
        'saved' => JobSaved::class,
151
    ];
152
153
    // @codeCoverageIgnoreStart
154
155
    public function department() // phpcs:ignore
156
    {
157
        return $this->belongsTo(\App\Models\Lookup\Department::class);
158
    }
159
160
    public function job_term() // phpcs:ignore
161
    {
162
        return $this->belongsTo(\App\Models\Lookup\JobTerm::class);
163
    }
164
165
    public function language_requirement() // phpcs:ignore
166
    {
167
        return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class);
168
    }
169
170
    public function manager() // phpcs:ignore
171
    {
172
        return $this->belongsTo(\App\Models\Manager::class);
173
    }
174
175
    public function province() // phpcs:ignore
176
    {
177
        return $this->belongsTo(\App\Models\Lookup\Province::class);
178
    }
179
180
    public function security_clearance() // phpcs:ignore
181
    {
182
        return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class);
183
    }
184
185
    public function criteria() // phpcs:ignore
186
    {
187
        return $this->hasMany(\App\Models\Criteria::class);
188
    }
189
190
    public function job_applications() // phpcs:ignore
191
    {
192
        return $this->hasMany(\App\Models\JobApplication::class);
193
    }
194
195
    public function job_poster_key_tasks() // phpcs:ignore
196
    {
197
        return $this->hasMany(\App\Models\JobPosterKeyTask::class);
198
    }
199
200
    public function job_poster_questions() // phpcs:ignore
201
    {
202
        return $this->hasMany(\App\Models\JobPosterQuestion::class);
203
    }
204
205
    public function job_poster_translations() // phpcs:ignore
206
    {
207
        return $this->hasMany(\App\Models\JobPosterTranslation::class);
208
    }
209
210
    public function screening_plans() // phpcs:ignore
211
    {
212
        return $this->hasMany(\App\Models\ScreeningPlan::class);
213
    }
214
215
    // Artificial Relations
216
217
    public function submitted_applications()
0 ignored issues
show
Coding Style introduced by
Public method name "JobPoster::submitted_applications" is not in camel caps format
Loading history...
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
218
    {
219
        return $this->job_applications()->whereDoesntHave('application_status', function ($query) {
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...
220
            $query->where('name', 'draft');
221
        });
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...
222
    }
223
224
    // @codeCoverageIgnoreEnd
225
226
    // Accessors
227
228
    // Methods
229
230 6
    public function submitted_applications_count()
0 ignored issues
show
Coding Style introduced by
Public method name "JobPoster::submitted_applications_count" is not in camel caps format
Loading history...
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
231
    {
232 6
        return $this->submitted_applications()->count();
233
    }
234
235
    /**
236
     * Formatted and localized date and time the Job Poster closes.
237
     *
238
     * @return string[]
239
     */
240 6
    public function applyBy() : array
241
    {
242 6
        $localCloseDate = new Date($this->close_date_time); // This initializes the date object in UTC time
243 6
        $localCloseDate->setTimezone(new \DateTimeZone(self::TIMEZONE)); // Then set the time zone for display
244
        $displayDate = [
245 6
            'date' => $localCloseDate->format(self::DATE_FORMAT[App::getLocale()]),
246 6
            'time' => $localCloseDate->format(self::TIME_FORMAT[App::getLocale()])
247
        ];
248
249 6
        if (App::isLocale('fr')) {
250
            $displayDate['time'] = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayDate['time']);
251
        }
252
253 6
        return $displayDate;
254
    }
255
256
    public function status()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function status()
Loading history...
257
    {
258
        return $this->isOpen() ? 'Open' : 'Closed';
259
    }
260
261
    /**
262
     * Check if a Job Poster is open for applications.
263
     *
264
     * @return boolean
265
     */
266 6
    public function isOpen() : bool
267
    {
268 6
        return $this->published
269 6
            && $this->open_date_time->isPast()
270 6
            && $this->close_date_time->isFuture();
271
    }
272
273
    /**
274
     * Calculate the remaining time a Job Poster is open.
275
     *
276
     * @return string
277
     */
278 2
    public function timeRemaining() : string
279
    {
280 2
        $interval = $this->close_date_time->diff(Date::now());
281
282 2
        $d = $interval->d;
283 2
        $h = $interval->h;
284 2
        $m = $interval->i;
285 2
        $s = $interval->s;
286
287 2
        if ($d > 0) {
288 2
            $unit = 'day';
289 2
            $count = $d;
290 2
        } elseif ($h > 0) {
291 2
            $unit = 'hour';
292 2
            $count = $h;
293 2
        } elseif ($m > 0) {
294 2
            $unit = 'minute';
295 2
            $count = $m;
296
        } else {
297 2
            $unit = 'second';
298 2
            $count = $s;
299
        }
300
301 2
        $key = "common/time.$unit";
302
303 2
        return Lang::choice($key, $count);
304
    }
305
}
306