Passed
Push — feature/classifications-admin-... ( a67d51...b77007 )
by Tristan
16:44 queued 08:18
created

JobPoster::criteria()   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
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Thu, 12 Jul 2018 22:39:27 +0000.
6
 */
7
8
namespace App\Models;
9
10
use App\Events\JobSaved;
11
use Illuminate\Notifications\Notifiable;
12
use Illuminate\Support\Facades\App;
13
use Illuminate\Support\Facades\Lang;
14
use Jenssegers\Date\Date;
15
use \Backpack\CRUD\CrudTrait;
16
use Astrotomic\Translatable\Translatable;
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 \Jenssegers\Date\Date $review_requested_at
28
 * @property \Jessengers\Date\Date $published_at
29
 * @property int $department_id
30
 * @property int $province_id
31
 * @property int $salary_min
32
 * @property int $salary_max
33
 * @property int $noc
34
 * @property int $classification_id
35
 * @property int $classification_level
36
 * @property int $security_clearance_id
37
 * @property int $language_requirement_id
38
 * @property boolean $remote_work_allowed
39
 * @property int $manager_id
40
 * @property boolean $published
41
 * @property int $team_size
42
 * @property array $work_env_features This should be an array of boolean flags for features, ie json of shape {[feature: string]: boolean}
43
 * @property int $fast_vs_steady
44
 * @property int $horizontal_vs_vertical
45
 * @property int $experimental_vs_ongoing
46
 * @property int $citizen_facing_vs_back_office
47
 * @property int $collaborative_vs_independent
48
 * @property int $telework_allowed_frequency_id
49
 * @property int $flexible_hours_frequency_id
50
 * @property \Jenssegers\Date\Date $created_at
51
 * @property \Jenssegers\Date\Date $updated_at
52
 *
53
 * @property int $submitted_applications_count
54
 *
55
 * @property \App\Models\Lookup\Department $department
56
 * @property \App\Models\Lookup\JobTerm $job_term
57
 * @property \App\Models\Lookup\LanguageRequirement $language_requirement
58
 * @property \App\Models\Manager $manager
59
 * @property \App\Models\Lookup\Province $province
60
 * @property \App\Models\Lookup\SecurityClearance $security_clearance
61
 * @property \Illuminate\Database\Eloquent\Collection $criteria
62
 * @property \Illuminate\Database\Eloquent\Collection $job_applications
63
 * @property \Illuminate\Database\Eloquent\Collection $job_poster_key_tasks
64
 * @property \Illuminate\Database\Eloquent\Collection $job_poster_questions
65
 * @property \Illuminate\Database\Eloquent\Collection $job_poster_translations
66
 * @property \Illuminate\Database\Eloquent\Collection $submitted_applications
67
 * @property \App\Models\Lookup\Frequency $telework_allowed_frequency
68
 * @property \App\Models\Lookup\Frequency $flexible_hours_frequency
69
 *
70
 * Localized Properties:
71
 * @property string $city
72
 * @property string $title
73
 * @property string $dept_impact
74
 * @property string $team_impact
75
 * @property string $hire_impact
76
 * @property string $branch
77
 * @property string $division
78
 * @property string $education
79
 * @property string $work_env_description
80
 * @property string $culture_summary
81
 * @property string $culture_special
82
 *
83
 * Computed Properties
84
 * @property string $classification
85
 * @property string $classification_code
86
 *
87
 * Methods
88
 * @method boolean isOpen()
89
 * @method string timeRemaining()
90
 * @method mixed[] toApiArray()
91
 */
