Total Complexity | 41 |
Total Lines | 196 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 past the open date |
||
23 | // Managers can always view jobs they created. |
||
24 | // Hr Advisors can view all jobs in their department. |
||
25 | return $jobPoster->isPublic() || |
||
26 | ($user && |
||
27 | $user->isManager() && |
||
28 | $jobPoster->manager->user_id == $user->id) || |
||
29 | ($user && |
||
30 | $user->isHrAdvisor() && |
||
31 | $user->department_id === $jobPoster->department_id && |
||
32 | $jobPoster->isVisibleToHr()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Any user is permitted to request a list of jobs, |
||
37 | * but only the jobs they are permitted to *view* should be returned. |
||
38 | * |
||
39 | * @param \App\Models\?User $user User object making the request. |
||
40 | * @return boolean |
||
41 | */ |
||
42 | public function viewAny(?User $user) |
||
43 | { |
||
44 | return true; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Determine whether the user can create job posters. |
||
49 | * |
||
50 | * @param \App\Models\User $user User to test against. |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function create(User $user) |
||
54 | { |
||
55 | // Any manager can create a new job poster. |
||
56 | return $user->isManager(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Determine whether the user can update the job poster. |
||
61 | * |
||
62 | * @param \App\Models\User $user User object making the request. |
||
63 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
64 | * @return mixed |
||
65 | */ |
||
66 | public function update(User $user, JobPoster $jobPoster) |
||
67 | { |
||
68 | // Only managers can edit jobs, and only their own, managers can't publish jobs or edit published jobs. |
||
69 | return $user->isManager() && |
||
70 | $jobPoster->manager->user->id == $user->id && |
||
71 | $jobPoster->isEditable(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Determine whether the user can delete the job poster. |
||
76 | * |
||
77 | * @param \App\Models\User $user User object making the request. |
||
78 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
79 | * |
||
80 | * @return boolean |
||
81 | */ |
||
82 | public function delete(User $user, JobPoster $jobPoster): bool |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Determine whether the user can review applications to the job poster. |
||
93 | * |
||
94 | * @param \App\Models\User $user User object making the request. |
||
95 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function reviewApplicationsFor(User $user, JobPoster $jobPoster) |
||
99 | { |
||
100 | // Managers can only review applications their own jobs. |
||
101 | // HR Advisors can review applications for jobs they manage. |
||
102 | // The job must always be closed. |
||
103 | $authManager = $user->isManager() && $jobPoster->manager->user->id == $user->id; |
||
104 | $authHr = $user->isHrAdvisor() && $this->manage($user, $jobPoster); |
||
105 | |||
106 | // If the job is in Emergency Response department then it does not need to be closed to be viewed. |
||
107 | if ($jobPoster->isInStrategicResponseDepartment()) { |
||
108 | return $jobPoster->isPublic() && ($authManager || $authHr); |
||
109 | } |
||
110 | |||
111 | return $jobPoster->isClosed() && ($authManager || $authHr); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Determine whether the user is a Manager or an HR Advisor with permission to manage this job. |
||
116 | * |
||
117 | * @param \App\Models\User $user User object making the request. |
||
118 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
119 | * @return boolean |
||
120 | */ |
||
121 | public function manage(User $user, JobPoster $jobPoster) |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Determine whether the user can view the comments. |
||
132 | * |
||
133 | * @param \App\Models\User $user User object making the request. |
||
134 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
135 | * @return boolean |
||
136 | */ |
||
137 | public function viewComments(User $user, JobPoster $jobPoster): bool |
||
138 | { |
||
139 | // Only the manager that created the job can view the comment. |
||
140 | // Only Hr advisors who have claimed a job can view the comments. |
||
141 | return $this->manage($user, $jobPoster); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Determine whether the user can create a comment |
||
146 | * |
||
147 | * @param \App\Models\User $user User object making the request. |
||
148 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
149 | * @return boolean |
||
150 | */ |
||
151 | public function storeComment(User $user, JobPoster $jobPoster): bool |
||
152 | { |
||
153 | // Only the manager that created the job can view the comment. |
||
154 | // Only Hr advisors who have claimed a job can view the comments. |
||
155 | return $this->viewComments($user, $jobPoster); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Determine whether the user can 'claim' this job. |
||
160 | * |
||
161 | * @param \App\Models\User $user User object making the request. |
||
162 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
163 | * @return boolean |
||
164 | */ |
||
165 | public function claim(User $user, JobPoster $jobPoster): bool |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Determine whether the user can 'unclaim' this job. |
||
172 | * |
||
173 | * @param \App\Models\User $user User object making the request. |
||
174 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
175 | * @return boolean |
||
176 | */ |
||
177 | public function unClaim(User $user, JobPoster $jobPoster): bool |
||
178 | { |
||
179 | return $this->claim($user, $jobPoster); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Determine whether the user can view assessment plan. |
||
184 | * |
||
185 | * @param \App\Models\User $user User object making the request. |
||
186 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
187 | * @return boolean |
||
188 | */ |
||
189 | public function viewAssessmentPlan(User $user, JobPoster $jobPoster): bool |
||
190 | { |
||
191 | return $user->isAdmin() || |
||
192 | $user->isManager() && $jobPoster->manager->user_id === $user->id || |
||
193 | $user->isHrAdvisor() && $jobPoster->hr_advisors->contains('user_id', $user->id); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Determine whether the user can download CSV file of applicants who have applied to job. |
||
198 | * |
||
199 | * @param \App\Models\User $user User object making the request. |
||
200 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
201 | * @return boolean |
||
202 | */ |
||
203 | public function downloadApplicants(User $user, JobPoster $jobPoster): bool |
||
206 | } |
||
207 | } |
||
208 |