Passed
Push — task/skillcategory-admin ( 2464ec...a87176 )
by
unknown
04:24
created

DevSeeder::run()   C

Complexity

Conditions 8
Paths 128

Size

Total Lines 186
Code Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 107
c 7
b 0
f 0
dl 0
loc 186
rs 6.5688
cc 8
nc 128
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use App\Models\Applicant;
4
use App\Models\Assessment;
5
use App\Models\Course;
6
use App\Models\Degree;
7
use App\Models\Lookup\Department;
8
use App\Models\ExperienceEducation;
9
use App\Models\ExperienceSkill;
10
use App\Models\ExperienceWork;
11
use App\Models\HrAdvisor;
12
use App\Models\JobApplication;
13
use App\Models\JobPoster;
14
use App\Models\Manager;
15
use App\Models\Reference;
16
use App\Models\SkillCategory;
17
use App\Models\User;
18
use App\Models\WorkExperience;
19
use Illuminate\Database\Seeder;
20
21
class DevSeeder extends Seeder // phpcs:ignore
22
{
23
    /**
24
     * This seeder uses this email for an admin user.
25
     * Note: all seeded users have 'password' for a password.
26
     *
27
     * @var string
28
     */
29
    protected $adminEmail = '[email protected]';
30
31
    /**
32
     * This seeder connects all manager objects to this user.
33
     * Note: all seeded users have 'password' for a password.
34
     *
35
     * @var string
36
     */
37
    protected $managerEmail = '[email protected]';
38
39
    /**
40
     * This seeder attaches all applicant objects to this user.
41
     * Note: all seeded users have 'password' for a password.
42
     *
43
     * @var string
44
     */
45
    protected $applicantEmail = '[email protected]';
46
47
    /**
48
     * This seeder attaches all hr_advisor objects to this user.
49
     * Note: all seeded users have 'password' for a password.
50
     *
51
     * @var string
52
     */
53
    protected $hrAdvisorEmail = '[email protected]';
54
55
    /**
56
     * Note: all seeded users have 'password' for a password.
57
     *
58
     * @var string
59
     */
60
    protected $newApplicantEmail = '[email protected]';
61
62
63
    /**
64
     * Run the database seeds.
65
     *
66
     * @return void
67
     */
68
    public function run(): void
69
    {
70
        /*
71
        Get random department_id.
72
        Use departmentId to ensure any jobs that are attached to a given user belong to that user's department.
73
        */
74
        $departmentId = Department::inRandomOrder()->first()->id;
75
76
        $adminUser = User::where('email', $this->adminEmail)->first();
77
        if ($adminUser === null) {
78
            $adminUser = factory(User::class)->state('admin')->create([
0 ignored issues
show
Unused Code introduced by
The assignment to $adminUser is dead and can be removed.
Loading history...
Bug introduced by
The function factory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            $adminUser = /** @scrutinizer ignore-call */ factory(User::class)->state('admin')->create([
Loading history...
79
                'email' => $this->adminEmail,
80
                'department_id' => $departmentId,
81
            ]);
82
        }
83
84
        $hrUser = User::where('email', $this->hrAdvisorEmail)->first();
85
        if ($hrUser === null) {
86
            $hrUser = factory(User::class)->state('hr_advisor')->create([
87
                'email' => $this->hrAdvisorEmail,
88
                'department_id' => $departmentId,
89
            ]);
90
            $hrUser->hr_advisor()->save(factory(HrAdvisor::class)->create([
91
                'user_id' => $hrUser->id,
92
            ]));
93
        }
94
95
        $managerUser = User::where('email', $this->managerEmail)->first();
96
        // Create the test manager if it does not exist yet.
97
        if ($managerUser === null) {
98
            $managerUser = factory(User::class)->state('upgradedManager')->create([
99
                'email' => $this->managerEmail,
100
                'department_id' => $departmentId,
101
            ]);
102
            $managerUser->manager()->save(factory(Manager::class)->create([
103
                'user_id' => $managerUser->id,
104
            ]));
105
        }
106
107
        factory(JobPoster::class, 3)->state('live')->create([
108
            'manager_id' => $managerUser->manager->id,
109
            'department_id' => $departmentId,
110
        ])->each(function ($job): void {
111
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
0 ignored issues
show
Bug introduced by
The function factory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            $job->job_applications()->saveMany(/** @scrutinizer ignore-call */ factory(JobApplication::class, 5))->create([
Loading history...
112
                'job_poster_id' => $job->id
113
            ]);
114
            // Then create one application with a priority user.
115
            $job->job_applications()->save(factory(JobApplication::class)->states(['version2', 'submitted'])->create([
116
                'job_poster_id' => $job->id,
117
                'applicant_id' => factory(Applicant::class)->create([
118
                    'user_id' => factory(User::class)->state('priority')->create()->id
119
                ])->id,
120
            ]));
121
        });
122
        factory(JobPoster::class, 3)->state('closed')->create([
123
            'manager_id' => $managerUser->manager->id,
124
            'department_id' => $departmentId,
125
        ])->each(function ($job): void {
126
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
0 ignored issues
show
Bug introduced by
The function factory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
            $job->job_applications()->saveMany(/** @scrutinizer ignore-call */ factory(JobApplication::class, 5))->create([
Loading history...
127
                'job_poster_id' => $job->id
128
            ]);
129
            // Then create one application with a priority user.
130
            $job->job_applications()->save(factory(JobApplication::class)->states(['version2', 'submitted'])->create([
131
                'job_poster_id' => $job->id,
132
                'applicant_id' => factory(Applicant::class)->create([
133
                    'user_id' => factory(User::class)->state('priority')->create()->id
134
                ])->id,
135
            ]));
136
        });
137
        factory(JobPoster::class, 3)->state('draft')->create([
138
            'manager_id' => $managerUser->manager->id,
139
            'department_id' => $departmentId,
140
        ]);
141
        factory(JobPoster::class, 3)->state('review_requested')->create([
142
            'manager_id' => $managerUser->manager->id,
143
            'department_id' => $departmentId,
144
        ]);
145
146
        // Create a Job Poster with an Assessment Plan.
147
        $jobWithAssessment = factory(JobPoster::class)->state('draft')->create([
148
            'manager_id' => $managerUser->manager->id,
149
        ]);
150
        foreach ($jobWithAssessment->criteria as $criterion) {
151
            // Create an assessment for each criterion.
152
            factory(Assessment::class)->state('withRatingGuide')->create([
153
                'criterion_id' => $criterion->id,
154
            ]);
155
        };
156
157
        $applicantUser = User::where('email', $this->applicantEmail)->first();
158
        if ($applicantUser === null) {
159
            $applicantUser = factory(User::class)->state('applicant')->create([
160
                'email' => $this->applicantEmail
161
            ]);
162
            $applicantUser->applicant()->save(factory(Applicant::class)->create([
163
                'user_id' => $applicantUser->id
164
            ]));
165
        }
166
167
        $newApplicantUser = User::where('email', $this->newApplicantEmail)->first();
168
        if ($newApplicantUser === null) {
169
            $newApplicantUser = factory(User::class)->state('applicant')->create([
170
                'email' => $this->newApplicantEmail
171
            ]);
172
            $newApplicantUser->applicant()->save(factory(Applicant::class)->create([
173
                'user_id' => $newApplicantUser->id
174
            ]));
175
        }
176
177
        // Add to application profile.
178
        $applicantUser->applicant->references()->saveMany(factory(Reference::class, 3)->create([
179
            'referenceable_id' => $applicantUser->applicant->id
180
        ]));
181
        $applicantUser->applicant->degrees()->saveMany(factory(Degree::class, 2)->create([
182
            'degreeable_id' => $applicantUser->applicant->id
183
        ]));
184
        $applicantUser->applicant->courses()->saveMany(factory(Course::class, 3)->create([
185
            'courseable_id' => $applicantUser->applicant->id
186
        ]));
187
        $applicantUser->applicant->work_experiences()->saveMany(factory(WorkExperience::class, 3)->create([
188
            'experienceable_id' => $applicantUser->applicant->id
189
        ]));
190
191
        // Create several applications for test user.
192
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 1)->state('submitted')->create([
193
            'applicant_id' => $applicantUser->applicant->id,
194
        ]));
195
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 1)->states(['version2', 'submitted'])->create([
196
            'applicant_id' => $applicantUser->applicant->id,
197
        ]));