92
class JobPoster extends BaseModel
93
{
94
    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...
95
    use Translatable;
96
    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...
97
98
    const DATE_FORMAT = [
99
        'en' => 'M jS, Y',
100
        'fr' => 'd M Y',
101
    ];
102
    const TIME_FORMAT = [
103
        'en' => 'h:i A T',
104
        'fr' => 'H \h i T',
105
    ];
106
    const TIMEZONE = 'America/Toronto';
107
108
    /**
109
     * @var string[] $translatedAttributes
110
     */
111
    public $translatedAttributes = [
112
        'city',
113
        'title',
114
        'dept_impact',
115
        'team_impact',
116
        'hire_impact',
117
        'branch',
118
        'division',
119
        'education',
120
        'work_env_description',
121
        'culture_summary',
122
        'culture_special',
123
    ];
124
125
    /**
126
     * @var string[] $casts
127
     */
128
    protected $casts = [
129
        'job_term_id' => 'int',
130
        'department_id' => 'int',
131
        'province_id' => 'int',
132
        'salary_min' => 'int',
133
        'salary_max' => 'int',
134
        'noc' => 'int',
135
        'classification_id' => 'int',
136
        'classification_level' => 'int',
137
        'security_clearance_id' => 'int',
138
        'language_requirement_id' => 'int',
139
        'remote_work_allowed' => 'boolean',
140
        'manager_id' => 'int',
141
        'published' => 'boolean',
142
        'team_size' => 'int',
143
        'work_env_features' => 'array',
144
        'fast_vs_steady' => 'int',
145
        'horizontal_vs_vertical' => 'int',
146
        'experimental_vs_ongoing' => 'int',
147
        'citizen_facing_vs_back_office' => 'int',
148
        'collaborative_vs_independent' => 'int',
149
        'telework_allowed_frequency_id' => 'int',
150
        'flexible_hours_frequency_id' => 'int',
151
    ];
152
153
    /**
154
     * @var string[] $dates
155
     */
156
    protected $dates = [
157
        'open_date_time',
158
        'close_date_time',
159
        'start_date_time',
160
        'review_requested_at',
161
        'published_at'
162
    ];
163
164
    /**
165
     * @var string[] $fillable
166
     */
167
    protected $fillable = [
168
        'job_term_id',
169
        'term_qty',
170
        'open_date_time',
171
        'close_date_time',
172
        'start_date_time',
173
        'department_id',
174
        'province_id',
175
        'salary_min',
176
        'salary_max',
177
        'noc',
178
        'classification',
179
        'classification_code',
180
        'classification_level',
181
        'security_clearance_id',
182
        'language_requirement_id',
183
        'remote_work_allowed',
184
        'published',
185
        'team_size',
186
        'work_env_features',
187
        'fast_vs_steady',
188
        'horizontal_vs_vertical',
189
        'experimental_vs_ongoing',
190
        'citizen_facing_vs_back_office',
191
        'collaborative_vs_independent',
192
        'telework_allowed_frequency_id',
193
        'flexible_hours_frequency_id',
194
    ];
195
196
    /**
197
     * The attributes that should be visible in arrays.
198
     * In this case, it blocks loaded relations from appearing.
199
     *
200
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Models\JobPoster::$visible does not specify type hint for its items.
Loading history...
201
     */
202
    protected $visible = [
203
        'id',
204
        'manager_id',
205
        'term_qty',
206
        'open_date_time',
207
        'close_date_time',
208
        'start_date_time',
209
        'department_id',
210
        'province_id',
211
        'salary_min',
212
        'salary_max',
213
        'noc',
214
        'classification_code',
215
        'classification_level',
216
        'security_clearance_id',
217
        'language_requirement_id',
218
        'remote_work_allowed',
219
        'published_at',
220
        'published',
221
        'review_requested_at',
222
        'team_size',
223
        'work_env_features',
224
        'fast_vs_steady',
225
        'horizontal_vs_vertical',
226
        'experimental_vs_ongoing',
227
        'citizen_facing_vs_back_office',
228
        'collaborative_vs_independent',
229
        'telework_allowed_frequency_id',
230
        'flexible_hours_frequency_id',
231
    ];
232
233
    /**
234
     * @var mixed[] $dispatchesEvents
235
     */
236
    protected $dispatchesEvents = [
237
        'saved' => JobSaved::class,
238
    ];
239
240
    // @codeCoverageIgnoreStart
241
    public function department() // phpcs:ignore
242
    {
243
        return $this->belongsTo(\App\Models\Lookup\Department::class);
244
    }
245
246
    public function job_term() // phpcs:ignore
247
    {
248
        return $this->belongsTo(\App\Models\Lookup\JobTerm::class);
249
    }
250
251
    public function language_requirement() // phpcs:ignore
252
    {
253
        return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class);
254
    }
255
256
    public function manager() // phpcs:ignore
257
    {
258
        return $this->belongsTo(\App\Models\Manager::class);
259
    }
260
261
    public function province() // phpcs:ignore
262
    {
263
        return $this->belongsTo(\App\Models\Lookup\Province::class);
264
    }
265
266
    public function security_clearance() // phpcs:ignore
267
    {
268
        return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class);
269
    }
270
271
    public function criteria() // phpcs:ignore
