Passed
Pull Request — dev (#1344)
by Chris
05:58
created

JobPolicy::forceDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
rs 10
ccs 0
cts 1
cp 0
cc 1
nc 1
nop 2
crap 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
9
class JobPolicy extends BasePolicy
10
{
11
12
    /**
13
     * Determine whether the user can view the job poster.
14
     *
15
     * @param  \App\Models\User  $user
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
16
     * @param  \App\Models\JobPoster  $jobPoster
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
17
     * @return mixed
18
     */
19 12
    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...
20
    {
21
        // Anyone can view a published job
22
        // Only the manager that created it can view an unpublished job
23 12
        return $jobPoster->published ||
24
        (
25 6
            $user &&
26 6
            $user->isManager() &&
27 12
            $jobPoster->manager->user_id == $user->id
28
        );
29
    }
30
31
    /**
32
     * Determine whether the user can create job posters.
33
     *
34
     * @param  \App\Models\User $user User to test against.
35
     * @return mixed
36
     */
37 1
    public function create(User $user)
38
    {
39
        // Any manager can create a new job poster.
40 1
        return $user->isManager();
41
    }
42
43
    /**
44
     * Determine whether the user can update the job poster.
45
     *
46
     * @param  \App\Models\User  $user
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
47
     * @param  \App\Models\JobPoster  $jobPoster
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
48
     * @return mixed
49
     */
50 19
    public function update(User $user, JobPoster $jobPoster)
51
    {
52
        // Only managers can edit jobs, and only their own, managers can't publish jobs or edit published jobs
53 19
        return $user->isManager() &&
54 19
        $jobPoster->manager->user->id == $user->id &&
55 19
        !$jobPoster->published;
56
    }
57
58
    /**
59
     * Determine whether the user can delete the job poster.
60
     *
61
     * @param \App\Models\User      $user      User object making the request.
62
     * @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon.
63
     *
64
     * @return boolean
65
     */
66
    public function delete(User $user, JobPoster $jobPoster) : bool
67
    {
68
        // Jobs can only be deleted when they're in the 'draft'
69
        // state, and only by managers that created them.
70
        return $user->isManager() &&
71
        $jobPoster->manager->user->id == $user->id &&
72
        !$jobPoster->published;
73
    }
74
75
    /**
76
     * Determine whether the user can submit a job poster for review.
77
     *
78
     * @param  \App\Models\User  $user
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
79
     * @param  \App\Models\JobPoster  $jobPoster
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
80
     * @return mixed
81
     */
82
    public function submitForReview(User $user, JobPoster $jobPoster)
83
    {
84
        // Only upgradedManagers can submit jobs for review, only their own jobs, and only if they're still drafts.
85
        // NOTE: this is one of the only permissions to require an upgradedManager, as opposed to a demoManager.var
86
        return $user->isUpgradedManager() &&
87
            $jobPoster->manager->user->id == $user->id &&
88
            $jobPoster->status() === 'draft';
89
    }
90
    /**
91
     * Determine whether the user can review applications to the job poster.
92
     *
93
     * @param  \App\Models\User  $user
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
94
     * @param  \App\Models\JobPoster  $jobPoster
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
95
     * @return mixed
96
     */
97
    public function reviewApplicationsFor(User $user, JobPoster $jobPoster)
98
    {
99
        // Only managers can review applications, and only for their own jobs.
100
        return $user->isManager() &&
101
            $jobPoster->manager->user->id == $user->id &&
102
            $jobPoster->isClosed();
103
    }
104
}
105