Completed
Push — dev ( 01a591...368594 )
by Chris
22s queued 11s
created

ApplicantPolicy::ownsJobApplicantAppliedTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.9
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace App\Policies;
4
5
use App\Models\User;
6
use App\Models\Applicant;
7
use App\Models\JobPoster;
8
use App\Policies\BasePolicy;
9
10
class ApplicantPolicy extends BasePolicy
11
{
12
13
    /**
14
     * Returns true if $user owns a job to which $applicant has applied.
15
     *
16
     * @param  \App\Models\User      $user      Generic User object for checking Manager relationship to Job Poster.
17
     * @param  \App\Models\Applicant $applicant Applicant object used within applications submitted to Job Poster.
18
     * @return boolean
19
     */
20 1
    protected function ownsJobApplicantAppliedTo(User $user, Applicant $applicant)
21 1
    {
22 1
        $applicant_id = $applicant->id;
23
        $user_id = $user->id;
24 1
        return JobPoster::whereHas(
25 1
            'manager',
26 1
            function ($q) use ($user_id) {
27
                $q->where('user_id', $user_id);
28
            }
29
        )->whereHas(
30
            'submitted_applications',
31
            function ($q) use ($applicant_id) {
32
                    $q->where('applicant_id', $applicant_id);
33
            }
34
        )->get()->isNotEmpty();
35
    }
36 3
37
    /**
38 3
     * Determine whether the user can view the applicant.
39 3
     *
40 3
     * @param  \App\Models\User      $user      User object making the view request.
41 3
     * @param  \App\Models\Applicant $applicant Applicant object to be viewed.
42
     * @return boolean
43
     */
44
    public function view(User $user, Applicant $applicant)
45
    {
46
        $authApplicant =  $user->isApplicant() &&
47
            $applicant->user->is($user);
48
        $authManager = $user->isManager() && $this->ownsJobApplicantAppliedTo($user, $applicant);
49
        return $authApplicant || $authManager;
50 1
    }
51
52 1
    /**
53
     * Determine whether the user can create applicants.
54
     *
55
     * @param  \App\Models\User $user User object making the create request.
56
     * @return boolean
57
     */
58
    public function create(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

58
    public function create(/** @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...
59
    {
60
        return false;
61
    }
62 1
63
    /**
64 1
     * Determine whether the user can update the applicant.
65 1
     *
66
     * @param  \App\Models\User      $user      User object making the update request.
67
     * @param  \App\Models\Applicant $applicant Applicant object being updated.
68
     * @return boolean
69
     */
70
    public function update(User $user, Applicant $applicant)
71
    {
72
        return $user->isApplicant() &&
73
            $applicant->user_id === $user->id;
74
    }
75
76
    /**
77
     * Determine whether the user can delete the applicant.
78
     *
79
     * @param  \App\Models\User      $user      User object making the delete request.
80
     * @param  \App\Models\Applicant $applicant Applicant object being deleted.
81
     * @return void
82
     */
83
    public function delete(User $user, Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $applicant 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

83
    public function delete(User $user, /** @scrutinizer ignore-unused */ Applicant $applicant)

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...
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

83
    public function delete(/** @scrutinizer ignore-unused */ User $user, Applicant $applicant)

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...
84
    {
85
    }
86
87
    /**
88
     * Determine whether the user can restore the applicant.
89
     *
90
     * @param  \App\Models\User      $user      User object making the restore request.
91
     * @param  \App\Models\Applicant $applicant Applicant object being restored.
92
     * @return void
93
     */
94
    public function restore(User $user, Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $applicant 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

94
    public function restore(User $user, /** @scrutinizer ignore-unused */ Applicant $applicant)

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...
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

94
    public function restore(/** @scrutinizer ignore-unused */ User $user, Applicant $applicant)

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...
95
    {
96
    }
97
98
    /**
99
     * Determine whether the user can permanently delete the applicant.
100
     *
101
     * @param  \App\Models\User      $user      User object making the forceDelete request.
102
     * @param  \App\Models\Applicant $applicant Applicant object being forceDeleted.
103
     * @return void
104
     */
105
    public function forceDelete(User $user, Applicant $applicant)
0 ignored issues
show
Unused Code introduced by
The parameter $applicant 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

105
    public function forceDelete(User $user, /** @scrutinizer ignore-unused */ Applicant $applicant)

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...
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

105
    public function forceDelete(/** @scrutinizer ignore-unused */ User $user, Applicant $applicant)

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...
106
    {
107
    }
108
}
109