Passed
Push — task/comment-feed ( 4588e4...75c509 )
by Yonathan
09:06 queued 18s
created

JobPolicy   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 28
eloc 24
c 2
b 0
f 0
dl 0
loc 148
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 9 5
A create() 0 4 1
A reviewApplicationsFor() 0 6 3
A submitForReview() 0 7 3
A update() 0 6 3
A delete() 0 7 3
A viewComments() 0 6 4
A claim() 0 3 1
A unClaim() 0 3 1
A storeComment() 0 6 4
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
        return $jobPoster->status() == 'published' || $jobPoster->status() == 'closed' ||
25
        (
26
            $user &&
27
            $user->isManager() &&
28
            $jobPoster->manager->user_id == $user->id
29
        );
30
    }
31
32
    /**
33
     * Determine whether the user can create job posters.
34
     *
35
     * @param  \App\Models\User $user User to test against.
36
     * @return mixed
37
     */
38
    public function create(User $user)
39
    {
40
        // Any manager can create a new job poster.
41
        return $user->isManager();
42
    }
43
44
    /**
45
     * Determine whether the user can update the job poster.
46
     *
47
     * @param  \App\Models\User  $user
48
     * @param  \App\Models\JobPoster  $jobPoster
49
     * @return mixed
50
     */
51
    public function update(User $user, JobPoster $jobPoster)
52
    {
53
        // Only managers can edit jobs, and only their own, managers can't publish jobs or edit published jobs
54
        return $user->isManager() &&
55
        $jobPoster->manager->user->id == $user->id &&
56
        !$jobPoster->published;
57
    }
58
59
    /**
60
     * Determine whether the user can delete 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
     *
65
     * @return boolean
66
     */
67
    public function delete(User $user, JobPoster $jobPoster) : bool
68
    {
69
        // Jobs can only be deleted when they're in the 'draft'
70
        // state, and only by managers that created them.
71
        return $user->isManager() &&
72
        $jobPoster->manager->user->id == $user->id &&
73
        !$jobPoster->published;
74
    }
75
76
    /**
77
     * Determine whether the user can submit a job poster for review.
78
     *
79
     * @param  \App\Models\User  $user
80
     * @param  \App\Models\JobPoster  $jobPoster
81
     * @return mixed
82
     */
83
    public function submitForReview(User $user, JobPoster $jobPoster)
84
    {
85
        // Only upgradedManagers can submit jobs for review, only their own jobs, and only if they're still drafts.
86
        // NOTE: this is one of the only permissions to require an upgradedManager, as opposed to a demoManager.
87
        return $user->isUpgradedManager() &&
88
            $jobPoster->manager->user->id == $user->id &&
89
            $jobPoster->status() === 'draft';
90
    }
91
    /**
92
     * Determine whether the user can review applications to the job poster.
93
     *
94
     * @param  \App\Models\User  $user
95
     * @param  \App\Models\JobPoster  $jobPoster
96
     * @return mixed
97
     */
98
    public function reviewApplicationsFor(User $user, JobPoster $jobPoster)
99
    {
100
        // Only managers can review applications, and only for their own jobs.
101
        return $user->isManager() &&
102
            $jobPoster->manager->user->id == $user->id &&
103
            $jobPoster->isClosed();
104
    }
105
106
     /**
107
     * Determine whether the user can view the comments.
108
     *
109
     * @param \App\Models\User $user
110
     * @param \App\Models\JobPoster $jobPoster
111
     * @return bool
112
     */
113
    public function viewComments(User $user, JobPoster $jobPoster) : bool
114
    {
115
        // Only the manager that created a comment can view the comment.
116
        // Only Hr advisors who have claimed a job can view the comments.
117
        return $user->isManager() && $jobPoster->manager->user->id == $user->id ||
118
            $user->isHrAdvisor() && $jobPoster->getHrAdvisorByUserId($user->id);
119
    }
120
121
    /**
122
     * Determine whether the user can create a comment
123
     *
124
     * @param \App\Models\User $user User to test against
125
     * @param \App\Models\JobPoster $jobPoster
126
     * @return bool
127
     */
128
    public function storeComment(User $user, JobPoster $jobPoster) : bool
129
    {
130
        // Only the manager that created a comment can view the comment.
131
        // Only Hr advisors who have claimed a job can view the comments.
132
        return $user->isManager() && $jobPoster->manager->user->id == $user->id ||
133
        $user->isHrAdvisor() && $jobPoster->getHrAdvisorByUserId($user->id);
134
    }
135
136
    /**
137
     * Determine whether the user can 'claim' this job.
138
     *
139
     * @param User $user
140
     * @param JobPoster $jobPoster
141
     * @return boolean
142
     */
143
    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

143
    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...
144
    {
145
        return $user->isHrAdvisor();
146
    }
147
148
    /**
149
     * Determine whether the user can 'unclaim' this job.
150
     *
151
     * @param User $user
152
     * @param JobPoster $jobPoster
153
     * @return boolean
154
     */
155
    public function unClaim(User $user, JobPoster $jobPoster) : bool
156
    {
157
        return $this->claim($user, $jobPoster);
158
    }
159
}
160