| Total Complexity | 40 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Coverage | 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 |
||
| 88 | class JobPoster extends BaseModel |
||
| 89 | { |
||
| 90 | use CrudTrait; |
||
|
|
|||
| 91 | use \Dimsav\Translatable\Translatable; |
||
| 92 | use Notifiable; |
||
| 93 | |||
| 94 | const DATE_FORMAT = [ |
||
| 95 | 'en' => 'M jS, Y', |
||
| 96 | 'fr' => 'd M Y', |
||
| 97 | ]; |
||
| 98 | const TIME_FORMAT = [ |
||
| 99 | 'en' => 'h:i A T', |
||
| 100 | 'fr' => 'H \h i T', |
||
| 101 | ]; |
||
| 102 | const TIMEZONE = 'America/Toronto'; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string[] $translatedAttributes |
||
| 106 | */ |
||
| 107 | public $translatedAttributes = [ |
||
| 108 | 'city', |
||
| 109 | 'title', |
||
| 110 | 'impact', |
||
| 111 | 'team_impact', |
||
| 112 | 'hire_impact', |
||
| 113 | 'branch', |
||
| 114 | 'division', |
||
| 115 | 'education', |
||
| 116 | 'work_env_description', |
||
| 117 | 'culture_summary', |
||
| 118 | 'culture_special', |
||
| 119 | ]; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string[] $casts |
||
| 123 | */ |
||
| 124 | protected $casts = [ |
||
| 125 | 'job_term_id' => 'int', |
||
| 126 | 'department_id' => 'int', |
||
| 127 | 'province_id' => 'int', |
||
| 128 | 'salary_min' => 'int', |
||
| 129 | 'salary_max' => 'int', |
||
| 130 | 'noc' => 'int', |
||
| 131 | 'classification_code' => 'string', |
||
| 132 | 'classification_level' => 'int', |
||
| 133 | 'security_clearance_id' => 'int', |
||
| 134 | 'language_requirement_id' => 'int', |
||
| 135 | 'remote_work_allowed' => 'boolean', |
||
| 136 | 'manager_id' => 'int', |
||
| 137 | 'published' => 'boolean', |
||
| 138 | 'team_size' => 'int', |
||
| 139 | 'work_env_features' => 'array', |
||
| 140 | 'fast_vs_steady' => 'int', |
||
| 141 | 'horizontal_vs_vertical' => 'int', |
||
| 142 | 'experimental_vs_ongoing' => 'int', |
||
| 143 | 'citizen_facing_vs_back_office' => 'int', |
||
| 144 | 'collaborative_vs_independent' => 'int', |
||
| 145 | 'telework_allowed_frequency_id' => 'int', |
||
| 146 | 'flexible_hours_frequency_id' => 'int', |
||
| 147 | ]; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var string[] $dates |
||
| 151 | */ |
||
| 152 | protected $dates = [ |
||
| 153 | 'open_date_time', |
||
| 154 | 'close_date_time', |
||
| 155 | 'start_date_time', |
||
| 156 | 'review_requested_at', |
||
| 157 | 'published_at' |
||
| 158 | ]; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var string[] $fillable |
||
| 162 | */ |
||
| 163 | protected $fillable = [ |
||
| 164 | 'job_term_id', |
||
| 165 | 'term_qty', |
||
| 166 | 'open_date_time', |
||
| 167 | 'close_date_time', |
||
| 168 | 'start_date_time', |
||
| 169 | 'department_id', |
||
| 170 | 'province_id', |
||
| 171 | 'salary_min', |
||
| 172 | 'salary_max', |
||
| 173 | 'noc', |
||
| 174 | 'classification', |
||
| 175 | 'classification_code', |
||
| 176 | 'classification_level', |
||
| 177 | 'security_clearance_id', |
||
| 178 | 'language_requirement_id', |
||
| 179 | 'remote_work_allowed', |
||
| 180 | 'published', |
||
| 181 | 'team_size', |
||
| 182 | 'work_env_features', |
||
| 183 | 'fast_vs_steady', |
||
| 184 | 'horizontal_vs_vertical', |
||
| 185 | 'experimental_vs_ongoing', |
||
| 186 | 'citizen_facing_vs_back_office', |
||
| 187 | 'collaborative_vs_independent', |
||
| 188 | 'telework_allowed_frequency_id', |
||
| 189 | 'flexible_hours_frequency_id', |
||
| 190 | ]; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @var mixed[] $dispatchesEvents |
||
| 194 | */ |
||
| 195 | protected $dispatchesEvents = [ |
||
| 196 | 'saved' => JobSaved::class, |
||
| 197 | ]; |
||
| 198 | |||
| 199 | // @codeCoverageIgnoreStart |
||
| 200 | public function department() // phpcs:ignore |
||
| 201 | { |
||
| 202 | return $this->belongsTo(\App\Models\Lookup\Department::class); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function job_term() // phpcs:ignore |
||
| 206 | { |
||
| 207 | return $this->belongsTo(\App\Models\Lookup\JobTerm::class); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function language_requirement() // phpcs:ignore |
||
| 211 | { |
||
| 212 | return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class); |
||
| 213 | } |
||
| 214 | |||
| 215 | public function manager() // phpcs:ignore |
||
| 216 | { |
||
| 217 | return $this->belongsTo(\App\Models\Manager::class); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function province() // phpcs:ignore |
||
| 221 | { |
||
| 222 | return $this->belongsTo(\App\Models\Lookup\Province::class); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function security_clearance() // phpcs:ignore |
||
| 226 | { |
||
| 227 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function criteria() // phpcs:ignore |
||
| 231 | { |
||
| 232 | return $this->hasMany(\App\Models\Criteria::class); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function job_applications() // phpcs:ignore |
||
| 236 | { |
||
| 237 | return $this->hasMany(\App\Models\JobApplication::class); |
||
| 238 | } |
||
| 239 | |||
| 240 | public function job_poster_key_tasks() // phpcs:ignore |
||
| 241 | { |
||
| 242 | return $this->hasMany(\App\Models\JobPosterKeyTask::class); |
||
| 243 | } |
||
| 244 | |||
| 245 | public function job_poster_questions() // phpcs:ignore |
||
| 246 | { |
||
| 247 | return $this->hasMany(\App\Models\JobPosterQuestion::class); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function job_poster_translations() // phpcs:ignore |
||
| 253 | } |
||
| 254 | |||
| 255 | public function telework_allowed_frequency() // phpcs:ignore |
||
| 256 | { |
||
| 257 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function flexible_hours_frequency() // phpcs:ignore |
||
| 261 | { |
||
| 262 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
| 263 | } |
||
| 264 | |||
| 265 | // Artificial Relations |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get all of the Job Applications submitted to this |
||
| 269 | * Job Poster. |
||
| 270 | * |
||
| 271 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 272 | */ |
||
| 273 | public function submitted_applications() // phpcs:ignore |
||
| 274 | { |
||
| 275 | return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query) : void { |
||
| 276 | $query->where('name', 'draft'); |
||
| 277 | }); |
||
| 278 | } |
||
| 279 | |||
| 280 | // Overrides |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Retrieve the model for a bound value. |
||
| 284 | * Seems to be a useful workaround for providing submitted_applications_count |
||
| 285 | * to any bound routes that receive a jobPoster instance without using the |
||
| 286 | * withCount property on the model itself. |
||
| 287 | * See https://github.com/laravel/framework/issues/23957 for more info. |
||
| 288 | * |
||
| 289 | * @param mixed $value Value used to retrieve the model instance. |
||
| 290 | * |
||
| 291 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 292 | */ |
||
| 293 | public function resolveRouteBinding($value) // phpcs:ignore |
||
| 294 | { |
||
| 295 | return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404); |
||
| 296 | } |
||
| 297 | |||
| 298 | // @codeCoverageIgnoreEnd |
||
| 299 | // Accessors |
||
| 300 | // Mutators |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Intercept setting the "published" attribute, and set the |
||
| 304 | * "published_at" timestamp if true. |
||
| 305 | * |
||
| 306 | * @param mixed $value Incoming value for the 'published' attribute. |
||
| 307 | * |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | public function setPublishedAttribute($value) : void |
||
| 318 | } |
||
| 319 | |||
| 320 | // Methods |
||
| 321 | public function submitted_applications_count() |
||
|
1 ignored issue
–
show
|
|||
| 322 | { |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Formatted and localized date and time the Job Poster closes. |
||
| 328 | * |
||
| 329 | * @return string[] |
||
| 330 | */ |
||
| 331 | public function applyBy() : array |
||
| 332 | { |
||
| 333 | $localCloseDate = new Date($this->close_date_time); // This initializes the date object in UTC time |
||
| 334 | $localCloseDate->setTimezone(new \DateTimeZone(self::TIMEZONE)); // Then set the time zone for display |
||
| 335 | $displayDate = [ |
||
| 336 | 'date' => $localCloseDate->format(self::DATE_FORMAT[App::getLocale()]), |
||
| 337 | 'time' => $localCloseDate->format(self::TIME_FORMAT[App::getLocale()]) |
||
| 338 | ]; |
||
| 339 | |||
| 340 | if (App::isLocale('fr')) { |
||
| 341 | $displayDate['time'] = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayDate['time']); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $displayDate; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Return whether the Job is Open or Closed. |
||
| 349 | * Used by the Admin Portal JobPosterCrudController. |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function displayStatus() : string |
||
| 354 | { |
||
| 355 | return $this->isOpen() ? 'Open' : 'Closed'; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check if a Job Poster is open for applications. |
||
| 360 | * |
||
| 361 | * @return boolean |
||
| 362 | */ |
||
| 363 | public function isOpen() : bool |
||
| 364 | { |
||
| 365 | return $this->published |
||
| 366 | && $this->open_date_time->isPast() |
||
| 367 | && $this->close_date_time->isFuture(); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Check if a Job Poster is closed for applications. |
||
| 372 | * |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | public function isClosed() : bool |
||
| 376 | { |
||
| 377 | return $this->published |
||
| 378 | && $this->open_date_time->isPast() |
||
| 379 | && $this->close_date_time->isPast(); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Calculate the remaining time a Job Poster is open. |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function timeRemaining() : string |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Return the current status for the Job Poster. |
||
| 417 | * Possible values are "draft", "submitted", "published" and "closed". |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function status() : string |
||
| 422 | { |
||
| 423 | $status = 'draft'; |
||
| 424 | if ($this->isOpen()) { |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Return the array of values used to represent this object in an api response. |
||
| 439 | * This array should contain no nested objects (besides translations). |
||
| 440 | * |
||
| 441 | * @return mixed[] |
||
| 442 | */ |
||
| 443 | public function toApiArray(): array |
||
| 469 | } |
||
| 470 | } |
||
| 471 |