Passed
Push — feature/job-env-culture ( 4b95ce...7a7dee )
by Tristan
13:52 queued 06:29
created

JobPoster::province()   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
17
/**
18
 * Class JobPoster
19
 *
20
 * @property int $id
21
 * @property int $job_term_id
22
 * @property int $term_qty
23
 * @property \Jenssegers\Date\Date $open_date_time
24
 * @property \Jenssegers\Date\Date $close_date_time
25
 * @property \Jenssegers\Date\Date $start_date_time
26
 * @property \Jenssegers\Date\Date $review_requested_at
27
 * @property \Jessengers\Date\Date $published_at
28
 * @property int $department_id
29
 * @property int $province_id
30
 * @property int $salary_min
31
 * @property int $salary_max
32
 * @property int $noc
33
 * @property string $classification
34
 * @property string $classification_code
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
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 $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
 * 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 \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...
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
        'impact',
111
        'team_impact',
112
        'hire_impact',
113
        'branch',
114
        'division',
115
        'education',
116
        'work_env_description',
117
        'culture_summary',
118
        'culture_special',
119
    ];
120
121
    /**
122
     * @var string[] $casts
123
     */
124
    protected $casts = [
125
        'job_term_id' => 'int',
126
        'department_id' => 'int',
127
        'province_id' => 'int',
128
        'salary_min' => 'int',
129
        'salary_max' => 'int',
130
        'noc' => 'int',
131
        'classification_code' => 'string',
132
        'classification_level' => 'int',
133
        'security_clearance_id' => 'int',
134
        'language_requirement_id' => 'int',
135
        'remote_work_allowed' => 'boolean',
136
        'manager_id' => 'int',
137
        'published' => 'boolean',
138
        'team_size' => 'int',
139
        'work_env_features' => 'array',
140
        'fast_vs_steady' => 'int',
141
        'horizontal_vs_vertical' => 'int',
142
        'experimental_vs_ongoing' => 'int',
143
        'citizen_facing_vs_back_office' => 'int',
144
        'collaborative_vs_independent' => 'int',
145
        'telework_allowed_frequency_id' => 'int',
146
        'flexible_hours_frequency_id' => 'int',
147
    ];
148
149
    /**
150
     * @var string[] $dates
151
     */
152
    protected $dates = [
153
        'open_date_time',
154
        'close_date_time',
155
        'start_date_time',
156
        'review_requested_at',
157
        'published_at'
158
    ];
159
160
    /**
161
     * @var string[] $fillable
162
     */
163
    protected $fillable = [
164
        'job_term_id',
165
        'term_qty',
166
        'open_date_time',
167
        'close_date_time',
168
        'start_date_time',
169
        'department_id',
170
        'province_id',
171
        'salary_min',
172
        'salary_max',
173
        'noc',
174
        'classification',
175
        'classification_code',
176
        'classification_level',
177
        'security_clearance_id',
178
        'language_requirement_id',
179
        'remote_work_allowed',
180
        'published',
181
        'team_size',
182
        'work_env_features',
183
        'fast_vs_steady',
184
        'horizontal_vs_vertical',
185
        'experimental_vs_ongoing',
186
        'citizen_facing_vs_back_office',
187
        'collaborative_vs_independent',
188
        'telework_allowed_frequency_id',
189
        'flexible_hours_frequency_id',
190
    ];
191
192
    /**
193
     * @var mixed[] $dispatchesEvents
194
     */
195
    protected $dispatchesEvents = [
196
        'saved' => JobSaved::class,
197
    ];
198
199
    // @codeCoverageIgnoreStart
200
    public function department() // phpcs:ignore
201
    {
202
        return $this->belongsTo(\App\Models\Lookup\Department::class);
203
    }
204
205
    public function job_term() // phpcs:ignore
206
    {
207
        return $this->belongsTo(\App\Models\Lookup\JobTerm::class);
208
    }
