Total Complexity | 43 |
Total Lines | 488 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
103 | class JobPoster extends BaseModel |
||
104 | { |
||
105 | use CrudTrait; |
||
106 | use HasTranslations; |
||
107 | use Notifiable; |
||
108 | |||
109 | const DATE_FORMAT = [ |
||
110 | 'en' => 'M jS, Y', |
||
111 | 'fr' => 'd M Y', |
||
112 | ]; |
||
113 | const TIME_FORMAT = [ |
||
114 | 'en' => 'h:i A T', |
||
115 | 'fr' => 'H \h i T', |
||
116 | ]; |
||
117 | const TIMEZONE = 'America/Toronto'; |
||
118 | |||
119 | /** |
||
120 | * @var string[] $translatable |
||
121 | */ |
||
122 | public $translatable = [ |
||
123 | 'city', |
||
124 | 'title', |
||
125 | 'dept_impact', |
||
126 | 'team_impact', |
||
127 | 'hire_impact', |
||
128 | 'division', |
||
129 | 'education', |
||
130 | 'work_env_description', |
||
131 | 'culture_summary', |
||
132 | 'culture_special', |
||
133 | ]; |
||
134 | |||
135 | /** |
||
136 | * @var string[] $casts |
||
137 | */ |
||
138 | protected $casts = [ |
||
139 | 'job_term_id' => 'int', |
||
140 | 'department_id' => 'int', |
||
141 | 'province_id' => 'int', |
||
142 | 'salary_min' => 'int', |
||
143 | 'salary_max' => 'int', |
||
144 | 'noc' => 'int', |
||
145 | 'classification_id' => 'int', |
||
146 | 'classification_level' => 'int', |
||
147 | 'security_clearance_id' => 'int', |
||
148 | 'language_requirement_id' => 'int', |
||
149 | 'remote_work_allowed' => 'boolean', |
||
150 | 'manager_id' => 'int', |
||
151 | 'internal_only' => 'boolean', |
||
152 | 'team_size' => 'int', |
||
153 | 'work_env_features' => 'array', |
||
154 | 'fast_vs_steady' => 'int', |
||
155 | 'horizontal_vs_vertical' => 'int', |
||
156 | 'experimental_vs_ongoing' => 'int', |
||
157 | 'citizen_facing_vs_back_office' => 'int', |
||
158 | 'collaborative_vs_independent' => 'int', |
||
159 | 'telework_allowed_frequency_id' => 'int', |
||
160 | 'flexible_hours_frequency_id' => 'int', |
||
161 | 'travel_requirement_id' => 'int', |
||
162 | 'overtime_requirement_id' => 'int', |
||
163 | 'process_number' => 'string', |
||
164 | 'priority_clearance_number' => 'int' |
||
165 | ]; |
||
166 | |||
167 | /** |
||
168 | * @var string[] $dates |
||
169 | */ |
||
170 | protected $dates = [ |
||
171 | 'open_date_time', |
||
172 | 'close_date_time', |
||
173 | 'start_date_time', |
||
174 | 'loo_issuance_date', |
||
175 | ]; |
||
176 | |||
177 | /** |
||
178 | * @var string[] $fillable |
||
179 | */ |
||
180 | protected $fillable = [ |
||
181 | 'job_term_id', |
||
182 | 'chosen_lang', |
||
183 | 'term_qty', |
||
184 | 'open_date_time', |
||
185 | 'close_date_time', |
||
186 | 'start_date_time', |
||
187 | 'department_id', |
||
188 | 'province_id', |
||
189 | 'salary_min', |
||
190 | 'salary_max', |
||
191 | 'noc', |
||
192 | 'security_clearance_id', |
||
193 | 'language_requirement_id', |
||
194 | 'remote_work_allowed', |
||
195 | 'internal_only', |
||
196 | 'team_size', |
||
197 | 'work_env_features', |
||
198 | 'fast_vs_steady', |
||
199 | 'horizontal_vs_vertical', |
||
200 | 'experimental_vs_ongoing', |
||
201 | 'citizen_facing_vs_back_office', |
||
202 | 'collaborative_vs_independent', |
||
203 | 'telework_allowed_frequency_id', |
||
204 | 'flexible_hours_frequency_id', |
||
205 | 'travel_requirement_id', |
||
206 | 'overtime_requirement_id', |
||
207 | 'process_number', |
||
208 | 'priority_clearance_number', |
||
209 | 'loo_issuance_date', |
||
210 | 'classification_id', |
||
211 | 'classification_level', |
||
212 | 'city', |
||
213 | 'title', |
||
214 | 'dept_impact', |
||
215 | 'team_impact', |
||
216 | 'hire_impact', |
||
217 | 'division', |
||
218 | 'education', |
||
219 | 'work_env_description', |
||
220 | 'culture_summary', |
||
221 | 'culture_special', |
||
222 | 'job_poster_status_id', // This really shouldn't be mass-editable, but its necesary for the admin crud portal to set it. |
||
223 | ]; |
||
224 | |||
225 | /** |
||
226 | * The attributes that should be visible in arrays. |
||
227 | * In this case, it blocks loaded relations from appearing. |
||
228 | * |
||
229 | * @var array |
||
230 | */ |
||
231 | protected $visible = [ |
||
232 | 'id', |
||
233 | 'manager_id', |
||
234 | 'chosen_lang', |
||
235 | 'term_qty', |
||
236 | 'open_date_time', |
||
237 | 'close_date_time', |
||
238 | 'start_date_time', |
||
239 | 'department_id', |
||
240 | 'province_id', |
||
241 | 'salary_min', |
||
242 | 'salary_max', |
||
243 | 'noc', |
||
244 | 'security_clearance_id', |
||
245 | 'language_requirement_id', |
||
246 | 'remote_work_allowed', |
||
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 | 'city', |
||
264 | 'title', |
||
265 | 'dept_impact', |
||
266 | 'team_impact', |
||
267 | 'hire_impact', |
||
268 | 'division', |
||
269 | 'education', |
||
270 | 'work_env_description', |
||
271 | 'culture_summary', |
||
272 | 'culture_special', |
||
273 | 'job_poster_status_id', |
||
274 | 'created_at', |
||
275 | ]; |
||
276 | |||
277 | /** |
||
278 | * The accessors to append to the model's array form. |
||
279 | * |
||
280 | * @var string[] $appends |
||
281 | */ |
||
282 | protected $appends = [ |
||
283 | 'classification_code', |
||
284 | 'classification_message', |
||
285 | ]; |
||
286 | |||
287 | /** |
||
288 | * Eager loaded relationships by default. |
||
289 | * |
||
290 | * @var string[] $with |
||
291 | */ |
||
292 | protected $with = [ |
||
293 | 'criteria', |
||
294 | 'manager' |
||
295 | ]; |
||
296 | |||
297 | /** |
||
298 | * @var mixed[] $dispatchesEvents |
||
299 | */ |
||
300 | protected $dispatchesEvents = [ |
||
301 | 'saved' => JobSaved::class, |
||
302 | ]; |
||
303 | |||
304 | // @codeCoverageIgnoreStart |
||
305 | public function department() // phpcs:ignore |
||
306 | { |
||
307 | return $this->belongsTo(\App\Models\Lookup\Department::class); |
||
308 | } |
||
309 | |||
310 | public function job_term() // phpcs:ignore |
||
311 | { |
||
312 | return $this->belongsTo(\App\Models\Lookup\JobTerm::class); |
||
313 | } |
||
314 | |||
315 | public function language_requirement() // phpcs:ignore |
||
316 | { |
||
317 | return $this->belongsTo(\App\Models\Lookup\LanguageRequirement::class); |
||
318 | } |
||
319 | |||
320 | public function manager() // phpcs:ignore |
||
321 | { |
||
322 | return $this->belongsTo(\App\Models\Manager::class); |
||
323 | } |
||
324 | |||
325 | public function province() // phpcs:ignore |
||
326 | { |
||
327 | return $this->belongsTo(\App\Models\Lookup\Province::class); |
||
328 | } |
||
329 | |||
330 | public function security_clearance() // phpcs:ignore |
||
331 | { |
||
332 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
333 | } |
||
334 | |||
335 | public function criteria() // phpcs:ignore |
||
336 | { |
||
337 | return $this->hasMany(\App\Models\Criteria::class); |
||
338 | } |
||
339 | |||
340 | public function hr_advisors() // phpcs:ignore |
||
341 | { |
||
342 | return $this->belongsToMany( |
||
343 | \App\Models\HrAdvisor::class, |
||
344 | 'claimed_jobs' |
||
345 | ); |
||
346 | } |
||
347 | |||
348 | public function job_applications() // phpcs:ignore |
||
349 | { |
||
350 | return $this->hasMany(\App\Models\JobApplication::class); |
||
351 | } |
||
352 | |||
353 | public function job_poster_key_tasks() // phpcs:ignore |
||
354 | { |
||
355 | return $this->hasMany(\App\Models\JobPosterKeyTask::class)->orderBy('order', 'asc'); |
||
356 | } |
||
357 | |||
358 | public function job_poster_questions() // phpcs:ignore |
||
359 | { |
||
360 | return $this->hasMany(\App\Models\JobPosterQuestion::class); |
||
361 | } |
||
362 | |||
363 | public function telework_allowed_frequency() // phpcs:ignore |
||
364 | { |
||
365 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
366 | } |
||
367 | |||
368 | public function flexible_hours_frequency() // phpcs:ignore |
||
369 | { |
||
370 | return $this->belongsTo(\App\Models\Lookup\Frequency::class); |
||
371 | } |
||
372 | |||
373 | public function travel_requirement() // phpcs:ignore |
||
374 | { |
||
375 | return $this->belongsTo(\App\Models\Lookup\TravelRequirement::class); |
||
376 | } |
||
377 | |||
378 | public function overtime_requirement() // phpcs:ignore |
||
379 | { |
||
380 | return $this->belongsTo(\App\Models\Lookup\OvertimeRequirement::class); |
||
381 | } |
||
382 | |||
383 | public function classification() // phpcs:ignore |
||
384 | { |
||
385 | return $this->belongsTo(\App\Models\Classification::class); |
||
386 | } |
||
387 | |||
388 | public function comments() // phpcs:ignore |
||
389 | { |
||
390 | return $this->hasMany(\App\Models\Comment::class); |
||
391 | } |
||
392 | |||
393 | public function job_poster_status() // phpcs:ignore |
||
394 | { |
||
395 | return $this->belongsTo(\App\Models\Lookup\JobPosterStatus::class); |
||
396 | } |
||
397 | |||
398 | public function job_poster_status_histories() // phpcs:ignore |
||
399 | { |
||
400 | return $this->hasMany(\App\Models\JobPosterStatusHistory::class); |
||
401 | } |
||
402 | |||
403 | // @codeCoverageIgnoreEnd |
||
404 | /* Artificial Relations */ |
||
405 | |||
406 | /** |
||
407 | * Get all of the Job Applications submitted to this |
||
408 | * Job Poster. |
||
409 | * |
||
410 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
411 | */ |
||
412 | public function submitted_applications() // phpcs:ignore |
||
413 | { |
||
414 | return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query): void { |
||
415 | $query->where('name', 'draft'); |
||
416 | }); |
||
417 | } |
||
418 | |||
419 | /* Overrides */ |
||
420 | |||
421 | /** |
||
422 | * Retrieve the model for a bound value. |
||
423 | * Seems to be a useful workaround for providing submitted_applications_count |
||
424 | * to any bound routes that receive a jobPoster instance without using the |
||
425 | * withCount property on the model itself. |
||
426 | * See https://github.com/laravel/framework/issues/23957 for more info. |
||
427 | * |
||
428 | * @param mixed $value Value used to retrieve the model instance. |
||
429 | * |
||
430 | * @return \Illuminate\Database\Eloquent\Model|null |
||
431 | */ |
||
432 | public function resolveRouteBinding($value) // phpcs:ignore |
||
433 | { |
||
434 | return $this->withCount('submitted_applications')->where('id', $value)->first() ?? abort(404); |
||
|
|||
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->job_poster_status->key === 'live'; |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Check if a Job Poster is closed for applications. |
||
488 | * |
||
489 | * @return boolean |
||
490 | */ |
||
491 | public function isClosed(): bool |
||
492 | { |
||
493 | return ($this->job_poster_status->key === 'assessment' |
||
494 | || $this->job_poster_status->key === 'completed'); |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Return true if this job should be visible to hr advisors. |
||
499 | * It should become visible after Manager has requested a review. |
||
500 | * |
||
501 | * @return boolean |
||
502 | */ |
||
503 | public function isVisibleToHr() |
||
504 | { |
||
505 | return $this->job_poster_status->key !== 'draft'; |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Check if a Job Poster is open to public view. |
||
510 | * |
||
511 | * @return boolean |
||
512 | */ |
||
513 | public function isPublic(): bool |
||
514 | { |
||
515 | return ($this->job_poster_status->key == 'live' |
||
516 | || $this->job_poster_status->key == 'assessment' |
||
517 | || $this->job_poster_status->key == 'completed'); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Check if a Job Poster is theoretically editable. |
||
522 | * |
||
523 | * @return boolean |
||
524 | */ |
||
525 | public function isEditable(): bool |
||
526 | { |
||
527 | // Admins, at least, should be able to edit the job up until the public can see it. |
||
528 | return !$this->isPublic(); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Calculate the remaining time a Job Poster is open. |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | public function timeRemaining(): string |
||
562 | } |
||
563 | |||
564 | |||
565 | /** |
||
566 | * The database model stores a foreign id to the classification table, |
||
567 | * but to simplify the API, this model simply returns the key as classification_code. |
||
568 | * |
||
569 | * @return string|null |
||
570 | */ |
||
571 | public function getClassificationCodeAttribute() |
||
572 | { |
||
573 | if ($this->classification_id !== null) { |
||
574 | return $this->classification->key; |
||
575 | } |
||
576 | return null; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * |
||
581 | * Get the full government classification message. |
||
582 | * |
||
583 | * @return string|null |
||
584 | */ |
||
585 | public function getClassificationMessageAttribute() |
||
591 | } |
||
592 | } |
||
593 |