Passed
Push — feature/hr-portal ( 530ca5...24c6cc )
by Tristan
02:57
created

ApplicantPolicy::claimsJobApplicantAppliedTo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
rs 10
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\JobApplication;
8
use App\Models\JobPoster;
9
use App\Models\Lookup\ApplicationStatus;
10
use App\Policies\BasePolicy;
11
12
class ApplicantPolicy extends BasePolicy
13
{
14
15
    /**
16
     * Returns true if $user owns a job to which $applicant has applied.
17
     *
18
     * @param  \App\Models\User      $user      Generic User object for checking Manager relationship to Job Poster.
19
     * @param  \App\Models\Applicant $applicant Applicant object used within applications submitted to Job Poster.
20
     * @return boolean
21
     */
22
    protected function ownsJobApplicantAppliedTo(User $user, Applicant $applicant)
23
    {
24
        $applicant_id = $applicant->id;
25
        $user_id = $user->id;
26
        return JobPoster::whereHas(
27
            'manager',
28
            function ($q) use ($user_id) {
29
                $q->where('user_id', $user_id);
30
            }
31
        )->whereHas(
32
            'submitted_applications',
33
            function ($q) use ($applicant_id) {
34
                $q->where('applicant_id', $applicant_id);
35
            }
36
        )->get()->isNotEmpty();
37
    }
38
39
    /**
40
     * Returns true if the $user is an hr_advisor which has claimed a job the applicant has applied to, where the job is closed.
41
     *
42
     * @param  \App\Models\User      $user      HR advisor relationship to Job Poster.
43
     * @param  \App\Models\Applicant $applicant Applicant object used within applications submitted to Job Poster.
44
     * @return boolean
45
     */
46
    protected function claimsJobApplicantAppliedTo(User $user, Applicant $applicant)
47
    {
48
        if ($user->isHrAdvisor()) {
49
            $submittedApplications = JobApplication::where([
50
                'applicant_id' => $applicant->id,
51
                'application_status_id' => ApplicationStatus::where('name', 'draft')->id
52
            ]);
53
            return $submittedApplications->some(function ($application) use ($user) {
54
                return $user->can('manage', $application->job_poster) && $application->job_poster->isClosed();
55
            });
56
        }
57
        return false;
58
    }
59
60
    /**
61
     * Determine whether the user can view the applicant.
62
     *
63
     * @param  \App\Models\User      $user      User object making the view request.
64
     * @param  \App\Models\Applicant $applicant Applicant object to be viewed.
65
     * @return boolean
66
     */
67
    public function view(User $user, Applicant $applicant)
68
    {
69
        $authApplicant = $user->isApplicant() &&
70
            $applicant->user->is($user);
71
        $authManager = $user->isManager() && $this->ownsJobApplicantAppliedTo($user, $applicant);
72
        $authHr = $user->isHrAdvisor() && $this->claimsJobApplicantAppliedTo($user, $applicant);
73
        return $authApplicant || $authManager || $authHr;
74
    }
75
76
    /**
77
     * Determine whether the user can create applicants.
78
     *
79
     * @param  \App\Models\User $user User object making the create request.
80
     * @return boolean
81
     */
82
    public function create(User $user)
83
    {
84
        return false;
85
    }
86
87
    /**
88
     * Determine whether the user can update the applicant.
89
     *
90
     * @param  \App\Models\User      $user      User object making the update request.
91
     * @param  \App\Models\Applicant $applicant Applicant object being updated.
92
     * @return boolean
93
     */
94
    public function update(User $user, Applicant $applicant)
95
    {
96
        return $user->isApplicant() &&
97
            $applicant->user_id === $user->id;
98
    }
99
100
    /**
101
     * Determine whether the user can delete the applicant.
102
     *
103
     * @param  \App\Models\User      $user      User object making the delete request.
104
     * @param  \App\Models\Applicant $applicant Applicant object being deleted.
105
     * @return void
106
     */
107
    public function delete(User $user, Applicant $applicant)
108
    {
109
    }
110
111
    /**
112
     * Determine whether the user can restore the applicant.
113
     *
114
     * @param  \App\Models\User      $user      User object making the restore request.
115
     * @param  \App\Models\Applicant $applicant Applicant object being restored.
116
     * @return void
117
     */
118
    public function restore(User $user, Applicant $applicant)
119
    {
120
    }
121
122
    /**
123
     * Determine whether the user can permanently delete the applicant.
124
     *
125
     * @param  \App\Models\User      $user      User object making the forceDelete request.
126
     * @param  \App\Models\Applicant $applicant Applicant object being forceDeleted.
127
     * @return void
128
     */
129
    public function forceDelete(User $user, Applicant $applicant)
130
    {
131
    }
132
}
133