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