Passed
Push — task/common-translation-packag... ( dbb970...52324d )
by Tristan
06:50 queued 11s
created

ApplicantPolicy::claimsJobApplicantAppliedTo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
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
    protected function ownsJobApplicantAppliedTo(User $user, Applicant $applicant)
21
    {
22
        $applicant_id = $applicant->id;
23
        $user_id = $user->id;
24
        return JobPoster::whereHas(
25
            'manager',
26
            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
37
    /**
38
     * Returns true if the $user is an hr_advisor which has claimed a job the applicant has applied to, where the job is closed.
39
     *
40
     * @param  \App\Models\User      $user      HR advisor relationship to Job Poster.
41
     * @param  \App\Models\Applicant $applicant Applicant object used within applications submitted to Job Poster.
42
     * @return boolean
43
     */
44
    protected function claimsJobApplicantAppliedTo(User $user, Applicant $applicant)
45
    {
46
        if ($user->isHrAdvisor()) {
47
            return $applicant->submitted_applications->some(function ($application) use ($user) {
48
                return $user->can('manage', $application->job_poster) && $application->job_poster->isClosed();
49
            });
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * Determine whether the user can view the applicant.
56
     *
57
     * @param  \App\Models\User      $user      User object making the view request.
58
     * @param  \App\Models\Applicant $applicant Applicant object to be viewed.
59
     * @return boolean
60
     */
61
    public function view(User $user, Applicant $applicant)
62
    {
63
        $authApplicant = $user->isApplicant() &&
64
            $applicant->user->is($user);
65
        $authManager = $user->isManager() && $this->ownsJobApplicantAppliedTo($user, $applicant);
66
        $authHr = $user->isHrAdvisor() && $this->claimsJobApplicantAppliedTo($user, $applicant);
67
        return $authApplicant || $authManager || $authHr;
68
    }
69
70
    /**
71
     * Determine whether the user can create applicants.
72
     *
73
     * @param  \App\Models\User $user User object making the create request.
74
     * @return boolean
75
     */
76
    public function create(User $user)
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * Determine whether the user can update the applicant.
83
     *
84
     * @param  \App\Models\User      $user      User object making the update request.
85
     * @param  \App\Models\Applicant $applicant Applicant object being updated.
86
     * @return boolean
87
     */
88
    public function update(User $user, Applicant $applicant)
89
    {
90
        return $user->isApplicant() &&
91
            $applicant->user_id === $user->id;
92
    }
93
94
    /**
95
     * Determine whether the user can delete the applicant.
96
     *
97
     * @param  \App\Models\User      $user      User object making the delete request.
98
     * @param  \App\Models\Applicant $applicant Applicant object being deleted.
99
     * @return void
100
     */
101
    public function delete(User $user, Applicant $applicant)
102
    {
103
    }
104
105
    /**
106
     * Determine whether the user can restore the applicant.
107
     *
108
     * @param  \App\Models\User      $user      User object making the restore request.
109
     * @param  \App\Models\Applicant $applicant Applicant object being restored.
110
     * @return void
111
     */
112
    public function restore(User $user, Applicant $applicant)
113
    {
114
    }
115
116
    /**
117
     * Determine whether the user can permanently delete the applicant.
118
     *
119
     * @param  \App\Models\User      $user      User object making the forceDelete request.
120
     * @param  \App\Models\Applicant $applicant Applicant object being forceDeleted.
121
     * @return void
122
     */
123
    public function forceDelete(User $user, Applicant $applicant)
124
    {
125
    }
126
}
127