Passed
Push — feature/profile-skills-finaliz... ( 961836...9d523b )
by Tristan
06:45
created

Applicant::experiences_award()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Traits\TalentCloudCrudTrait as CrudTrait;
6
7
/**
8
 * Class Applicant
9
 *
10
 * @property int $id
11
 * @property string $personal_website
12
 * @property string $tagline
13
 * @property string $twitter_username
14
 * @property string $linkedin_url
15
 * @property int $user_id
16
 * @property boolean $is_snapshot
17
 * @property int $citizenship_declaration_id
18
 * @property int $veteran_status_id
19
 *
20
 * @property \Jenssegers\Date\Date $created_at
21
 * @property \Jenssegers\Date\Date $updated_at
22
 *
23
 * @property \App\Models\User $user
24
 * @property \Illuminate\Database\Eloquent\Collection $applicant_profile_answers
25
 * @property \Illuminate\Database\Eloquent\Collection $job_applications
26
 * @property \Illuminate\Database\Eloquent\Collection $submitted_applications
27
 * @property \Illuminate\Database\Eloquent\Collection $degrees
28
 * @property \Illuminate\Database\Eloquent\Collection $courses
29
 * @property \Illuminate\Database\Eloquent\Collection $work_experiences
30
 * @property \Illuminate\Database\Eloquent\Collection $skill_declarations
31
 * @property \Illuminate\Database\Eloquent\Collection $references
32
 * @property \Illuminate\Database\Eloquent\Collection $work_samples
33
 * @property \Illuminate\Database\Eloquent\Collection $projects
34
 * @property \Illuminate\Database\Eloquent\Collection $applicant_classifications
35
 * @property \Illuminate\Database\Eloquent\Collection $skills
36
 *
37
 * Version 2 application models.
38
 * @property \Illuminate\Database\Eloquent\Collection $experiences_work
39
 * @property \Illuminate\Database\Eloquent\Collection $experiences_personal
40
 * @property \Illuminate\Database\Eloquent\Collection $experiences_education
41
 * @property \Illuminate\Database\Eloquent\Collection $experiences_award
42
 * @property \Illuminate\Database\Eloquent\Collection $experiences_community
43
 *
44
 * @method \Illuminate\Database\Query\Builder experienceSkillsQuery
45
 */
