| Conditions | 21 |
| Paths | 1 |
| Total Lines | 77 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 72 | protected function defineGates(): void |
||
| 73 | { |
||
| 74 | Gate::define('view-assessment-plan', function ($user, $jobPoster) { |
||
| 75 | return $user->isAdmin() || |
||
| 76 | $user->isManager() && $jobPoster->manager->user_id === $user->id; |
||
| 77 | }); |
||
| 78 | |||
| 79 | /* |
||
| 80 | * Returns true if $user owns a job to which $applicant has applied. |
||
| 81 | */ |
||
| 82 | Gate::define('owns-job-applicant-applied-to', function ($user, $applicant) { |
||
| 83 | $applicant_id = $applicant->id; |
||
| 84 | $user_id = $user->id; |
||
| 85 | return JobPoster::whereHas( |
||
| 86 | 'manager', |
||
| 87 | function ($q) use ($user_id): void { |
||
| 88 | $q->where('user_id', $user_id); |
||
| 89 | } |
||
| 90 | )->whereHas( |
||
| 91 | 'submitted_applications', |
||
| 92 | function ($q) use ($applicant_id): void { |
||
| 93 | $q->where('applicant_id', $applicant_id); |
||
| 94 | } |
||
| 95 | )->get()->isNotEmpty(); |
||
| 96 | }); |
||
| 97 | |||
| 98 | /* |
||
| 99 | * Returns true if the $user is an hr_advisor which has claimed a job the applicant has applied to, |
||
| 100 | * where the job is closed. |
||
| 101 | */ |
||
| 102 | Gate::define('claims-job-applicant-applied-to', function ($user, $applicant) { |
||
| 103 | if ($user->isHrAdvisor()) { |
||
| 104 | return $applicant->submitted_applications->some(function ($application) use ($user) { |
||
| 105 | return $user->can('manage', $application->job_poster) && $application->job_poster->isClosed(); |
||
| 106 | }); |
||
| 107 | } |
||
| 108 | return false; |
||
| 109 | }); |
||
| 110 | |||
| 111 | /* Logged-in Users can view themselves. Admins can view everyone. Managers can view |
||
| 112 | * Applicants of their Job Posters. HR Advisors can view Managers |
||
| 113 | * within their department, and any Applicants of Job Posters created |
||
| 114 | * by those managers. |
||
| 115 | */ |
||
| 116 | |||
| 117 | /* TODO: User roles/permissions are getting a little unruly. I needed to add an |
||
| 118 | * additional check alongside isUpgradedManager() because we have an isAdmin() |
||
| 119 | * passthrough on that method, which was causing issues on the hr_advisor/manager |
||
| 120 | * reference. |
||
| 121 | */ |
||
| 122 | Gate::define('view-user', function ($user, $userProfile) { |
||
| 123 | return ( |
||
| 124 | // Any user can view themselves. |
||
| 125 | $user->id === $userProfile->id |
||
| 126 | ) || |
||
| 127 | ( |
||
| 128 | // Admins can view anyone. |
||
| 129 | $user->isAdmin() |
||
| 130 | ) || |
||
| 131 | ( |
||
| 132 | // Managers should be able to view HR Advisors within their department. |
||
| 133 | ($user->isUpgradedManager() && !$user->isAdmin() && $userProfile->isHrAdvisor() && !$userProfile->isAdmin()) && |
||
| 134 | ($user->manager->department_id === $userProfile->hr_advisor->department_id) |
||
| 135 | ) || |
||
| 136 | ( |
||
| 137 | // HR Advisors can view applicants that have applied to Job Posters that have been claimed. |
||
| 138 | ($user->isHrAdvisor() && $userProfile->applicant !== null) && |
||
| 139 | Gate::forUser($user)->allows('claims-job-applicant-applied-to', $userProfile->applicant) |
||
| 140 | ) || |
||
| 141 | ( |
||
| 142 | // Managers can view Applicants who have applied to their Job Posters. |
||
| 143 | (!$user->isAdmin() && $user->isUpgradedManager() && $userProfile->applicant !== null) && |
||
| 144 | Gate::forUser($user)->allows('owns-job-applicant-applied-to', $userProfile->applicant) |
||
| 145 | ) || |
||
| 146 | ( |
||
| 147 | // Manager profiles are viewable by any logged in User. |
||
| 148 | $user !== null && !$userProfile->isAdmin() && $userProfile->isUpgradedManager() |
||
| 149 | ); |
||
| 169 |