| Total Complexity | 52 |
| Total Lines | 479 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 102 | class JobPoster extends BaseModel |
||
| 103 | { |
||
| 104 | use CrudTrait; |
||
|
|
|||
| 105 | use Translatable; |
||
| 106 | use Notifiable; |
||
| 107 | |||
| 108 | const DATE_FORMAT = [ |
||
| 109 | 'en' => 'M jS, Y', |
||
| 110 | 'fr' => 'd M Y', |
||
| 111 | ]; |
||
| 112 | const TIME_FORMAT = [ |
||
| 113 | 'en' => 'h:i A T', |
||
| 114 | 'fr' => 'H \h i T', |
||
| 115 | ]; |
||
| 116 | const TIMEZONE = 'America/Toronto'; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string[] $translatedAttributes |
||
| 120 | */ |
||
| 121 | public $translatedAttributes = [ |
||
| 122 | 'city', |
||
| 123 | 'title', |
||
| 124 | 'dept_impact', |
||
| 125 | 'team_impact', |
||
| 126 | 'hire_impact', |
||
| 127 | 'division', |
||
| 128 | 'education', |
||
| 129 | 'work_env_description', |
||
| 130 | 'culture_summary', |
||
| 131 | 'culture_special', |
||
| 132 | ]; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string[] $casts |
||
| 136 | */ |
||
| 137 | protected $casts = [ |
||
| 138 | 'job_term_id' => 'int', |
||
| 139 | 'department_id' => 'int', |
||
| 140 | 'province_id' => 'int', |
||
| 141 | 'salary_min' => 'int', |
||
| 142 | 'salary_max' => 'int', |
||
| 143 | 'noc' => 'int', |
||
| 144 | 'classification_id' => 'int', |
||
| 145 | 'classification_level' => 'int', |
||
| 146 | 'security_clearance_id' => 'int', |
||
| 147 | 'language_requirement_id' => 'int', |
||
| 148 | 'remote_work_allowed' => 'boolean', |
||
| 149 | 'manager_id' => 'int', |
||
| 150 | 'published' => 'boolean', |
||
| 151 | 'team_size' => 'int', |
||
| 152 | 'work_env_features' => 'array', |
||
| 153 | 'fast_vs_steady' => 'int', |
||
| 154 | 'horizontal_vs_vertical' => 'int', |
||
| 155 | 'experimental_vs_ongoing' => 'int', |
||
| 156 | 'citizen_facing_vs_back_office' => 'int', |
||
| 157 | 'collaborative_vs_independent' => 'int', |
||
| 158 | 'telework_allowed_frequency_id' => 'int', |
||
| 159 | 'flexible_hours_frequency_id' => 'int', |
||
| 160 | 'travel_requirement_id' => 'int', |
||
| 161 | 'overtime_requirement_id' => 'int', |
||
| 162 | ]; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var string[] $dates |
||
| 166 | */ |
||
| 167 | protected $dates = [ |
||
| 168 | 'open_date_time', |
||
| 169 | 'close_date_time', |
||
| 170 | 'start_date_time', |
||
| 171 | 'review_requested_at', |
||
| 172 | 'published_at', |
||
| 173 | 'loo_issuance_date', |
||
| 174 | ]; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var string[] $fillable |
||
| 178 | */ |
||
| 179 | protected $fillable = [ |
||
| 180 | 'job_term_id', |
||
| 181 | 'chosen_lang', |
||
| 182 | 'term_qty', |
||
| 183 | 'open_date_time', |
||
| 184 | 'close_date_time', |
||
| 185 | 'start_date_time', |
||
| 186 | 'department_id', |
||
| 187 | 'province_id', |
||
| 188 | 'salary_min', |
||
| 189 | 'salary_max', |
||
| 190 | 'noc', |
||
| 191 | 'security_clearance_id', |
||
| 192 | 'language_requirement_id', |
||
| 193 | 'remote_work_allowed', |
||
| 194 | 'published', |
||
| 195 | 'team_size', |
||
| 196 | 'work_env_features', |
||
| 197 | 'fast_vs_steady', |
||
| 198 | 'horizontal_vs_vertical', |
||
| 199 | 'experimental_vs_ongoing', |
||
| 200 | 'citizen_facing_vs_back_office', |
||
| 201 | 'collaborative_vs_independent', |
||
| 202 | 'telework_allowed_frequency_id', |
||
| 203 | 'flexible_hours_frequency_id', |
||
| 204 | 'travel_requirement_id', |
||
| 205 | 'overtime_requirement_id', |
||
| 206 | 'process_number', |
||
| 207 | 'priority_clearance_number', |
||
| 208 | 'loo_issuance_date', |
||
| 209 | 'classification_id', |
||
| 210 | 'classification_level', |
||
| 211 | ]; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * The attributes that should be visible in arrays. |
||
| 215 | * In this case, it blocks loaded relations from appearing. |
||
| 216 | * |
||
| 217 | * @var array |
||
| 218 | */ |
||
| 219 | protected $visible = [ |
||
| 220 | 'id', |
||
| 221 | 'manager_id', |
||
| 222 | 'chosen_lang', |
||
| 223 | 'term_qty', |
||
| 224 | 'open_date_time', |
||
| 225 | 'close_date_time', |
||
| 226 | 'start_date_time', |
||
| 227 | 'department_id', |
||
| 228 | 'province_id', |
||
| 229 | 'salary_min', |
||
| 230 | 'salary_max', |
||
| 231 | 'noc', |
||
| 232 | 'security_clearance_id', |
||
| 233 | 'language_requirement_id', |
||
| 234 | 'remote_work_allowed', |
||
| 235 | 'published_at', |
||
| 236 | 'published', |
||
| 237 | 'review_requested_at', |
||
| 238 | 'team_size', |
||
| 239 | 'work_env_features', |
||
| 240 | 'fast_vs_steady', |
||
| 241 | 'horizontal_vs_vertical', |
||
| 242 | 'experimental_vs_ongoing', |
||
| 243 | 'citizen_facing_vs_back_office', |
||
| 244 | 'collaborative_vs_independent', |
||
| 245 | 'telework_allowed_frequency_id', |
||
| 246 | 'flexible_hours_frequency_id', |
||
| 247 | 'travel_requirement_id', |
||
| 248 | 'overtime_requirement_id', |
||
| 249 | 'process_number', |
||
| 250 | 'priority_clearance_number', |
||
| 251 | 'loo_issuance_date', |
||
| 252 | 'classification_id', |
||
| 253 | 'classification_level' |
||
| 254 | ]; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * The accessors to append to the model's array form. |
||
| 258 | * |
||
| 259 | * @var string[] $appends |
||
| 260 | */ |
||
| 261 | protected $appends = [ |
||
| 262 | 'classification_code', |
||
| 263 | 'classification_message' |
||
| 264 | ]; |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @var mixed[] $dispatchesEvents |
||
| 268 | */ |
||
| 269 | protected $dispatchesEvents = [ |
||
| 270 | 'saved' => JobSaved::class, |
||
| 271 | ]; |
||
| 272 | |||
| 273 | // @codeCoverageIgnoreStart |
||
| 274 | public function department() // phpcs:ignore |
||
| 275 | { |
||
| 276 | return $this->belongsTo(\App\Models\Lookup\Department::class); |
||
| 277 | } |
||
| 278 | |||
| 279 | public function job_term() // phpcs:ignore |
||
| 280 | { |
||
| 281 | return $this->belongsTo(\App\Models\Lookup\JobTerm::class); |
||
| 282 | } |
||
| 283 | |||
| 284 | public function language_requirement() // phpcs:ignore |
||
| 285 | { |
||
| 286 | return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function manager() // phpcs:ignore |
||
| 290 | { |
||
| 291 | return $this->belongsTo(\App\Models\Manager::class); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function province() // phpcs:ignore |
||
| 295 | { |
||
| 296 | return $this->belongsTo(\App\Models\Lookup\Province::class); |
||
| 297 | } |
||
| 298 | |||
| 299 | public function security_clearance() // phpcs:ignore |
||
| 300 | { |
||
| 301 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
| 302 | } |
||
| 303 | |||
| 304 | public function criteria() // phpcs:ignore |
||
| 305 | { |
||
| 306 | return $this->hasMany(\App\Models\Criteria::class); |
||
| 307 | } |
||
| 308 | |||
| 309 | public function hr_advisors() // phpcs:ignore |
||
| 310 | { |
||
| 311 | return $this->belongsToMany( |
||
| 312 | \App\Models\HrAdvisor::class, |
||
| 313 | 'claimed_jobs' |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | public function job_applications() // phpcs:ignore |
||
| 318 | { |
||
| 319 | return $this->hasMany(\App\Models\JobApplication::class); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function job_poster_key_tasks() // phpcs:ignore |
||
| 323 | { |
||
| 324 | return $this->hasMany(\App\Models\JobPosterKeyTask::class); |
||
| 325 | } |
||
| 326 | |||
| 327 | public function job_poster_questions() // phpcs:ignore |
||
| 328 | { |
||
| 329 | return $this->hasMany(\App\Models\JobPosterQuestion::class); |
||
| 330 | } |
||
| 331 | |||
| 332 | public function job_poster_translations() // phpcs:ignore |
||
| 335 | } |
||
| 336 | |||
| 337 | public function telework_allowed_frequency() // phpcs:ignore |
||
| 340 | } |
||
| 341 | |||
| 342 | public function flexible_hours_frequency() // phpcs:ignore |
||
| 343 | { |
||
| 344 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function travel_requirement() // phpcs:ignore |
||
| 348 | { |
||
| 349 | return $this->belongsTo(\App\Models\Lookup\TravelRequirement::class); |
||
| 350 | } |
||
| 351 | |||
| 352 | public function overtime_requirement() // phpcs:ignore |
||
| 355 | } |
||
| 356 | |||
| 357 | public function classification() // phpcs:ignore |
||
| 358 | { |
||
| 359 | return $this->belongsTo(\App\Models\Classification::class); |
||
| 360 | } |
||
| 361 | |||
| 362 | public function comments() // phpcs:ignore |
||
| 363 | { |
||
| 364 | return $this->hasMany(\App\Models\Comment::class); |
||
| 365 | } |
||
| 366 | // @codeCoverageIgnoreEnd |
||
| 367 | /* Artificial Relations */ |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get all of the Job Applications submitted to this |
||
| 371 | * Job Poster. |
||
| 372 | * |
||
| 373 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 374 | */ |
||
| 375 | public function submitted_applications() // phpcs:ignore |
||
| 376 | { |
||
| 377 | return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query): void { |
||
| 378 | $query->where('name', 'draft'); |
||
| 379 | }); |
||
| 380 | } |
||
| 381 | |||
| 382 | /* Overrides */ |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Retrieve the model for a bound value. |
||
| 386 | * Seems to be a useful workaround for providing submitted_applications_count |
||
| 387 | * to any bound routes that receive a jobPoster instance without using the |
||
| 388 | * withCount property on the model itself. |
||
| 389 | * See https://github.com/laravel/framework/issues/23957 for more info. |
||
| 390 | * |
||
| 391 | * @param mixed $value Value used to retrieve the model instance. |
||
| 392 | * |
||
| 393 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 394 | */ |
||
| 395 | public function resolveRouteBinding($value) // phpcs:ignore |
||
| 396 | { |
||
| 397 | return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Intercept setting the "published" attribute, and set the |
||
| 402 | * "published_at" timestamp if true. |
||
| 403 | * |
||
| 404 | * @param mixed $value Incoming value for the 'published' attribute. |
||
| 405 | * |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | public function setPublishedAttribute($value): void |
||
| 409 | { |
||
| 410 | if ($value) { |
||
| 411 | $this->attributes['published_at'] = new Date(); |
||
| 412 | } else { |
||
| 413 | $this->attributes['published_at'] = null; |
||
| 414 | } |
||
| 415 | if ($value === null) { |
||
| 416 | $value = false; |
||
| 417 | } |
||
| 418 | $this->attributes['published'] = $value; |
||
| 419 | } |
||
| 420 | |||
| 421 | /* Methods */ |
||
| 422 | |||
| 423 | public function submitted_applications_count() //phpcs:ignore |
||
| 424 | { |
||
| 425 | return $this->submitted_applications()->count(); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Formatted and localized date and time the Job Poster closes. |
||
| 430 | * |
||
| 431 | * @return string[] |
||
| 432 | */ |
||
| 433 | public function applyBy(): array |
||
| 434 | { |
||
| 435 | $localCloseDate = new Date($this->close_date_time); // This initializes the date object in UTC time. |
||
| 436 | $localCloseDate->setTimezone(new \DateTimeZone(self::TIMEZONE)); // Then set the time zone for display. |
||
| 437 | $displayDate = [ |
||
| 438 | 'date' => $localCloseDate->format(self::DATE_FORMAT[App::getLocale()]), |
||
| 439 | 'time' => $localCloseDate->format(self::TIME_FORMAT[App::getLocale()]) |
||
| 440 | ]; |
||
| 441 | |||
| 442 | if (App::isLocale('fr')) { |
||
| 443 | $displayDate['time'] = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayDate['time']); |
||
| 444 | } |
||
| 445 | |||
| 446 | return $displayDate; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Return whether the Job is Open or Closed. |
||
| 451 | * Used by the Admin Portal JobPosterCrudController. |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function displayStatus(): string |
||
| 456 | { |
||
| 457 | return $this->isOpen() ? 'Open' : 'Closed'; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Check if a Job Poster is open for applications. |
||
| 462 | * |
||
| 463 | * @return boolean |
||
| 464 | */ |
||
| 465 | public function isOpen(): bool |
||
| 466 | { |
||
| 467 | return $this->published |
||
| 468 | && $this->open_date_time !== null |
||
| 469 | && $this->close_date_time !== null |
||
| 470 | && $this->open_date_time->isPast() |
||
| 471 | && $this->close_date_time->isFuture(); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Check if a Job Poster is closed for applications. |
||
| 476 | * |
||
| 477 | * @return boolean |
||
| 478 | */ |
||
| 479 | public function isClosed(): bool |
||
| 480 | { |
||
| 481 | return $this->published |
||
| 482 | && $this->open_date_time !== null |
||
| 483 | && $this->close_date_time !== null |
||
| 484 | && $this->open_date_time->isPast() |
||
| 485 | && $this->close_date_time->isPast(); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Calculate the remaining time a Job Poster is open. |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function timeRemaining(): string |
||
| 494 | { |
||
| 495 | $interval = $this->close_date_time->diff(Date::now()); |
||
| 496 | |||
| 497 | $d = $interval->d; |
||
| 498 | $h = $interval->h; |
||
| 499 | $m = $interval->i; |
||
| 500 | $s = $interval->s; |
||
| 501 | |||
| 502 | if ($d > 0) { |
||
| 503 | $unit = 'day'; |
||
| 504 | $count = $d; |
||
| 505 | } elseif ($h > 0) { |
||
| 506 | $unit = 'hour'; |
||
| 507 | $count = $h; |
||
| 508 | } elseif ($m > 0) { |
||
| 509 | $unit = 'minute'; |
||
| 510 | $count = $m; |
||
| 511 | } else { |
||
| 512 | $unit = 'second'; |
||
| 513 | $count = $s; |
||
| 514 | } |
||
| 515 | |||
| 516 | $key = "common/time.$unit"; |
||
| 517 | |||
| 518 | return Lang::choice($key, $count); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Return the current status for the Job Poster. |
||
| 523 | * Possible values are "draft", "submitted", "published" and "closed". |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function status(): string |
||
| 528 | { |
||
| 529 | $status = 'draft'; |
||
| 530 | if ($this->isOpen()) { |
||
| 531 | $status = 'published'; |
||
| 532 | } elseif ($this->isClosed()) { |
||
| 533 | $status = 'closed'; |
||
| 534 | } elseif ($this->review_requested_at !== null) { |
||
| 535 | $status = 'submitted'; |
||
| 536 | } else { |
||
| 537 | $status = 'draft'; |
||
| 538 | } |
||
| 539 | |||
| 540 | return $status; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * The database model stores a foreign id to the classification table, |
||
| 545 | * but to simplify the API, this model simply returns the key as classification_code. |
||
| 546 | * |
||
| 547 | * @return string|null |
||
| 548 | */ |
||
| 549 | public function getClassificationCodeAttribute() |
||
| 550 | { |
||
| 551 | if ($this->classification_id !== null) { |
||
| 552 | return $this->classification->key; |
||
| 553 | } |
||
| 554 | return null; |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * |
||
| 559 | * Get the full government classification message. |
||
| 560 | * |
||
| 561 | * @return string|null |
||
| 562 | */ |
||
| 563 | public function getClassificationMessageAttribute() |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Return the array of values used to represent this object in an api response. |
||
| 573 | * This array should contain no nested objects (besides translations). |
||
| 574 | * |
||
| 575 | * @return mixed[] |
||
| 576 | */ |
||
| 577 | public function toApiArray(): array |
||
| 581 | } |
||
| 582 | } |
||
| 583 |