46
class Applicant extends BaseModel
47
{
48
    // Trait for Backpack.
49
    use CrudTrait;
50
51
    protected $casts = [
52
        'user_id' => 'int',
53
        'personal_website' => 'string',
54
        'tagline' => 'string',
55
        'twitter_username' => 'string',
56
        'linkedin_url' => 'string',
57
        'is_snapshot' => 'boolean',
58
        'citizenship_declaration_id' => 'int',
59
        'veteran_status_id' => 'int',
60
    ];
61
    protected $fillable = [
62
        'personal_website',
63
        'tagline',
64
        'twitter_username',
65
        'linkedin_url',
66
        'citizenship_declaration_id' => 'int',
67
        'veteran_status_id' => 'int',
68
    ];
69
70
    public function user()
71
    {
72
        return $this->belongsTo(\App\Models\User::class);
73
    }
74
75
    public function applicant_profile_answers() //phpcs:ignore
76
    {
77
        return $this->hasMany(\App\Models\ApplicantProfileAnswer::class);
78
    }
79
80
    public function job_applications() //phpcs:ignore
81
    {
82
        if ($this->is_snapshot) {
83
            return $this->hasMany(\App\Models\JobApplication::class, 'applicant_snapshot_id');
84
        }
85
        return $this->hasMany(\App\Models\JobApplication::class);
86
    }
87
88
    /**
89
     * Get all of the Job Applications submitted by this applicant
90
     *
91
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
92
     */
93
    public function submitted_applications() // phpcs:ignore
94
    {
95
        return $this->hasMany(\App\Models\JobApplication::class)->whereDoesntHave('application_status', function ($query): void {
96
            $query->where('name', 'draft');
97
        });
98
    }
99
100
    public function degrees()
101
    {
102
        return $this->morphMany(\App\Models\Degree::class, 'degreeable')->orderBy('end_date', 'desc');
103
    }
104
105
    public function courses()
106
    {
107
        return $this->morphMany(\App\Models\Course::class, 'courseable')->orderBy('end_date', 'desc');
108
    }
109
110
    public function work_experiences() //phpcs:ignore
111
    {
112
        return $this->morphMany(\App\Models\WorkExperience::class, 'experienceable')->orderBy('end_date', 'desc');
113
    }
114
115
    public function skill_declarations() //phpcs:ignore
116
    {
117
        return $this->morphMany(\App\Models\SkillDeclaration::class, 'skillable');
118
    }
119
120
    public function references()
121
    {
122
        return $this->morphMany(\App\Models\Reference::class, 'referenceable');
123
    }
124
125
    public function work_samples() //phpcs:ignore
126
    {
127
        return $this->morphMany(\App\Models\WorkSample::class, 'work_sampleable');
128
    }
129
130
    public function projects()
131
    {
132
        return $this->morphMany(\App\Models\Project::class, 'projectable');
133
    }
134
135
    public function applicant_classifications() //phpcs:ignore
136
    {
137
        return $this->hasMany(\App\Models\ApplicantClassification::class);
138
    }
139
140
    public function skills() // phpcs:ignore
141
    {
142
        return $this->belongsToMany(\App\Models\Skill::class, 'applicant_skill');
143
    }
144
145
    // Version 2 application models.
146
147
    public function experiences_work() //phpcs:ignore
148
    {
149
        return $this->morphMany(\App\Models\ExperienceWork::class, 'experienceable')
150
            ->orderBy('end_date', 'desc');
151
    }
152
153
    public function experiences_personal() //phpcs:ignore
154
    {
155
        return $this->morphMany(\App\Models\ExperiencePersonal::class, 'experienceable')
156
            ->orderBy('end_date', 'desc');
157
    }
158
159
    public function experiences_education() //phpcs:ignore
160
    {
161
        return $this->morphMany(\App\Models\ExperienceEducation::class, 'experienceable')
162
            ->orderBy('end_date', 'desc');
163
    }
164
165
    public function experiences_award() //phpcs:ignore
166
    {
167
        return $this->morphMany(\App\Models\ExperienceAward::class, 'experienceable');
168
    }
169
170
    public function experiences_community() //phpcs:ignore
171
    {
172
        return $this->morphMany(\App\Models\ExperienceCommunity::class, 'experienceable')
173
            ->orderBy('end_date', 'desc');
174
    }
175
176
    /**
177
     * Returns a Laravel QueryBuilder object which will retrieve all ExperienceSkills
178
     * which are linked to this Applicant, through this Applicant's Experiences.
179
     *
180
     * It returns the query builder object instead of the results of the query, so that additional
181
     * clauses can be added by other code.
182
     *
183
     * @return \Illuminate\Database\Query\Builder
184
     */
185
    public function experienceSkillsQuery()
186
    {
187
        $applicantId = $this->id;
188
189
        // Create a subquery which gets all ExperienceSkill ids associated with one Experience type.
190
        $makeSubQuery = function ($experienceTable, $experienceType) use ($applicantId) {
191
            return function ($query) use ($experienceTable, $experienceType, $applicantId) {
192
                $query->select('experience_skills.id')->from('experience_skills')->join(
193
                    $experienceTable,
194
                    function ($join) use ($experienceTable, $experienceType, $applicantId) {
195
                        $join->on('experience_skills.experience_id', '=', "$experienceTable.id")
196
                            ->where('experience_skills.experience_type', $experienceType)
197
                            ->where("$experienceTable.experienceable_type", 'applicant')
198
                            ->where("$experienceTable.experienceable_id", $applicantId);
199
                    }
200
                );
201
            };
202
        };
203
204
        return ExperienceSkill::where(function ($query) use ($makeSubQuery) {
205
            // A single where clause wraps five OR WHERE clauses.
206
            // Each WHERE clause gets all the ExperienceSkills linked to a particular Experience type (and this Applicant).
207
            $query->orWhere(function ($query) use ($makeSubQuery) {
208
                $query->whereIn('id', $makeSubQuery('experiences_work', 'experience_work'));
209
            })->orWhere(function ($query) use ($makeSubQuery) {
210
                $query->whereIn('id', $makeSubQuery('experiences_personal', 'experience_personal'));
211
            })->orWhere(function ($query) use ($makeSubQuery) {
212
                $query->whereIn('id', $makeSubQuery('experiences_education', 'experience_education'));
213
            })->orWhere(function ($query) use ($makeSubQuery) {
214
                $query->whereIn('id', $makeSubQuery('experiences_award', 'experience_award'));
215
            })->orWhere(function ($query) use ($makeSubQuery) {
216
                $query->whereIn('id', $makeSubQuery('experiences_community', 'experience_community'));
217
            });
218
        });
219
    }
220
}
221