Total Complexity | 40 |
Total Lines | 194 |
Duplicated Lines | 0 % |
Changes | 0 |
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' || $jobPoster->status() == 'closed' || |
||
26 | ($user && |
||
27 | $user->isManager() && |
||
28 | $jobPoster->manager->user_id == $user->id) || |
||
29 | ($user && |
||
30 | $user->isHrAdvisor() && |
||
31 | $user->hr_advisor->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->published; |
||
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 submit a job poster for review. |
||
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 submitForReview(User $user, JobPoster $jobPoster) |
||
99 | { |
||
100 | // Only upgradedManagers can submit jobs for review, only their own jobs, and only if they're still drafts. |
||
101 | // NOTE: this is one of the only permissions to require an upgradedManager, as opposed to a demoManager. |
||
102 | return $user->isUpgradedManager() && |
||
103 | $jobPoster->manager->user->id == $user->id && |
||
104 | $jobPoster->status() === 'draft'; |
||
105 | } |
||
106 | /** |
||
107 | * Determine whether the user can review applications to the job poster. |
||
108 | * |
||
109 | * @param \App\Models\User $user User object making the request. |
||
110 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function reviewApplicationsFor(User $user, JobPoster $jobPoster) |
||
114 | { |
||
115 | // Managers can only review applications their own jobs. |
||
116 | // HR Advisors can review applications for jobs they manage. |
||
117 | // The job must always be closed. |
||
118 | $authManager = $user->isManager() && $jobPoster->manager->user->id == $user->id; |
||
119 | $authHr = $user->isHrAdvisor() && $this->manage($user, $jobPoster); |
||
120 | |||
121 | return $jobPoster->isClosed() && ($authManager || $authHr); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Determine whether the user is a Manager or an HR Advisor with permission to manage this job. |
||
126 | * |
||
127 | * @param \App\Models\User $user User object making the request. |
||
128 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
129 | * @return boolean |
||
130 | */ |
||
131 | public function manage(User $user, JobPoster $jobPoster) |
||
132 | { |
||
133 | return ($user->isManager() && |
||
134 | $jobPoster->manager->user->id == $user->id) || |
||
135 | ($user->isHrAdvisor() |
||
136 | && $this->view($user, $jobPoster) |
||
137 | && $user->hr_advisor->claimed_job_ids->contains($jobPoster->id)); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Determine whether the user can view the comments. |
||
142 | * |
||
143 | * @param \App\Models\User $user User object making the request. |
||
144 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function viewComments(User $user, JobPoster $jobPoster): bool |
||
148 | { |
||
149 | // Only the manager that created the job can view the comment. |
||
150 | // Only Hr advisors who have claimed a job can view the comments. |
||
151 | return $this->manage($user, $jobPoster); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Determine whether the user can create a comment |
||
156 | * |
||
157 | * @param \App\Models\User $user User object making the request. |
||
158 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
159 | * @return boolean |
||
160 | */ |
||
161 | public function storeComment(User $user, JobPoster $jobPoster): bool |
||
162 | { |
||
163 | // Only the manager that created the job can view the comment. |
||
164 | // Only Hr advisors who have claimed a job can view the comments. |
||
165 | return $this->viewComments($user, $jobPoster); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Determine whether the user can 'claim' this job. |
||
170 | * |
||
171 | * @param \App\Models\User $user User object making the request. |
||
172 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function claim(User $user, JobPoster $jobPoster): bool |
||
176 | { |
||
177 | return $user->isHrAdvisor() && $this->view($user, $jobPoster); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Determine whether the user can 'unclaim' this job. |
||
182 | * |
||
183 | * @param \App\Models\User $user User object making the request. |
||
184 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public function unClaim(User $user, JobPoster $jobPoster): bool |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Determine whether the user can view assessment plan. |
||
194 | * |
||
195 | * @param \App\Models\User $user User object making the request. |
||
196 | * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
||
197 | * @return boolean |
||
198 | */ |
||
199 | public function viewAssessmentPlan(User $user, JobPoster $jobPoster): bool |
||
200 | { |
||
206 |