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