Conditions | 24 |
Paths | 1 |
Total Lines | 79 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | // Admins can view anyone. |
||
128 | $user->isAdmin()) || |
||
129 | ( |
||
130 | // Managers should be able to view HR Advisors within their department. |
||
131 | $user->isUpgradedManager() && ($user->manager !== null) |
||
132 | && !$user->isAdmin() |
||
133 | && $userProfile->isHrAdvisor() && ($userProfile->hr_advisor !== null) |
||
134 | && !$userProfile->isAdmin() |
||
135 | && ($user->manager->department_id === $userProfile->hr_advisor->department_id)) || |
||
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 | // Managers can view Applicants who have applied to their Job Posters. |
||
142 | (!$user->isAdmin() && $user->isUpgradedManager() && $userProfile->applicant !== null) && |
||
143 | Gate::forUser($user)->allows('owns-job-applicant-applied-to', $userProfile->applicant)) || |
||
144 | ( |
||
145 | // Manager profiles are viewable by any logged in User. |
||
146 | $user !== null && !$userProfile->isAdmin() && $userProfile->isUpgradedManager()); |
||
147 | }); |
||
148 | |||
149 | Gate::define('view-resources', function ($user) { |
||
150 | return $user->isUpgradedManager() || $user->isHrAdvisor(); |
||
151 | }); |
||
170 |