Passed
Push — task/comment-feed ( 81a5e9...83e610 )
by Yonathan
19:04 queued 05:11
created

JobPolicy::view()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 14
rs 8.8333
c 1
b 0
f 0
cc 7
nc 15
nop 2
1
<?php
2
3
namespace App\Policies;
4
5
use App\Models\User;
6
use App\Models\JobPoster;
7
use App\Policies\BasePolicy;
8
use Illuminate\Support\Facades\Log;
9
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
17
     * @param  \App\Models\JobPoster  $jobPoster
18
     * @return mixed
19
     */
20
    public function view(?User $user, JobPoster $jobPoster)
0 ignored issues
show
Coding Style introduced by
Expected type hint "\App\Models\User"; found "?User" for $user
Loading history...
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
        (
27
            $user &&
28
            $user->isManager() &&
29
            $jobPoster->manager->user_id == $user->id
30
        ) ||
31
        (
32
            $user &&
33
            $user->isHrAdvisor()
34
        );
35
    }
36
37
    /**
38
     * Any user is permitted to request a list of jobs,
39
     * but only the jobs they are permitted to *view* should be returned.
40
     *
41
     * @return void
42
     */
43
    public function viewAny(?User $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public function viewAny(/** @scrutinizer ignore-unused */ ?User $user)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
55
    {
56
        // Any manager can create a new job poster.
57
        return $user->isManager();
58
    }
59
60
    /**
61
     * Determine whether the user can update the job poster.
62
     *
63
     * @param  \App\Models\User  $user
64
     * @param  \App\Models\JobPoster  $jobPoster
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
96
     * @param  \App\Models\JobPoster  $jobPoster
97
     * @return mixed
98
     */
99
    public function submitForReview(User $user, JobPoster $jobPoster)
100
    {
101
        // Only upgradedManagers can submit jobs for review, only their own jobs, and only if they're still drafts.
102
        // NOTE: this is one of the only permissions to require an upgradedManager, as opposed to a demoManager.
103
        return $user->isUpgradedManager() &&
104
            $jobPoster->manager->user->id == $user->id &&
105
            $jobPoster->status() === 'draft';
106
    }
107
    /**
108
     * Determine whether the user can review applications to the job poster.
109
     *
110
     * @param  \App\Models\User  $user
111
     * @param  \App\Models\JobPoster  $jobPoster
112
     * @return mixed
113
     */
114
    public function reviewApplicationsFor(User $user, JobPoster $jobPoster)
115
    {
116
        // Only managers can review applications, and only for their own jobs.
117
        return $user->isManager() &&
118
            $jobPoster->manager->user->id == $user->id &&
119
            $jobPoster->isClosed();
120
    }
121
122
     /**
123
     * Determine whether the user can view the comments.
124
     *
125
     * @param \App\Models\User $user
126
     * @param \App\Models\JobPoster $jobPoster
127
     * @return bool
128
     */
129
    public function viewComments(User $user, JobPoster $jobPoster) : bool
130
    {
131
        // Only the manager that created the job can view the comment.
132
        // Only Hr advisors who have claimed a job can view the comments.
133
        return $user->isManager() && $jobPoster->manager->user->id == $user->id ||
134
        $user->isHrAdvisor() && $jobPoster->hr_advisors->where('user_id', $user->id)->isNotEmpty();
135
    }
136
137
    /**
138
     * Determine whether the user can create a comment
139
     *
140
     * @param \App\Models\User $user User to test against
141
     * @param \App\Models\JobPoster $jobPoster
142
     * @return bool
143
     */
144
    public function storeComment(User $user, JobPoster $jobPoster) : bool
145
    {
146
        // Only the manager that created the job can view the comment.
147
        // Only Hr advisors who have claimed a job can view the comments.
148
        return $user->isManager() && $jobPoster->manager->user->id == $user->id ||
149
        $user->isHrAdvisor() && $jobPoster->hr_advisors->where('user_id', $user->id)->isNotEmpty();
150
    }
151
152
    /**
153
     * Determine whether the user can 'claim' this job.
154
     *
155
     * @param User $user
156
     * @param JobPoster $jobPoster
157
     * @return boolean
158
     */
159
    public function claim(User $user, JobPoster $jobPoster) : bool
0 ignored issues
show
Unused Code introduced by
The parameter $jobPoster is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

159
    public function claim(User $user, /** @scrutinizer ignore-unused */ JobPoster $jobPoster) : bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        return $user->isHrAdvisor();
162
    }
163
164
    /**
165
     * Determine whether the user can 'unclaim' this job.
166
     *
167
     * @param User $user
168
     * @param JobPoster $jobPoster
169
     * @return boolean
170
     */
171
    public function unClaim(User $user, JobPoster $jobPoster) : bool
172
    {
173
        return $this->claim($user, $jobPoster);
174
    }
175
}
176