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