209
210
    public function language_requirement() // phpcs:ignore
211
    {
212
        return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class);
213
    }
214
215
    public function manager() // phpcs:ignore
216
    {
217
        return $this->belongsTo(\App\Models\Manager::class);
218
    }
219
220
    public function province() // phpcs:ignore
221
    {
222
        return $this->belongsTo(\App\Models\Lookup\Province::class);
223
    }
224
225
    public function security_clearance() // phpcs:ignore
226
    {
227
        return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class);
228
    }
229
230
    public function criteria() // phpcs:ignore
231
    {
232
        return $this->hasMany(\App\Models\Criteria::class);
233
    }
234
235
    public function job_applications() // phpcs:ignore
236
    {
237
        return $this->hasMany(\App\Models\JobApplication::class);
238
    }
239
240
    public function job_poster_key_tasks() // phpcs:ignore
241
    {
242
        return $this->hasMany(\App\Models\JobPosterKeyTask::class);
243
    }
244
245
    public function job_poster_questions() // phpcs:ignore
246
    {
247
        return $this->hasMany(\App\Models\JobPosterQuestion::class);
248
    }
249
250
    public function job_poster_translations() // phpcs:ignore
251
    {
252
        return $this->hasMany(\App\Models\JobPosterTranslation::class);
253
    }
254
255
    public function telework_allowed_frequency() // phpcs:ignore
256
    {
257
        return $this->belongsTo(\App\Models\Lookup\Frequency::class);
258
    }
259
260
    public function flexible_hours_frequency() // phpcs:ignore
261
    {
262
        return $this->belongsTo(\App\Models\Lookup\Frequency::class);
263
    }
264
265
    // Artificial Relations
266
267
    /**
268
     * Get all of the Job Applications submitted to this
269
     * Job Poster.
270
     *
271
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
272
     */
273
    public function submitted_applications() // phpcs:ignore
274
    {
275
        return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query) : void {
276
            $query->where('name', 'draft');
277
        });
278
    }
279
280
    // Overrides
281
282
    /**
283
     * Retrieve the model for a bound value.
284
     * Seems to be a useful workaround for providing submitted_applications_count
285
     * to any bound routes that receive a jobPoster instance without using the
286
     * withCount property on the model itself.
287
     * See https://github.com/laravel/framework/issues/23957 for more info.
288
     *
289
     * @param mixed $value Value used to retrieve the model instance.
290
     *
291
     * @return \Illuminate\Database\Eloquent\Model|null
292
     */
293
    public function resolveRouteBinding($value) // phpcs:ignore
294
    {
295
        return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404);
296
    }
297
298
    // @codeCoverageIgnoreEnd
299
    // Accessors
300
    // Mutators
301
302
    /**
303
     * Intercept setting the "published" attribute, and set the
304
     * "published_at" timestamp if true.
305
     *
306
     * @param mixed $value Incoming value for the 'published' attribute.
307
     *
308
     * @return void
309
     */
310
    public function setPublishedAttribute($value) : void
311
    {
312
        if ($value && $this->open_date_time->isPast()) {
313
            $this->attributes['published_at'] = new Date();
314
        } elseif ($value && $this->open_date_time->isFuture()) {
315
            $this->attributes['published_at'] = $this->open_date_time;
316
        }
317
        $this->attributes['published'] = $value;
318
    }
319
320
    // Methods
321
    public function submitted_applications_count()
1 ignored issue
show
introduced by
Method \App\Models\JobPoster::submitted_applications_count() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
Coding Style introduced by
Method name "JobPoster::submitted_applications_count" is not in camel caps format
Loading history...
322
    {
323
        return $this->submitted_applications()->count();
324
    }
325
326
    /**
327
     * Formatted and localized date and time the Job Poster closes.
328
     *
329
     * @return string[]
330
     */
331
    public function applyBy() : array
