Passed
Push — feature/job-summary-hr ( 67ecfb )
by Grant
13:42
created

JobPolicy::claim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
        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 'claim' this job.
108
     *
109
     * @param User $user
110
     * @param JobPoster $jobPoster
111
     * @return boolean
112
     */
113
    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

113
    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...
114
    {
115
        return $user->isHrAdvisor();
116
    }
117
118
    /**
119
     * Determine whether the user can 'unclaim' this job.
120
     *
121
     * @param User $user
122
     * @param JobPoster $jobPoster
123
     * @return boolean
124
     */
125
    public function unClaim(User $user, JobPoster $jobPoster) : bool
126
    {
127
        return $this->claim($user, $jobPoster);
128
    }
129
}
130