| Total Complexity | 40 |
| Total Lines | 195 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Complex classes like JobPolicy 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 JobPolicy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class JobPolicy extends BasePolicy |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Determine whether the user can view the job poster. |
||
| 15 | * |
||
| 16 | * @param \App\Models\?User $user User object making the request. |
||
| 17 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 18 | * @return mixed |
||
| 19 | */ |
||
| 20 | public function view(?User $user, JobPoster $jobPoster) |
||
| 21 | { |
||
| 22 | // Anyone can view a published job |
||
| 23 | // Only the manager that created it can view an unpublished job |
||
| 24 | // Hr Advisors can view all jobs. |
||
| 25 | return $jobPoster->status() == 'published' || |
||
| 26 | $jobPoster->status() == 'closed' || |
||
| 27 | ($user && |
||
| 28 | $user->isManager() && |
||
| 29 | $jobPoster->manager->user_id == $user->id) || |
||
| 30 | ($user && |
||
| 31 | $user->isHrAdvisor() && |
||
| 32 | $user->hr_advisor->department_id === $jobPoster->department_id && |
||
| 33 | $jobPoster->isVisibleToHr()); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Any user is permitted to request a list of jobs, |
||
| 38 | * but only the jobs they are permitted to *view* should be returned. |
||
| 39 | * |
||
| 40 | * @param \App\Models\?User $user User object making the request. |
||
| 41 | * @return boolean |
||
| 42 | */ |
||
| 43 | public function viewAny(?User $user) |
||
| 44 | { |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Determine whether the user can create job posters. |
||
| 50 | * |
||
| 51 | * @param \App\Models\User $user User to test against. |
||
| 52 | * @return mixed |
||
| 53 | */ |
||
| 54 | public function create(User $user) |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Determine whether the user can update the job poster. |
||
| 62 | * |
||
| 63 | * @param \App\Models\User $user User object making the request. |
||
| 64 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | public function update(User $user, JobPoster $jobPoster) |
||
| 68 | { |
||
| 69 | // Only managers can edit jobs, and only their own, managers can't publish jobs or edit published jobs. |
||
| 70 | return $user->isManager() && |
||
| 71 | $jobPoster->manager->user->id == $user->id && |
||
| 72 | !$jobPoster->published; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Determine whether the user can delete the job poster. |
||
| 77 | * |
||
| 78 | * @param \App\Models\User $user User object making the request. |
||
| 79 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 80 | * |
||
| 81 | * @return boolean |
||
| 82 | */ |
||
| 83 | public function delete(User $user, JobPoster $jobPoster): bool |
||
| 84 | { |
||
| 85 | // Jobs can only be deleted when they're in the 'draft' |
||
| 86 | // state, and only by managers that created them. |
||
| 87 | return $user->isManager() && |
||
| 88 | $jobPoster->manager->user->id == $user->id && |
||
| 89 | !$jobPoster->published; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Determine whether the user can submit a job poster for review. |
||
| 94 | * |
||
| 95 | * @param \App\Models\User $user User object making the request. |
||
| 96 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 97 | * @return mixed |
||
| 98 | */ |
||
| 99 | public function submitForReview(User $user, JobPoster $jobPoster) |
||
| 106 | } |
||
| 107 | /** |
||
| 108 | * Determine whether the user can review applications to the job poster. |
||
| 109 | * |
||
| 110 | * @param \App\Models\User $user User object making the request. |
||
| 111 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public function reviewApplicationsFor(User $user, JobPoster $jobPoster) |
||
| 115 | { |
||
| 116 | // Managers can only review applications their own jobs. |
||
| 117 | // HR Advisors can review applications for jobs they manage. |
||
| 118 | // The job must always be closed. |
||
| 119 | $authManager = $user->isManager() && $jobPoster->manager->user->id == $user->id; |
||
| 120 | $authHr = $user->isHrAdvisor() && $this->manage($user, $jobPoster); |
||
| 121 | |||
| 122 | return $jobPoster->isClosed() && ($authManager || $authHr); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Determine whether the user is a Manager or an HR Advisor with permission to manage this job. |
||
| 127 | * |
||
| 128 | * @param \App\Models\User $user User object making the request. |
||
| 129 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 130 | * @return boolean |
||
| 131 | */ |
||
| 132 | public function manage(User $user, JobPoster $jobPoster) |
||
| 133 | { |
||
| 134 | return ($user->isManager() && |
||
| 135 | $jobPoster->manager->user->id == $user->id) || |
||
| 136 | ($user->isHrAdvisor() |
||
| 137 | && $this->view($user, $jobPoster) |
||
| 138 | && $user->hr_advisor->claimed_job_ids->contains($jobPoster->id)); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Determine whether the user can view the comments. |
||
| 143 | * |
||
| 144 | * @param \App\Models\User $user User object making the request. |
||
| 145 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 146 | * @return boolean |
||
| 147 | */ |
||
| 148 | public function viewComments(User $user, JobPoster $jobPoster): bool |
||
| 149 | { |
||
| 150 | // Only the manager that created the job can view the comment. |
||
| 151 | // Only Hr advisors who have claimed a job can view the comments. |
||
| 152 | return $this->manage($user, $jobPoster); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Determine whether the user can create a comment |
||
| 157 | * |
||
| 158 | * @param \App\Models\User $user User object making the request. |
||
| 159 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | public function storeComment(User $user, JobPoster $jobPoster): bool |
||
| 163 | { |
||
| 164 | // Only the manager that created the job can view the comment. |
||
| 165 | // Only Hr advisors who have claimed a job can view the comments. |
||
| 166 | return $this->viewComments($user, $jobPoster); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Determine whether the user can 'claim' this job. |
||
| 171 | * |
||
| 172 | * @param \App\Models\User $user User object making the request. |
||
| 173 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 174 | * @return boolean |
||
| 175 | */ |
||
| 176 | public function claim(User $user, JobPoster $jobPoster): bool |
||
| 177 | { |
||
| 178 | return $user->isHrAdvisor() && $this->view($user, $jobPoster); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Determine whether the user can 'unclaim' this job. |
||
| 183 | * |
||
| 184 | * @param \App\Models\User $user User object making the request. |
||
| 185 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | public function unClaim(User $user, JobPoster $jobPoster): bool |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Determine whether the user can view assessment plan. |
||
| 195 | * |
||
| 196 | * @param \App\Models\User $user User object making the request. |
||
| 197 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | public function viewAssessmentPlan(User $user, JobPoster $jobPoster): bool |
||
| 207 |