332
    {
333
        $localCloseDate = new Date($this->close_date_time); // This initializes the date object in UTC time
334
        $localCloseDate->setTimezone(new \DateTimeZone(self::TIMEZONE)); // Then set the time zone for display
335
        $displayDate = [
336
            'date' => $localCloseDate->format(self::DATE_FORMAT[App::getLocale()]),
337
            'time' => $localCloseDate->format(self::TIME_FORMAT[App::getLocale()])
338
        ];
339
340
        if (App::isLocale('fr')) {
341
            $displayDate['time'] = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayDate['time']);
342
        }
343
344
        return $displayDate;
345
    }
346
347
    /**
348
     * Return whether the Job is Open or Closed.
349
     * Used by the Admin Portal JobPosterCrudController.
350
     *
351
     * @return string
352
     */
353
    public function displayStatus() : string
354
    {
355
        return $this->isOpen() ? 'Open' : 'Closed';
356
    }
357
358
    /**
359
     * Check if a Job Poster is open for applications.
360
     *
361
     * @return boolean
362
     */
363
    public function isOpen() : bool
364
    {
365
        return $this->published
366
            && $this->open_date_time->isPast()
367
            && $this->close_date_time->isFuture();
368
    }
369
370
    /**
371
     * Check if a Job Poster is closed for applications.
372
     *
373
     * @return boolean
374
     */
375
    public function isClosed() : bool
376
    {
377
        return $this->published
378
            && $this->open_date_time->isPast()
379
            && $this->close_date_time->isPast();
380
    }
381
382
    /**
383
     * Calculate the remaining time a Job Poster is open.
384
     *
385
     * @return string
386
     */
387
    public function timeRemaining() : string
388
    {
389
        $interval = $this->close_date_time->diff(Date::now());
390
391
        $d = $interval->d;
392
        $h = $interval->h;
393
        $m = $interval->i;
394
        $s = $interval->s;
395
396
        if ($d > 0) {
397
            $unit = 'day';
398
            $count = $d;
399
        } elseif ($h > 0) {
400
            $unit = 'hour';
401
            $count = $h;
402
        } elseif ($m > 0) {
403
            $unit = 'minute';
404
            $count = $m;
405
        } else {
406
            $unit = 'second';
407
            $count = $s;
408
        }
409
410
        $key = "common/time.$unit";
411
412
        return Lang::choice($key, $count);
413
    }
414
415
    /**
416
     * Return the current status for the Job Poster.
417
     * Possible values are "draft", "submitted", "published" and "closed".
418
     *
419
     * @return string
420
     */
421
    public function status() : string
422
    {
423
        $status = 'draft';
424
        if ($this->isOpen()) {
425
            $status = 'published';
426
        } elseif ($this->isClosed()) {
427
            $status = 'closed';
428
        } elseif ($this->review_requested_at !== null) {
429
            $status = 'submitted';
430
        } else {
431
            $status = 'draft';
432
        }
433
434
        return $status;
435
    }
436
437
    /**
438
     * Return the array of values used to represent this object in an api response.
439
     * This array should contain no nested objects (besides translations).
440
     *
441
     * @return mixed[]
442
     */
443
    public function toApiArray(): array
444
    {
445
        $jobWithTranslations = array_merge($this->toArray(), $this->getTranslationsArray());
446
        $jobCollection = collect($jobWithTranslations)->only([
447
            'id',
448
            'manager_id',
449
            'term_qty',
450
            'open_date_time',
451
            'close_date_time',
452
            'start_date_time',
453
            'department_id',
454
            'province_id',
455
            'salary_min',
456
            'salary_max',
457
            'noc',
458
            'classification_code',
459
            'classification_level',
460
            'security_clearance_id',
461
            'language_requirement_id',
462
            'remote_work_allowed',
463
            'published_at',
464
            'review_requested_at',
465
            'en',
466
            'fr',
467
        ])->all();
468
        return $jobCollection;
469
    }
470
}
471