272
    {
273
        return $this->hasMany(\App\Models\Criteria::class);
274
    }
275
276
    public function job_applications() // phpcs:ignore
277
    {
278
        return $this->hasMany(\App\Models\JobApplication::class);
279
    }
280
281
    public function job_poster_key_tasks() // phpcs:ignore
282
    {
283
        return $this->hasMany(\App\Models\JobPosterKeyTask::class);
284
    }
285
286
    public function job_poster_questions() // phpcs:ignore
287
    {
288
        return $this->hasMany(\App\Models\JobPosterQuestion::class);
289
    }
290
291
    public function job_poster_translations() // phpcs:ignore
292
    {
293
        return $this->hasMany(\App\Models\JobPosterTranslation::class);
294
    }
295
296
    public function telework_allowed_frequency() // phpcs:ignore
297
    {
298
        return $this->belongsTo(\App\Models\Lookup\Frequency::class);
299
    }
300
301
    public function flexible_hours_frequency() // phpcs:ignore
302
    {
303
        return $this->belongsTo(\App\Models\Lookup\Frequency::class);
304
    }
305
306
    // Artificial Relations
307
308
    /**
309
     * Get all of the Job Applications submitted to this
310
     * Job Poster.
311
     *
312
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
313
     */
314
    public function submitted_applications() // phpcs:ignore
315
    {
316
        return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query) : void {
317
            $query->where('name', 'draft');
318
        });
319
    }
320
321
    // Overrides
322
323
    /**
324
     * Retrieve the model for a bound value.
325
     * Seems to be a useful workaround for providing submitted_applications_count
326
     * to any bound routes that receive a jobPoster instance without using the
327
     * withCount property on the model itself.
328
     * See https://github.com/laravel/framework/issues/23957 for more info.
329
     *
330
     * @param mixed $value Value used to retrieve the model instance.
331
     *
332
     * @return \Illuminate\Database\Eloquent\Model|null
333
     */
334
    public function resolveRouteBinding($value) // phpcs:ignore
335
    {
336
        return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404);
337
    }
338
339
    // @codeCoverageIgnoreEnd
340
    // Accessors.
341
342
    /**
343
     * The database model stores a foreign id to the classifiction table,
344
     * but to simplify the API, this model simply returns the key as classification_code.
345
     *
346
     * @return void
1 ignored issue
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
347
     */
348 10
    public function getClassificationCodeAttribute()
0 ignored issues
show
introduced by
Method \App\Models\JobPoster::getClassificationCodeAttribute() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
349
    {
350 10
        if ($this->classification_id !== null) {
351 10
            $classification = Classification::find($this->classification_id);
352 10
            return $classification->key;
353
        }
354 1
        return null;
355
    }
356
357
    /**
358
     * The classification property is deprecated. To ensure
359
     * Twig template consistency, check for populated
360
     * classification_code and classification_level and return
361
     * the combination of those instead.
362
     *
363
     * @param mixed $value Incoming attribute value.
364
     *
365
     * @return string|null
366
     */
367 10
    public function getClassificationAttribute($value)
0 ignored issues
show
introduced by
Method \App\Models\JobPoster::getClassificationAttribute() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string|null".
Loading history...
368
    {
369 10
        if (!empty($this->classification_code) && !empty($this->classification_level)) {
370 10
            return "$this->classification_code-$this->classification_level";
371
        } else {
372 1
            return $value;
373
        }
374
    }
375
376
    // Mutators.
377
378
    /**
379
     * This model exposes the classification_code attribute, but it is determined
380
     * by the classification_id column in the database, so set that value instead.
381
     *
382
     * @param string $value
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
383
     * @return void
384
     */
385 40
    public function setClassificationCodeAttribute($value): void
1 ignored issue
show
introduced by
Method \App\Models\JobPoster::setClassificationCodeAttribute() does not have parameter type hint for its parameter $value but it should be possible to add it based on @param annotation "string".
Loading history...
Coding Style introduced by
Type hint "string" missing for $value
Loading history...
386
    {
387 40
        $classification = Classification::where('key', $value)->first();
388 40
        if ($classification !== null) {
389 40
            $this->attributes['classification_id'] = $classification->id;
390
        } else {
391 1
            $this->attributes['classification_id'] = null;
392
        }
393 40
    }
394
395
    /**
396
     * Intercept setting the "published" attribute, and set the
397
     * "published_at" timestamp if true.
398
     *
399
     * @param mixed $value Incoming value for the 'published' attribute.
400
     *
401
     * @return void
402
     */
