DevSeeder::run()   C
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 191
Code Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 191
rs 6.5688
c 0
b 0
f 0
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\Skill;
18
use App\Models\User;
19
use App\Models\WorkExperience;
20
use Illuminate\Database\Seeder;
21
22
class DevSeeder extends Seeder // phpcs:ignore
23
{
24
    /**
25
     * This seeder uses this email for an admin user.
26
     * Note: all seeded users have 'password' for a password.
27
     *
28
     * @var string
29
     */
30
    protected $adminEmail = '[email protected]';
31
32
    /**
33
     * This seeder connects all manager objects to this user.
34
     * Note: all seeded users have 'password' for a password.
35
     *
36
     * @var string
37
     */
38
    protected $managerEmail = '[email protected]';
39
40
    /**
41
     * This seeder attaches all applicant objects to this user.
42
     * Note: all seeded users have 'password' for a password.
43
     *
44
     * @var string
45
     */
46
    protected $applicantEmail = '[email protected]';
47
48
    /**
49
     * This seeder attaches all hr_advisor objects to this user.
50
     * Note: all seeded users have 'password' for a password.
51
     *
52
     * @var string
53
     */
54
    protected $hrAdvisorEmail = '[email protected]';
55
56
    /**
57
     * Note: all seeded users have 'password' for a password.
58
     *
59
     * @var string
60
     */
61
    protected $newApplicantEmail = '[email protected]';
62
63
64
    /**
65
     * Run the database seeds.
66
     *
67
     * @return void
68
     */
69
    public function run(): void
70
    {
71
        /*
72
        Get random department_id.
73
        Use departmentId to ensure any jobs that are attached to a given user belong to that user's department.
74
        */
75
        $departmentId = Department::inRandomOrder()->first()->id;
76
77
        $adminUser = User::where('email', $this->adminEmail)->first();
78
        if ($adminUser === null) {
79
            $adminUser = factory(User::class)->state('admin')->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

79
            $adminUser = /** @scrutinizer ignore-call */ factory(User::class)->state('admin')->create([
Loading history...
Unused Code introduced by
The assignment to $adminUser is dead and can be removed.
Loading history...
80
                'email' => $this->adminEmail,
81
                'department_id' => $departmentId,
82
            ]);
83
        }
84
85
        $hrUser = User::where('email', $this->hrAdvisorEmail)->first();
86
        if ($hrUser === null) {
87
            $hrUser = factory(User::class)->state('hr_advisor')->create([
88
                'email' => $this->hrAdvisorEmail,
89
                'department_id' => $departmentId,
90
            ]);
91
            $hrUser->hr_advisor()->save(factory(HrAdvisor::class)->create([
92
                'user_id' => $hrUser->id,
93
            ]));
94
        }
95
96
        $managerUser = User::where('email', $this->managerEmail)->first();
97
        // Create the test manager if it does not exist yet.
98
        if ($managerUser === null) {
99
            $managerUser = factory(User::class)->state('upgradedManager')->create([
100
                'email' => $this->managerEmail,
101
                'department_id' => $departmentId,
102
            ]);
103
            $managerUser->manager()->save(factory(Manager::class)->create([
104
                'user_id' => $managerUser->id,
105
            ]));
106
        }
107
108
        factory(JobPoster::class, 3)->state('live')->create([
109
            'manager_id' => $managerUser->manager->id,
110
            'department_id' => $departmentId,
111
        ])->each(function ($job): void {
112
            $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

112
            $job->job_applications()->saveMany(/** @scrutinizer ignore-call */ factory(JobApplication::class, 5))->create([
Loading history...
113
                'job_poster_id' => $job->id
114
            ]);
115
            // Then create one application with a priority user.
116
            $job->job_applications()->save(factory(JobApplication::class)->states(['version2', 'submitted'])->create([
117
                'job_poster_id' => $job->id,
118
                'applicant_id' => factory(Applicant::class)->create([
119
                    'user_id' => factory(User::class)->state('priority')->create()->id
120
                ])->id,
121
            ]));
122
        });
123
        factory(JobPoster::class, 3)->state('closed')->create([
124
            'manager_id' => $managerUser->manager->id,
125
            'department_id' => $departmentId,
126
        ])->each(function ($job): void {
127
            $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

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