Passed
Push — task/all-applications-to-pdf ( cb7375...852a46 )
by Tristan
59:34 queued 50:51
created

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