403 40
    public function setPublishedAttribute($value) : void
404
    {
405 40
        if ($value && $this->open_date_time->isPast()) {
406 23
            $this->attributes['published_at'] = new Date();
407 36
        } elseif ($value && $this->open_date_time->isFuture()) {
408 1
            $this->attributes['published_at'] = $this->open_date_time;
409
        }
410 40
        $this->attributes['published'] = $value;
411 40
    }
412
413
    // Methods
414 6
    public function submitted_applications_count() //phpcs:ignore
415
    {
416 6
        return $this->submitted_applications()->count();
417
    }
418
419
    /**
420
     * Formatted and localized date and time the Job Poster closes.
421
     *
422
     * @return string[]
423
     */
424 1
    public function applyBy() : array
425
    {
426 1
        $localCloseDate = new Date($this->close_date_time); // This initializes the date object in UTC time
427 1
        $localCloseDate->setTimezone(new \DateTimeZone(self::TIMEZONE)); // Then set the time zone for display
428
        $displayDate = [
429 1
            'date' => $localCloseDate->format(self::DATE_FORMAT[App::getLocale()]),
430 1
            'time' => $localCloseDate->format(self::TIME_FORMAT[App::getLocale()])
431
        ];
432
433 1
        if (App::isLocale('fr')) {
434
            $displayDate['time'] = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayDate['time']);
435
        }
436
437 1
        return $displayDate;
438
    }
439
440
    /**
441
     * Return whether the Job is Open or Closed.
442
     * Used by the Admin Portal JobPosterCrudController.
443
     *
444
     * @return string
445
     */
446
    public function displayStatus() : string
447
    {
448
        return $this->isOpen() ? 'Open' : 'Closed';
449
    }
450
451
    /**
452
     * Check if a Job Poster is open for applications.
453
     *
454
     * @return boolean
455
     */
456 6
    public function isOpen() : bool
457
    {
458 6
        return $this->published
459 6
            && $this->open_date_time->isPast()
460 6
            && $this->close_date_time->isFuture();
461
    }
462
463
    /**
464
     * Check if a Job Poster is closed for applications.
465
     *
466
     * @return boolean
467
     */
468 4
    public function isClosed() : bool
469
    {
470 4
        return $this->published
471 4
            && $this->open_date_time->isPast()
472 4
            && $this->close_date_time->isPast();
473
    }
474
475
    /**
476
     * Calculate the remaining time a Job Poster is open.
477
     *
478
     * @return string
479
     */
480 1
    public function timeRemaining() : string
481
    {
482 1
        $interval = $this->close_date_time->diff(Date::now());
483
484 1
        $d = $interval->d;
485 1
        $h = $interval->h;
486 1
        $m = $interval->i;
487 1
        $s = $interval->s;
488
489 1
        if ($d > 0) {
490 1
            $unit = 'day';
491 1
            $count = $d;
492 1
        } elseif ($h > 0) {
493 1
            $unit = 'hour';
494 1
            $count = $h;
495 1
        } elseif ($m > 0) {
496 1
            $unit = 'minute';
497 1
            $count = $m;
498
        } else {
499
            $unit = 'second';
500
            $count = $s;
501
        }
502
503 1
        $key = "common/time.$unit";
504
505 1
        return Lang::choice($key, $count);
506
    }
507
508
    /**
509
     * Return the current status for the Job Poster.
510
     * Possible values are "draft", "submitted", "published" and "closed".
511
     *
512
     * @return string
513
     */
514 3
    public function status() : string
515
    {
516 3
        $status = 'draft';
517 3
        if ($this->isOpen()) {
518 1
            $status = 'published';
519 3
        } elseif ($this->isClosed()) {
520 1
            $status = 'closed';
521 3
        } elseif ($this->review_requested_at !== null) {
522 3
            $status = 'submitted';
523
        } else {
524 1
            $status = 'draft';
525
        }
526
527 3
        return $status;
528
    }
529
530
    /**
531
     * Return the array of values used to represent this object in an api response.
532
     * This array should contain no nested objects (besides translations).
533
     *
534
     * @return mixed[]
535
     */
536 4
    public function toApiArray(): array
537
    {
538 4
        $jobWithTranslations = array_merge($this->toArray(), $this->getTranslationsArray());
539 4
        return $jobWithTranslations;
540
    }
541
}
542