| Total Complexity | 52 |
| Total Lines | 494 |
| 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 |
||
| 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 | 'classification', |
||
| 192 | 'classification_code', |
||
| 193 | 'classification_level', |
||
| 194 | 'security_clearance_id', |
||
| 195 | 'language_requirement_id', |
||
| 196 | 'remote_work_allowed', |
||
| 197 | 'published', |
||
| 198 | 'team_size', |
||
| 199 | 'work_env_features', |
||
| 200 | 'fast_vs_steady', |
||
| 201 | 'horizontal_vs_vertical', |
||
| 202 | 'experimental_vs_ongoing', |
||
| 203 | 'citizen_facing_vs_back_office', |
||
| 204 | 'collaborative_vs_independent', |
||
| 205 | 'telework_allowed_frequency_id', |
||
| 206 | 'flexible_hours_frequency_id', |
||
| 207 | 'travel_requirement_id', |
||
| 208 | 'overtime_requirement_id', |
||
| 209 | 'process_number', |
||
| 210 | 'priority_clearance_number', |
||
| 211 | 'loo_issuance_date' |
||
| 212 | ]; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The attributes that should be visible in arrays. |
||
| 216 | * In this case, it blocks loaded relations from appearing. |
||
| 217 | * |
||
| 218 | * @var array |
||
| 219 | */ |
||
| 220 | protected $visible = [ |
||
| 221 | 'id', |
||
| 222 | 'manager_id', |
||
| 223 | 'chosen_lang', |
||
| 224 | 'term_qty', |
||
| 225 | 'open_date_time', |
||
| 226 | 'close_date_time', |
||
| 227 | 'start_date_time', |
||
| 228 | 'department_id', |
||
| 229 | 'province_id', |
||
| 230 | 'salary_min', |
||
| 231 | 'salary_max', |
||
| 232 | 'noc', |
||
| 233 | 'classification_code', |
||
| 234 | 'classification_level', |
||
| 235 | 'security_clearance_id', |
||
| 236 | 'language_requirement_id', |
||
| 237 | 'remote_work_allowed', |
||
| 238 | 'published_at', |
||
| 239 | 'published', |
||
| 240 | 'review_requested_at', |
||
| 241 | 'team_size', |
||
| 242 | 'work_env_features', |
||
| 243 | 'fast_vs_steady', |
||
| 244 | 'horizontal_vs_vertical', |
||
| 245 | 'experimental_vs_ongoing', |
||
| 246 | 'citizen_facing_vs_back_office', |
||
| 247 | 'collaborative_vs_independent', |
||
| 248 | 'telework_allowed_frequency_id', |
||
| 249 | 'flexible_hours_frequency_id', |
||
| 250 | 'travel_requirement_id', |
||
| 251 | 'overtime_requirement_id', |
||
| 252 | 'process_number', |
||
| 253 | 'priority_clearance_number', |
||
| 254 | 'loo_issuance_date' |
||
| 255 | ]; |
||
| 256 | |||
| 257 | /** |
||
| 258 | * The accessors to append to the model's array form. |
||
| 259 | * |
||
| 260 | * @var mixed[] $appends |
||
| 261 | */ |
||
| 262 | protected $appends = ['classification_code']; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @var mixed[] $dispatchesEvents |
||
| 266 | */ |
||
| 267 | protected $dispatchesEvents = [ |
||
| 268 | 'saved' => JobSaved::class, |
||
| 269 | ]; |
||
| 270 | |||
| 271 | // @codeCoverageIgnoreStart |
||
| 272 | public function department() // phpcs:ignore |
||
| 273 | { |
||
| 274 | return $this->belongsTo(\App\Models\Lookup\Department::class); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function job_term() // phpcs:ignore |
||
| 278 | { |
||
| 279 | return $this->belongsTo(\App\Models\Lookup\JobTerm::class); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function language_requirement() // phpcs:ignore |
||
| 283 | { |
||
| 284 | return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function manager() // phpcs:ignore |
||
| 288 | { |
||
| 289 | return $this->belongsTo(\App\Models\Manager::class); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function province() // phpcs:ignore |
||
| 293 | { |
||
| 294 | return $this->belongsTo(\App\Models\Lookup\Province::class); |
||
| 295 | } |
||
| 296 | |||
| 297 | public function security_clearance() // phpcs:ignore |
||
| 298 | { |
||
| 299 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function criteria() // phpcs:ignore |
||
| 303 | { |
||
| 304 | return $this->hasMany(\App\Models\Criteria::class); |
||
| 305 | } |
||
| 306 | |||
| 307 | public function hr_advisors() // phpcs:ignore |
||
| 308 | { |
||
| 309 | return $this->belongsToMany( |
||
| 310 | \App\Models\HrAdvisor::class, |
||
| 311 | 'claimed_jobs' |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | |||
| 315 | public function job_applications() // phpcs:ignore |
||
| 316 | { |
||
| 317 | return $this->hasMany(\App\Models\JobApplication::class); |
||
| 318 | } |
||
| 319 | |||
| 320 | public function job_poster_key_tasks() // phpcs:ignore |
||
| 321 | { |
||
| 322 | return $this->hasMany(\App\Models\JobPosterKeyTask::class); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function job_poster_questions() // phpcs:ignore |
||
| 326 | { |
||
| 327 | return $this->hasMany(\App\Models\JobPosterQuestion::class); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function job_poster_translations() // phpcs:ignore |
||
| 333 | } |
||
| 334 | |||
| 335 | public function telework_allowed_frequency() // phpcs:ignore |
||
| 336 | { |
||
| 337 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function flexible_hours_frequency() // phpcs:ignore |
||
| 341 | { |
||
| 342 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
| 343 | } |
||
| 344 | |||
| 345 | public function travel_requirement() // phpcs:ignore |
||
| 346 | { |
||
| 347 | return $this->belongsTo(\App\Models\Lookup\TravelRequirement::class); |
||
| 348 | } |
||
| 349 | |||
| 350 | public function overtime_requirement() // phpcs:ignore |
||
| 351 | { |
||
| 352 | return $this->belongsTo(\App\Models\Lookup\OvertimeRequirement::class); |
||
| 353 | } |
||
| 354 | |||
| 355 | // Artificial Relations |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get all of the Job Applications submitted to this |
||
| 359 | * Job Poster. |
||
| 360 | * |
||
| 361 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 362 | */ |
||
| 363 | public function submitted_applications() // phpcs:ignore |
||
| 364 | { |
||
| 365 | return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query) : void { |
||
| 366 | $query->where('name', 'draft'); |
||
| 367 | }); |
||
| 368 | } |
||
| 369 | |||
| 370 | // Overrides |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Retrieve the model for a bound value. |
||
| 374 | * Seems to be a useful workaround for providing submitted_applications_count |
||
| 375 | * to any bound routes that receive a jobPoster instance without using the |
||
| 376 | * withCount property on the model itself. |
||
| 377 | * See https://github.com/laravel/framework/issues/23957 for more info. |
||
| 378 | * |
||
| 379 | * @param mixed $value Value used to retrieve the model instance. |
||
| 380 | * |
||
| 381 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 382 | */ |
||
| 383 | public function resolveRouteBinding($value) // phpcs:ignore |
||
| 384 | { |
||
| 385 | return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404); |
||
| 386 | } |
||
| 387 | |||
| 388 | // @codeCoverageIgnoreEnd |
||
| 389 | // Accessors. |
||
| 390 | |||
| 391 | /** |
||
| 392 | * The database model stores a foreign id to the classification table, |
||
| 393 | * but to simplify the API, this model simply returns the key as classification_code. |
||
| 394 | * |
||
| 395 | * @return void |
||
|
1 ignored issue
–
show
|
|||
| 396 | */ |
||
| 397 | public function getClassificationCodeAttribute() |
||
| 398 | { |
||
| 399 | if ($this->classification_id !== null) { |
||
| 400 | $classification = Classification::find($this->classification_id); |
||
| 401 | return $classification->key; |
||
| 402 | } |
||
| 403 | return null; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * The classification property is deprecated. To ensure |
||
| 408 | * Twig template consistency, check for populated |
||
| 409 | * classification_code and classification_level and return |
||
| 410 | * the combination of those instead. |
||
| 411 | * |
||
| 412 | * @param mixed $value Incoming attribute value. |
||
| 413 | * |
||
| 414 | * @return string|null |
||
| 415 | */ |
||
| 416 | public function getClassificationAttribute($value) |
||
| 417 | { |
||
| 418 | if (!empty($this->classification_code) && !empty($this->classification_level)) { |
||
| 419 | return "$this->classification_code-$this->classification_level"; |
||
| 420 | } else { |
||
| 421 | return $value; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | // Mutators. |
||
| 426 | |||
| 427 | /** |
||
| 428 | * This model exposes the classification_code attribute, but it is determined |
||
| 429 | * by the classification_id column in the database, so set that value instead. |
||
| 430 | * |
||
| 431 | * @param string $value |
||
|
1 ignored issue
–
show
|
|||
| 432 | * @return void |
||
|
1 ignored issue
–
show
|
|||
| 433 | */ |
||
| 434 | public function setClassificationCodeAttribute($value): void |
||
|
1 ignored issue
–
show
|
|||
| 435 | { |
||
| 436 | $classification = Classification::where('key', $value)->first(); |
||
| 437 | if ($classification !== null) { |
||
| 438 | $this->attributes['classification_id'] = $classification->id; |
||
| 439 | } else { |
||
| 440 | $this->attributes['classification_id'] = null; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Intercept setting the "published" attribute, and set the |
||
| 446 | * "published_at" timestamp if true. |
||
| 447 | * |
||
| 448 | * @param mixed $value Incoming value for the 'published' attribute. |
||
| 449 | * |
||
| 450 | * @return void |
||
|
1 ignored issue
–
show
|
|||
| 451 | */ |
||
| 452 | public function setPublishedAttribute($value) : void |
||
| 463 | } |
||
| 464 | |||
| 465 | // Methods |
||
| 466 | public function submitted_applications_count() //phpcs:ignore |
||
| 467 | { |
||
| 468 | return $this->submitted_applications()->count(); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Formatted and localized date and time the Job Poster closes. |
||
| 473 | * |
||
| 474 | * @return string[] |
||
| 475 | */ |
||
| 476 | public function applyBy() : array |
||
| 477 | { |
||
| 478 | $localCloseDate = new Date($this->close_date_time); // This initializes the date object in UTC time |
||
| 479 | $localCloseDate->setTimezone(new \DateTimeZone(self::TIMEZONE)); // Then set the time zone for display |
||
| 480 | $displayDate = [ |
||
| 481 | 'date' => $localCloseDate->format(self::DATE_FORMAT[App::getLocale()]), |
||
| 482 | 'time' => $localCloseDate->format(self::TIME_FORMAT[App::getLocale()]) |
||
| 483 | ]; |
||
| 484 | |||
| 485 | if (App::isLocale('fr')) { |
||
| 486 | $displayDate['time'] = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayDate['time']); |
||
| 487 | } |
||
| 488 | |||
| 489 | return $displayDate; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Return whether the Job is Open or Closed. |
||
| 494 | * Used by the Admin Portal JobPosterCrudController. |
||
| 495 | * |
||
| 496 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 497 | */ |
||
| 498 | public function displayStatus() : string |
||
| 499 | { |
||
| 500 | return $this->isOpen() ? 'Open' : 'Closed'; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Check if a Job Poster is open for applications. |
||
| 505 | * |
||
| 506 | * @return boolean |
||
|
1 ignored issue
–
show
|
|||
| 507 | */ |
||
| 508 | public function isOpen() : bool |
||
| 509 | { |
||
| 510 | return $this->published |
||
| 511 | && $this->open_date_time !== null |
||
| 512 | && $this->close_date_time !== null |
||
| 513 | && $this->open_date_time->isPast() |
||
| 514 | && $this->close_date_time->isFuture(); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Check if a Job Poster is closed for applications. |
||
| 519 | * |
||
| 520 | * @return boolean |
||
|
1 ignored issue
–
show
|
|||
| 521 | */ |
||
| 522 | public function isClosed() : bool |
||
| 523 | { |
||
| 524 | return $this->published |
||
| 525 | && $this->open_date_time !== null |
||
| 526 | && $this->close_date_time !== null |
||
| 527 | && $this->open_date_time->isPast() |
||
| 528 | && $this->close_date_time->isPast(); |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Calculate the remaining time a Job Poster is open. |
||
| 533 | * |
||
| 534 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 535 | */ |
||
| 536 | public function timeRemaining() : string |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Return the current status for the Job Poster. |
||
| 566 | * Possible values are "draft", "submitted", "published" and "closed". |
||
| 567 | * |
||
| 568 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 569 | */ |
||
| 570 | public function status() : string |
||
| 571 | { |
||
| 572 | $status = 'draft'; |
||
| 573 | if ($this->isOpen()) { |
||
| 574 | $status = 'published'; |
||
| 575 | } elseif ($this->isClosed()) { |
||
| 576 | $status = 'closed'; |
||
| 577 | } elseif ($this->review_requested_at !== null) { |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Return the array of values used to represent this object in an api response. |
||
| 588 | * This array should contain no nested objects (besides translations). |
||
| 589 | * |
||
| 590 | * @return mixed[] |
||
| 591 | */ |
||
| 592 | public function toApiArray(): array |
||
| 596 | } |
||
| 597 | } |
||
| 598 |