Total Complexity | 58 |
Total Lines | 529 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like JobPoster often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JobPoster, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
101 | class JobPoster extends BaseModel |
||
102 | { |
||
103 | use CrudTrait; |
||
104 | use HasTranslations; |
||
105 | use Notifiable; |
||
106 | |||
107 | const DATE_FORMAT = [ |
||
108 | 'en' => 'M jS, Y', |
||
109 | 'fr' => 'd M Y', |
||
110 | ]; |
||
111 | const TIME_FORMAT = [ |
||
112 | 'en' => 'h:i A T', |
||
113 | 'fr' => 'H \h i T', |
||
114 | ]; |
||
115 | const TIMEZONE = 'America/Toronto'; |
||
116 | |||
117 | /** |
||
118 | * @var string[] $translatable |
||
119 | */ |
||
120 | public $translatable = [ |
||
121 | 'city', |
||
122 | 'title', |
||
123 | 'dept_impact', |
||
124 | 'team_impact', |
||
125 | 'hire_impact', |
||
126 | 'division', |
||
127 | 'education', |
||
128 | 'work_env_description', |
||
129 | 'culture_summary', |
||
130 | 'culture_special', |
||
131 | ]; |
||
132 | |||
133 | /** |
||
134 | * @var string[] $casts |
||
135 | */ |
||
136 | protected $casts = [ |
||
137 | 'job_term_id' => 'int', |
||
138 | 'department_id' => 'int', |
||
139 | 'province_id' => 'int', |
||
140 | 'salary_min' => 'int', |
||
141 | 'salary_max' => 'int', |
||
142 | 'noc' => 'int', |
||
143 | 'classification_id' => 'int', |
||
144 | 'classification_level' => 'int', |
||
145 | 'security_clearance_id' => 'int', |
||
146 | 'language_requirement_id' => 'int', |
||
147 | 'remote_work_allowed' => 'boolean', |
||
148 | 'manager_id' => 'int', |
||
149 | 'published' => 'boolean', |
||
150 | 'team_size' => 'int', |
||
151 | 'work_env_features' => 'array', |
||
152 | 'fast_vs_steady' => 'int', |
||
153 | 'horizontal_vs_vertical' => 'int', |
||
154 | 'experimental_vs_ongoing' => 'int', |
||
155 | 'citizen_facing_vs_back_office' => 'int', |
||
156 | 'collaborative_vs_independent' => 'int', |
||
157 | 'telework_allowed_frequency_id' => 'int', |
||
158 | 'flexible_hours_frequency_id' => 'int', |
||
159 | 'travel_requirement_id' => 'int', |
||
160 | 'overtime_requirement_id' => 'int', |
||
161 | ]; |
||
162 | |||
163 | /** |
||
164 | * @var string[] $dates |
||
165 | */ |
||
166 | protected $dates = [ |
||
167 | 'open_date_time', |
||
168 | 'close_date_time', |
||
169 | 'start_date_time', |
||
170 | 'review_requested_at', |
||
171 | 'published_at', |
||
172 | 'loo_issuance_date', |
||
173 | ]; |
||
174 | |||
175 | /** |
||
176 | * @var string[] $fillable |
||
177 | */ |
||
178 | protected $fillable = [ |
||
179 | 'job_term_id', |
||
180 | 'chosen_lang', |
||
181 | 'term_qty', |
||
182 | 'open_date_time', |
||
183 | 'close_date_time', |
||
184 | 'start_date_time', |
||
185 | 'department_id', |
||
186 | 'province_id', |
||
187 | 'salary_min', |
||
188 | 'salary_max', |
||
189 | 'noc', |
||
190 | 'security_clearance_id', |
||
191 | 'language_requirement_id', |
||
192 | 'remote_work_allowed', |
||
193 | 'published', |
||
194 | 'team_size', |
||
195 | 'work_env_features', |
||
196 | 'fast_vs_steady', |
||
197 | 'horizontal_vs_vertical', |
||
198 | 'experimental_vs_ongoing', |
||
199 | 'citizen_facing_vs_back_office', |
||
200 | 'collaborative_vs_independent', |
||
201 | 'telework_allowed_frequency_id', |
||
202 | 'flexible_hours_frequency_id', |
||
203 | 'travel_requirement_id', |
||
204 | 'overtime_requirement_id', |
||
205 | 'process_number', |
||
206 | 'priority_clearance_number', |
||
207 | 'loo_issuance_date', |
||
208 | 'classification_id', |
||
209 | 'classification_level', |
||
210 | 'city', |
||
211 | 'title', |
||
212 | 'dept_impact', |
||
213 | 'team_impact', |
||
214 | 'hire_impact', |
||
215 | 'division', |
||
216 | 'education', |
||
217 | 'work_env_description', |
||
218 | 'culture_summary', |
||
219 | 'culture_special', |
||
220 | ]; |
||
221 | |||
222 | /** |
||
223 | * The attributes that should be visible in arrays. |
||
224 | * In this case, it blocks loaded relations from appearing. |
||
225 | * |
||
226 | * @var array |
||
227 | */ |
||
228 | protected $visible = [ |
||
229 | 'id', |
||
230 | 'manager_id', |
||
231 | 'chosen_lang', |
||
232 | 'term_qty', |
||
233 | 'open_date_time', |
||
234 | 'close_date_time', |
||
235 | 'start_date_time', |
||
236 | 'department_id', |
||
237 | 'province_id', |
||
238 | 'salary_min', |
||
239 | 'salary_max', |
||
240 | 'noc', |
||
241 | 'security_clearance_id', |
||
242 | 'language_requirement_id', |
||
243 | 'remote_work_allowed', |
||
244 | 'published_at', |
||
245 | 'published', |
||
246 | 'review_requested_at', |
||
247 | 'team_size', |
||
248 | 'work_env_features', |
||
249 | 'fast_vs_steady', |
||
250 | 'horizontal_vs_vertical', |
||
251 | 'experimental_vs_ongoing', |
||
252 | 'citizen_facing_vs_back_office', |
||
253 | 'collaborative_vs_independent', |
||
254 | 'telework_allowed_frequency_id', |
||
255 | 'flexible_hours_frequency_id', |
||
256 | 'travel_requirement_id', |
||
257 | 'overtime_requirement_id', |
||
258 | 'process_number', |
||
259 | 'priority_clearance_number', |
||
260 | 'loo_issuance_date', |
||
261 | 'classification_id', |
||
262 | 'classification_level', |
||
263 | 'job_status_id' |
||
264 | ]; |
||
265 | |||
266 | /** |
||
267 | * The accessors to append to the model's array form. |
||
268 | * |
||
269 | * @var string[] $appends |
||
270 | */ |
||
271 | protected $appends = [ |
||
272 | 'classification_code', |
||
273 | 'classification_message', |
||
274 | 'job_status_id', |
||
275 | ]; |
||
276 | |||
277 | /** |
||
278 | * Eager loaded relationships by default. |
||
279 | * |
||
280 | * @var string[] $with |
||
281 | */ |
||
282 | protected $with = [ |
||
283 | 'criteria', |
||
284 | 'manager' |
||
285 | ]; |
||
286 | |||
287 | /** |
||
288 | * @var mixed[] $dispatchesEvents |
||
289 | */ |
||
290 | protected $dispatchesEvents = [ |
||
291 | 'saved' => JobSaved::class, |
||
292 | ]; |
||
293 | |||
294 | // @codeCoverageIgnoreStart |
||
295 | public function department() // phpcs:ignore |
||
296 | { |
||
297 | return $this->belongsTo(\App\Models\Lookup\Department::class); |
||
298 | } |
||
299 | |||
300 | public function job_term() // phpcs:ignore |
||
301 | { |
||
302 | return $this->belongsTo(\App\Models\Lookup\JobTerm::class); |
||
303 | } |
||
304 | |||
305 | public function language_requirement() // phpcs:ignore |
||
306 | { |
||
307 | return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class); |
||
308 | } |
||
309 | |||
310 | public function manager() // phpcs:ignore |
||
311 | { |
||
312 | return $this->belongsTo(\App\Models\Manager::class); |
||
313 | } |
||
314 | |||
315 | public function province() // phpcs:ignore |
||
316 | { |
||
317 | return $this->belongsTo(\App\Models\Lookup\Province::class); |
||
318 | } |
||
319 | |||
320 | public function security_clearance() // phpcs:ignore |
||
321 | { |
||
322 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
323 | } |
||
324 | |||
325 | public function criteria() // phpcs:ignore |
||
326 | { |
||
327 | return $this->hasMany(\App\Models\Criteria::class); |
||
328 | } |
||
329 | |||
330 | public function hr_advisors() // phpcs:ignore |
||
331 | { |
||
332 | return $this->belongsToMany( |
||
333 | \App\Models\HrAdvisor::class, |
||
334 | 'claimed_jobs' |
||
335 | ); |
||
336 | } |
||
337 | |||
338 | public function job_applications() // phpcs:ignore |
||
339 | { |
||
340 | return $this->hasMany(\App\Models\JobApplication::class); |
||
341 | } |
||
342 | |||
343 | public function job_poster_key_tasks() // phpcs:ignore |
||
344 | { |
||
345 | return $this->hasMany(\App\Models\JobPosterKeyTask::class); |
||
346 | } |
||
347 | |||
348 | public function job_poster_questions() // phpcs:ignore |
||
349 | { |
||
350 | return $this->hasMany(\App\Models\JobPosterQuestion::class); |
||
351 | } |
||
352 | |||
353 | public function telework_allowed_frequency() // phpcs:ignore |
||
354 | { |
||
355 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
356 | } |
||
357 | |||
358 | public function flexible_hours_frequency() // phpcs:ignore |
||
359 | { |
||
360 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
361 | } |
||
362 | |||
363 | public function travel_requirement() // phpcs:ignore |
||
364 | { |
||
365 | return $this->belongsTo(\App\Models\Lookup\TravelRequirement::class); |
||
366 | } |
||
367 | |||
368 | public function overtime_requirement() // phpcs:ignore |
||
369 | { |
||
370 | return $this->belongsTo(\App\Models\Lookup\OvertimeRequirement::class); |
||
371 | } |
||
372 | |||
373 | public function classification() // phpcs:ignore |
||
374 | { |
||
375 | return $this->belongsTo(\App\Models\Classification::class); |
||
376 | } |
||
377 | |||
378 | public function comments() // phpcs:ignore |
||
379 | { |
||
380 | return $this->hasMany(\App\Models\Comment::class); |
||
381 | } |
||
382 | // @codeCoverageIgnoreEnd |
||
383 | /* Artificial Relations */ |
||
384 | |||
385 | /** |
||
386 | * Get all of the Job Applications submitted to this |
||
387 | * Job Poster. |
||
388 | * |
||
389 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
390 | */ |
||
391 | public function submitted_applications() // phpcs:ignore |
||
392 | { |
||
393 | return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query): void { |
||
394 | $query->where('name', 'draft'); |
||
395 | }); |
||
396 | } |
||
397 | |||
398 | /* Overrides */ |
||
399 | |||
400 | /** |
||
401 | * Retrieve the model for a bound value. |
||
402 | * Seems to be a useful workaround for providing submitted_applications_count |
||
403 | * to any bound routes that receive a jobPoster instance without using the |
||
404 | * withCount property on the model itself. |
||
405 | * See https://github.com/laravel/framework/issues/23957 for more info. |
||
406 | * |
||
407 | * @param mixed $value Value used to retrieve the model instance. |
||
408 | * |
||
409 | * @return \Illuminate\Database\Eloquent\Model|null |
||
410 | */ |
||
411 | public function resolveRouteBinding($value) // phpcs:ignore |
||
412 | { |
||
413 | return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404); |
||
|
|||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Intercept setting the "published" attribute, and set the |
||
418 | * "published_at" timestamp if true. |
||
419 | * |
||
420 | * @param mixed $value Incoming value for the 'published' attribute. |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | public function setPublishedAttribute($value): void |
||
425 | { |
||
426 | if ($value) { |
||
427 | $this->attributes['published_at'] = new Date(); |
||
428 | } else { |
||
429 | $this->attributes['published_at'] = null; |
||
430 | } |
||
431 | if ($value === null) { |
||
432 | $value = false; |
||
433 | } |
||
434 | $this->attributes['published'] = $value; |
||
435 | } |
||
436 | |||
437 | /* Methods */ |
||
438 | |||
439 | public function submitted_applications_count() //phpcs:ignore |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Formatted and localized date and time the Job Poster closes. |
||
446 | * |
||
447 | * @return string[] |
||
448 | */ |
||
449 | public function applyBy(): array |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Return whether the Job is Open or Closed. |
||
467 | * Used by the Admin Portal JobPosterCrudController. |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | public function displayStatus(): string |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Check if a Job Poster is open for applications. |
||
478 | * |
||
479 | * @return boolean |
||
480 | */ |
||
481 | public function isOpen(): bool |
||
482 | { |
||
483 | return $this->published |
||
484 | && $this->open_date_time !== null |
||
485 | && $this->close_date_time !== null |
||
486 | && $this->open_date_time->isPast() |
||
487 | && $this->close_date_time->isFuture(); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Check if a Job Poster is closed for applications. |
||
492 | * |
||
493 | * @return boolean |
||
494 | */ |
||
495 | public function isClosed(): bool |
||
496 | { |
||
497 | return $this->published |
||
498 | && $this->open_date_time !== null |
||
499 | && $this->close_date_time !== null |
||
500 | && $this->open_date_time->isPast() |
||
501 | && $this->close_date_time->isPast(); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Calculate the remaining time a Job Poster is open. |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | public function timeRemaining(): string |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Return the current status for the Job Poster. |
||
539 | * Possible values are "draft", "submitted", "published" and "closed". |
||
540 | * |
||
541 | * @return string |
||
542 | */ |
||
543 | public function status(): string |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * FIXME: |
||
561 | * Return a calculated job status id. |
||
562 | * For now these ids represent the following: |
||
563 | * 1 = Draft |
||
564 | * 2 = Review requested |
||
565 | * 3 = Approved |
||
566 | * 4 = Open |
||
567 | * 5 = Closed |
||
568 | * These statuses needs an immenent refactoring, so I'm not going to create |
||
569 | * a lookup table for them yet. |
||
570 | * TODO: When this is rebuilt, make sure to change matching JobStatus code in |
||
571 | * resources\assets\js\models\lookupConstants.ts |
||
572 | * |
||
573 | * @return integer |
||
574 | */ |
||
575 | public function getJobStatusIdAttribute() |
||
576 | { |
||
577 | $now = new Date(); |
||
578 | if ($this->review_requested_at === null) { |
||
579 | return 1; // Draft. |
||
580 | } elseif ($this->published_at === null) { |
||
581 | return 2; // Review requested, but not approved. |
||
582 | } elseif ($this->open_date_time === null || $this->open_date_time >$now) { |
||
583 | return 3; // Approved, but not open. |
||
584 | } elseif ($this->close_date_time === null || $this->close_date_time > $now) { |
||
585 | // Approved and currently open. |
||
586 | return 4; // Open. |
||
587 | } else { |
||
588 | // Published and close date has passed. |
||
589 | return 5; // Closed. |
||
590 | } |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Return true if this job should be visible to hr advisors. |
||
595 | * It should become visible after Manager has requested a review. |
||
596 | * |
||
597 | * @return boolean |
||
598 | */ |
||
599 | public function isVisibleToHr() |
||
600 | { |
||
601 | return $this->job_status_id !== 1; |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * The database model stores a foreign id to the classification table, |
||
606 | * but to simplify the API, this model simply returns the key as classification_code. |
||
607 | * |
||
608 | * @return string|null |
||
609 | */ |
||
610 | public function getClassificationCodeAttribute() |
||
611 | { |
||
612 | if ($this->classification_id !== null) { |
||
613 | return $this->classification->key; |
||
614 | } |
||
615 | return null; |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * |
||
620 | * Get the full government classification message. |
||
621 | * |
||
622 | * @return string|null |
||
623 | */ |
||
624 | public function getClassificationMessageAttribute() |
||
630 | } |
||
631 | } |
||
632 |