198
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 1)->state('draft')->create([
199
            'applicant_id' => $applicantUser->applicant->id,
200
        ]));
201
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 1)->states(['draft', 'version2'])->create([
202
            'applicant_id' => $applicantUser->applicant->id,
203
        ]));
204
205
        // Ensure there are several jobs the hr advisor can claim.
206
        $hrDepartment = $hrUser->department_id;
207
        factory(JobPoster::class)->states(['byUpgradedManager', 'draft'])
208
            ->create(['department_id' => $hrDepartment]);
209
        factory(JobPoster::class)->states(['byUpgradedManager', 'review_requested'])
210
            ->create(['department_id' => $hrDepartment]);
211
        $hrOpenJob = factory(JobPoster::class)->states(['byUpgradedManager', 'live'])
212
            ->create(['department_id' => $hrDepartment]);
213
        $hrClosedJob = factory(JobPoster::class)->states(['byUpgradedManager', 'closed'])
214
            ->create(['department_id' => $hrDepartment]);
215
216
        $hrOpenJob->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
217
            'job_poster_id' => $hrOpenJob->id
218
        ]);
219
        $hrClosedJob->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
220
            'job_poster_id' => $hrClosedJob->id
221
        ]);
222
223
         // Create first parent skill category.
224
        $skillCategoryParentFirst = factory(SkillCategory::class, 1)->create(['depth' => 1]);
225
226
        // Create second parent skill category.
227
        $skillCategoryParentSecond = factory(SkillCategory::class, 1)->create(['depth' => 1]);
228
229
        // Create child categories for the first parent category.
230
        factory(SkillCategory::class, 4)->create([
231
            'parent_id' => $skillCategoryParentFirst->first()->id,
232
            'depth' => 2
233
        ]);
234
235
        // Create child categories for the second parent category.
236
        factory(SkillCategory::class, 4)->create([
237
            'parent_id' => $skillCategoryParentSecond->first()->id,
238
            'depth' => 2
239
        ]);
240
241
        // Create relationship between skills and skill categories.
242
        $skills = DB::table('skills')->select('id', 'skill_type_id')->get();
243
        foreach ($skills as $skill) {
244
            DB::table('skill_categories_skills')->updateOrInsert(
245
                [
246
                    'skill_id' => $skill->id
247
                ],
248
                [
249
                    /*
250
                    Include skill categories created.
251
                    Exclude skill categories created that do not have children.
252
                    */
253
                    'skill_category_id' => rand(3, 10)
254
                ]
255
            );
256
        }
257
    }
258
}
259