Complex classes like Appointment 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Appointment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Appointment extends EloquentModel implements HasPresenter |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The attributes that are mass assignable. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $fillable = [ |
||
| 25 | 'issuer_id', |
||
| 26 | 'contact_id', |
||
| 27 | 'business_id', |
||
| 28 | 'service_id', |
||
| 29 | 'resource_id', |
||
| 30 | 'start_at', |
||
| 31 | 'finish_at', |
||
| 32 | 'duration', |
||
| 33 | 'comments', |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The attributes that aren't mass assignable. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $guarded = ['id', 'hash', 'status', 'vacancy_id']; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The attributes that should be mutated to dates. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $dates = ['start_at', 'finish_at']; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Appointment Hard Status Constants. |
||
| 52 | */ |
||
| 53 | const STATUS_RESERVED = 'R'; |
||
| 54 | const STATUS_CONFIRMED = 'C'; |
||
| 55 | const STATUS_CANCELED = 'A'; |
||
| 56 | const STATUS_SERVED = 'S'; |
||
| 57 | |||
| 58 | /////////////// |
||
| 59 | // PRESENTER // |
||
| 60 | /////////////// |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get Presenter Class. |
||
| 64 | * |
||
| 65 | * @return App\Presenters\AppointmentPresenter |
||
| 66 | */ |
||
| 67 | 1 | public function getPresenterClass() |
|
| 68 | { |
||
| 69 | 1 | return AppointmentPresenter::class; |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Generate hash and save the model to the database. |
||
| 74 | * |
||
| 75 | * @param array $options |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | 30 | public function save(array $options = []) |
|
| 80 | { |
||
| 81 | 30 | $this->doHash(); |
|
| 82 | |||
| 83 | 30 | return parent::save($options); |
|
| 84 | } |
||
| 85 | |||
| 86 | /////////////////// |
||
| 87 | // Relationships // |
||
| 88 | /////////////////// |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the issuer (the User that generated the Appointment). |
||
| 92 | * |
||
| 93 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 94 | */ |
||
| 95 | 4 | public function issuer() |
|
| 96 | { |
||
| 97 | 4 | return $this->belongsTo(config('auth.providers.users.model')); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the target Contact (for whom is reserved the Appointment). |
||
| 102 | * |
||
| 103 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 104 | */ |
||
| 105 | 4 | public function contact() |
|
| 106 | { |
||
| 107 | 4 | return $this->belongsTo(Contact::class); |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the holding Business (that has taken the reservation). |
||
| 112 | * |
||
| 113 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 114 | */ |
||
| 115 | 10 | public function business() |
|
| 116 | { |
||
| 117 | 10 | return $this->belongsTo(Business::class); |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the reserved Service. |
||
| 122 | * |
||
| 123 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 124 | */ |
||
| 125 | 8 | public function service() |
|
| 126 | { |
||
| 127 | 8 | return $this->belongsTo(Service::class); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Humanresource. |
||
| 132 | * |
||
| 133 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 134 | */ |
||
| 135 | 4 | public function humanresource() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get the Vacancy (that justifies the availability of resources for the |
||
| 142 | * Appointment generation). |
||
| 143 | * |
||
| 144 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 145 | */ |
||
| 146 | 6 | public function vacancy() |
|
| 147 | { |
||
| 148 | 6 | return $this->belongsTo(Vacancy::class); |
|
| 149 | } |
||
| 150 | |||
| 151 | /////////// |
||
| 152 | // Other // |
||
| 153 | /////////// |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the User through Contact. |
||
| 157 | * |
||
| 158 | * @return User |
||
| 159 | */ |
||
| 160 | 2 | public function user() |
|
| 161 | { |
||
| 162 | 2 | return $this->contact->user; |
|
|
|
|||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Determine if the new Appointment will hash-crash with another existing |
||
| 167 | * Appointment. |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | 5 | public function duplicates() |
|
| 172 | { |
||
| 173 | 5 | return !self::where('hash', $this->hash)->get()->isEmpty(); |
|
| 174 | } |
||
| 175 | |||
| 176 | 2 | public function duration() |
|
| 180 | |||
| 181 | /////////////// |
||
| 182 | // Accessors // |
||
| 183 | /////////////// |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get Hash. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 8 | public function getHashAttribute() |
|
| 191 | { |
||
| 192 | 8 | return isset($this->attributes['hash']) |
|
| 193 | 8 | ? $this->attributes['hash'] |
|
| 194 | 8 | : $this->doHash(); |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get Finish At: |
||
| 199 | * Calculates the start_at time plus duration in minutes. |
||
| 200 | * |
||
| 201 | * @return Carbon |
||
| 202 | */ |
||
| 203 | 3 | public function getFinishAtAttribute() |
|
| 204 | { |
||
| 205 | 3 | if (array_get($this->attributes, 'finish_at') !== null) { |
|
| 206 | return Carbon::parse($this->attributes['finish_at']); |
||
| 207 | } |
||
| 208 | |||
| 209 | 3 | if (is_numeric($this->duration)) { |
|
| 210 | 3 | return $this->start_at->addMinutes($this->duration); |
|
| 211 | } |
||
| 212 | |||
| 213 | return $this->start_at; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get cancellation deadline (target date). |
||
| 218 | * |
||
| 219 | * @return Carbon\Carbon |
||
| 220 | */ |
||
| 221 | public function getCancellationDeadlineAttribute() |
||
| 222 | { |
||
| 223 | $hours = $this->business->pref('appointment_cancellation_pre_hs'); |
||
| 224 | |||
| 225 | return $this->start_at |
||
| 226 | ->subHours($hours) |
||
| 227 | ->timezone($this->business->timezone); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the human readable status name. |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getStatusLabelAttribute() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get the date of the Appointment. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 3 | public function getDateAttribute() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get user-friendly unique identification code. |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 2 | public function getCodeAttribute() |
|
| 272 | |||
| 273 | ////////////// |
||
| 274 | // Mutators // |
||
| 275 | ////////////// |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Generate Appointment hash. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 30 | public function doHash() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Set start at. |
||
| 294 | * |
||
| 295 | * @param Carbon $datetime |
||
| 296 | */ |
||
| 297 | 30 | public function setStartAtAttribute(Carbon $datetime) |
|
| 298 | { |
||
| 299 | 30 | $this->attributes['start_at'] = $datetime; |
|
| 300 | 30 | } |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Set finish_at attribute. |
||
| 304 | * |
||
| 305 | * @param Carbon $datetime |
||
| 306 | */ |
||
| 307 | 11 | public function setFinishAtAttribute(Carbon $datetime) |
|
| 308 | { |
||
| 309 | 11 | $this->attributes['finish_at'] = $datetime; |
|
| 310 | 11 | } |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Set Comments. |
||
| 314 | * |
||
| 315 | * @param string $comments |
||
| 316 | */ |
||
| 317 | 30 | public function setCommentsAttribute($comments) |
|
| 318 | { |
||
| 319 | 30 | $this->attributes['comments'] = trim($comments) ?: null; |
|
| 320 | 30 | } |
|
| 321 | |||
| 322 | ///////////////// |
||
| 323 | // HARD STATUS // |
||
| 324 | ///////////////// |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Determine if is Reserved. |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | 1 | public function isReserved() |
|
| 332 | { |
||
| 333 | 1 | return $this->status == Self::STATUS_RESERVED; |
|
| 334 | } |
||
| 335 | |||
| 336 | /////////////////////////// |
||
| 337 | // Calculated attributes // |
||
| 338 | /////////////////////////// |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Appointment Status Workflow. |
||
| 342 | * |
||
| 343 | * Hard Status: Those concrete values stored in DB |
||
| 344 | * Soft Status: Those values calculated from stored values in DB |
||
| 345 | * |
||
| 346 | * Suggested transitions (Binding is not mandatory) |
||
| 347 | * Reserved -> Confirmed -> Served |
||
| 348 | * Reserved -> Served |
||
| 349 | * Reserved -> Canceled |
||
| 350 | * Reserved -> Confirmed -> Canceled |
||
| 351 | * |
||
| 352 | * Soft Status |
||
| 353 | * (Active) [ Reserved | Confirmed ] |
||
| 354 | * (InActive) [ Canceled | Served ] |
||
| 355 | */ |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Determine if is Active. |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | 5 | public function isActive() |
|
| 363 | { |
||
| 364 | return |
||
| 365 | 5 | $this->status == Self::STATUS_CONFIRMED || |
|
| 366 | 5 | $this->status == Self::STATUS_RESERVED; |
|
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Determine if is Pending. |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | 3 | public function isPending() |
|
| 375 | { |
||
| 376 | 3 | return $this->isActive() && $this->isFuture(); |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Determine if is Future. |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | 4 | public function isFuture() |
|
| 385 | { |
||
| 386 | 4 | return !$this->isDue(); |
|
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Determine if is due. |
||
| 391 | * |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | 5 | public function isDue() |
|
| 395 | { |
||
| 396 | 5 | return $this->start_at->isPast(); |
|
| 397 | } |
||
| 398 | |||
| 399 | //////////// |
||
| 400 | // Scopes // |
||
| 401 | //////////// |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Scope to Business. |
||
| 405 | * |
||
| 406 | * @param Illuminate\Database\Query $query |
||
| 407 | * |
||
| 408 | * @return Illuminate\Database\Query |
||
| 409 | */ |
||
| 410 | 1 | public function scopeOfBusiness($query, $businessId) |
|
| 414 | |||
| 415 | ///////////////////////// |
||
| 416 | // Hard Status Scoping // |
||
| 417 | ///////////////////////// |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Scope to Contacts Collection. |
||
| 421 | * |
||
| 422 | * @param Illuminate\Database\Query $query |
||
| 423 | * |
||
| 424 | * @return Illuminate\Database\Query |
||
| 425 | */ |
||
| 426 | public function scopeForContacts($query, $contacts) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Scope to Unarchived Appointments. |
||
| 433 | * |
||
| 434 | * @param Illuminate\Database\Query $query |
||
| 435 | * |
||
| 436 | * @return Illuminate\Database\Query |
||
| 437 | */ |
||
| 438 | public function scopeUnarchived($query) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Scope to Served Appointments. |
||
| 455 | * |
||
| 456 | * @param Illuminate\Database\Query $query |
||
| 457 | * |
||
| 458 | * @return Illuminate\Database\Query |
||
| 459 | */ |
||
| 460 | public function scopeServed($query) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Scope to Canceled Appointments. |
||
| 467 | * |
||
| 468 | * @param Illuminate\Database\Query $query |
||
| 469 | * |
||
| 470 | * @return Illuminate\Database\Query |
||
| 471 | */ |
||
| 472 | public function scopeCanceled($query) |
||
| 476 | |||
| 477 | ///////////////////////// |
||
| 478 | // Soft Status Scoping // |
||
| 479 | ///////////////////////// |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Scope to not Served Appointments. |
||
| 483 | * |
||
| 484 | * @param Illuminate\Database\Query $query |
||
| 485 | * |
||
| 486 | * @return Illuminate\Database\Query |
||
| 487 | */ |
||
| 488 | public function scopeUnServed($query) |
||
| 489 | { |
||
| 490 | return $query->where('status', '<>', Self::STATUS_SERVED); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Scope to Active Appointments. |
||
| 495 | * |
||
| 496 | * @param Illuminate\Database\Query $query |
||
| 497 | * |
||
| 498 | * @return Illuminate\Database\Query |
||
| 499 | */ |
||
| 500 | 18 | public function scopeActive($query) |
|
| 501 | { |
||
| 502 | 18 | return $query->whereIn('status', [Self::STATUS_RESERVED, Self::STATUS_CONFIRMED]); |
|
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Scope of date. |
||
| 507 | * |
||
| 508 | * @param Illuminate\Database\Query $query |
||
| 509 | * @param Carbon $date |
||
| 510 | * |
||
| 511 | * @return Illuminate\Database\Query |
||
| 512 | */ |
||
| 513 | public function scopeOfDate($query, Carbon $date) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Soft check of time interval affectation |
||
| 522 | * |
||
| 523 | * @param Illuminate\Database\Query $query |
||
| 524 | * @param Carbon $startAt |
||
| 525 | * @param Carbon $finishAt |
||
| 526 | * |
||
| 527 | * @return Illuminate\Database\Query |
||
| 528 | */ |
||
| 529 | 12 | public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Scope Affecting Humanresource. |
||
| 559 | * |
||
| 560 | * @param Illuminate\Database\Query $query |
||
| 561 | * |
||
| 562 | * @return Illuminate\Database\Query |
||
| 563 | */ |
||
| 564 | 12 | public function scopeAffectingHumanresource($query, $humanresourceId) |
|
| 572 | |||
| 573 | ////////////////////////// |
||
| 574 | // Soft Status Checkers // |
||
| 575 | ////////////////////////// |
||
| 576 | |||
| 577 | /** |
||
| 578 | * User is target contact of the appointment. |
||
| 579 | * |
||
| 580 | * @param int $userId |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function isTarget($userId) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * User is issuer of the appointment. |
||
| 591 | * |
||
| 592 | * @param int $userId |
||
| 593 | * |
||
| 594 | * @return bool |
||
| 595 | */ |
||
| 596 | public function isIssuer($userId) |
||
| 597 | { |
||
| 598 | return $this->issuer ? $this->issuer->id == $userId : false; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * User is owner of business. |
||
| 603 | * |
||
| 604 | * @param int $userId |
||
| 605 | * |
||
| 606 | * @return bool |
||
| 607 | */ |
||
| 608 | public function isOwner($userId) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * can be canceled by user. |
||
| 615 | * |
||
| 616 | * @param int $userId |
||
| 617 | * |
||
| 618 | * @return bool |
||
| 619 | */ |
||
| 620 | public function canCancel($userId) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Determine if it is still possible to cancel according business policy. |
||
| 629 | * |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | public function isOnTimeToCancel() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * can Serve. |
||
| 643 | * |
||
| 644 | * @param int $userId |
||
| 645 | * |
||
| 646 | * @return bool |
||
| 647 | */ |
||
| 648 | public function canServe($userId) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * can confirm. |
||
| 655 | * |
||
| 656 | * @param int $userId |
||
| 657 | * |
||
| 658 | * @return bool |
||
| 659 | */ |
||
| 660 | public function canConfirm($userId) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * is Serveable by user. |
||
| 667 | * |
||
| 668 | * @param int $userId |
||
| 669 | * |
||
| 670 | * @return bool |
||
| 671 | */ |
||
| 672 | public function isServeableBy($userId) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * is Confirmable By user. |
||
| 679 | * |
||
| 680 | * @param int $userId |
||
| 681 | * |
||
| 682 | * @return bool |
||
| 683 | */ |
||
| 684 | public function isConfirmableBy($userId) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * is cancelable By user. |
||
| 694 | * |
||
| 695 | * @param int $userId |
||
| 696 | * |
||
| 697 | * @return bool |
||
| 698 | */ |
||
| 699 | public function isCancelableBy($userId) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Determine if the queried userId may confirm the appointment or not. |
||
| 706 | * |
||
| 707 | * @param int $userId |
||
| 708 | * |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | public function shouldConfirmBy($userId) |
||
| 712 | { |
||
| 713 | return ($this->isSelfIssued() && $this->isOwner($userId)) || $this->isIssuer($userId); |
||
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Determine if the target Contact's User is the same of the Appointment |
||
| 718 | * issuer User. |
||
| 719 | * |
||
| 720 | * @return bool |
||
| 721 | */ |
||
| 722 | public function isSelfIssued() |
||
| 723 | { |
||
| 724 | if (!$this->issuer) { |
||
| 725 | return false; |
||
| 726 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Determine if the Serve action can be performed. |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | 1 | public function isServeable() |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Determine if the Confirm action can be performed. |
||
| 749 | * |
||
| 750 | * @return bool |
||
| 751 | */ |
||
| 752 | 1 | public function isConfirmable() |
|
| 756 | |||
| 757 | /** |
||
| 758 | * Determine if the cancelable action can be performed. |
||
| 759 | * |
||
| 760 | * @return bool |
||
| 761 | */ |
||
| 762 | 1 | public function isCancelable() |
|
| 766 | |||
| 767 | ///////////////////////// |
||
| 768 | // Hard Status Actions // |
||
| 769 | ///////////////////////// |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Check and perform Confirm action. |
||
| 773 | * |
||
| 774 | * @return $this |
||
| 775 | */ |
||
| 776 | 5 | public function doReserve() |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Check and perform Confirm action. |
||
| 787 | * |
||
| 788 | * @return $this |
||
| 789 | */ |
||
| 790 | 2 | public function doConfirm() |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Check and perform cancel action. |
||
| 803 | * |
||
| 804 | * @return $this |
||
| 805 | */ |
||
| 806 | 2 | public function doCancel() |
|
| 816 | |||
| 817 | /** |
||
| 818 | * Check and perform Serve action. |
||
| 819 | * |
||
| 820 | * @return $this |
||
| 821 | */ |
||
| 822 | 3 | public function doServe() |
|
| 832 | } |
||
| 833 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.