Passed
Push — master ( 69ee29...385ff7 )
by Grant
16:52
created

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