Passed
Push — feature/job-builder/step-03-ui ( dabf2e...dabf2e )
by Xander
20:05 queued 13s
created

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