Cancelled
Push — feature/application-basic-info... ( 98c9e5...88f9a8 )
by Tristan
05:35 queued 01:32
created

DevSeeder::run()   C

Complexity

Conditions 9
Paths 192

Size

Total Lines 145
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 91
c 1
b 0
f 0
dl 0
loc 145
rs 6.0274
cc 9
nc 192
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\ExperienceSkill;
8
use App\Models\ExperienceWork;
9
use App\Models\HrAdvisor;
10
use App\Models\JobApplication;
11
use App\Models\JobPoster;
12
use App\Models\Manager;
13
use App\Models\Reference;
14
use App\Models\User;
15
use App\Models\WorkExperience;
16
use Illuminate\Database\Seeder;
17
18
class DevSeeder extends Seeder // phpcs:ignore
19
{
20
    /**
21
     * This seeder uses this email for an admin user.
22
     * Note: all seeded users have 'password' for a password.
23
     *
24
     * @var string
25
     */
26
    protected $adminEmail = '[email protected]';
27
28
    /**
29
     * This seeder connects all manager objects to this user.
30
     * Note: all seeded users have 'password' for a password.
31
     *
32
     * @var string
33
     */
34
    protected $managerEmail = '[email protected]';
35
36
    /**
37
     * This seeder attaches all applicant objects to this user.
38
     * Note: all seeded users have 'password' for a password.
39
     *
40
     * @var string
41
     */
42
    protected $applicantEmail = '[email protected]';
43
44
    /**
45
     * This seeder attaches all hr_advisor objects to this user.
46
     * Note: all seeded users have 'password' for a password.
47
     *
48
     * @var string
49
     */
50
    protected $hrAdvisorEmail = '[email protected]';
51
52
    /**
53
     * Note: all seeded users have 'password' for a password.
54
     *
55
     * @var string
56
     */
57
    protected $newApplicantEmail = '[email protected]';
58
59
60
    /**
61
     * Run the database seeds.
62
     *
63
     * @return void
64
     */
65
    public function run(): void
66
    {
67
        $adminUser = User::where('email', $this->adminEmail)->first();
68
        if ($adminUser === null) {
69
            $adminUser = factory(User::class)->state('admin')->create(['email' => $this->adminEmail]);
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

69
            $adminUser = /** @scrutinizer ignore-call */ factory(User::class)->state('admin')->create(['email' => $this->adminEmail]);
Loading history...
70
        }
71
72
        $hrUser = User::where('email', $this->hrAdvisorEmail)->first();
73
        if ($hrUser === null) {
74
            $hrUser = factory(User::class)->state('hr_advisor')->create([
75
                'email' => $this->hrAdvisorEmail,
76
            ]);
77
            $hrUser->hr_advisor()->save(factory(HrAdvisor::class)->create([
78
                'user_id' => $hrUser->id,
79
            ]));
80
        }
81
82
        $managerUser = User::where('email', $this->managerEmail)->first();
83
        // Create the test manager if it does not exist yet.
84
        if ($managerUser === null) {
85
            $managerUser = factory(User::class)->state('upgradedManager')->create(['email' => $this->managerEmail]);
86
            $managerUser->manager()->save(factory(Manager::class)->create([
87
                'user_id' => $managerUser->id
88
            ]));
89
        }
90
91
        factory(JobPoster::class, 3)->state('live')->create([
92
            'manager_id' => $managerUser->manager->id
93
        ])->each(function ($job): void {
94
            $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

94
            $job->job_applications()->saveMany(/** @scrutinizer ignore-call */ factory(JobApplication::class, 5))->create([
Loading history...
95
                'job_poster_id' => $job->id
96
            ]);
97
            // Then create one application with a priority user.
98
            $job->job_applications()->save(factory(JobApplication::class)->create([
99
                'job_poster_id' => $job->id,
100
                'applicant_id' => factory(Applicant::class)->create([
101
                    'user_id' => factory(User::class)->state('priority')->create()->id
102
                ])->id
103
            ]));
104
        });
105
        factory(JobPoster::class, 3)->state('closed')->create([
106
            'manager_id' => $managerUser->manager->id
107
        ])->each(function ($job): void {
108
            $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

108
            $job->job_applications()->saveMany(/** @scrutinizer ignore-call */ factory(JobApplication::class, 5))->create([
Loading history...
109
                'job_poster_id' => $job->id
110
            ]);
111
            // Then create one application with a priority user.
112
            $job->job_applications()->save(factory(JobApplication::class)->create([
113
                'job_poster_id' => $job->id,
114
                'applicant_id' => factory(Applicant::class)->create([
115
                    'user_id' => factory(User::class)->state('priority')->create()->id
116
                ])->id
117
            ]));
118
        });
119
        factory(JobPoster::class, 3)->state('draft')->create([
120
            'manager_id' => $managerUser->manager->id
121
        ]);
122
        factory(JobPoster::class, 3)->state('review_requested')->create([
123
            'manager_id' => $managerUser->manager->id
124
        ]);
125
126
        // Create a Job Poster with an Assessment Plan.
127
        $jobWithAssessment = factory(JobPoster::class)->state('draft')->create([
128
            'manager_id' => $managerUser->manager->id,
129
        ]);
130
        foreach ($jobWithAssessment->criteria as $criterion) {
131
            // Create an assessment for each criterion.
132
            factory(Assessment::class)->state('withRatingGuide')->create([
133
                'criterion_id' => $criterion->id,
134
            ]);
135
        };
136
137
        $applicantUser = User::where('email', $this->applicantEmail)->first();
138
        if ($applicantUser === null) {
139
            $applicantUser = factory(User::class)->state('applicant')->create([
140
                'email' => $this->applicantEmail
141
            ]);
142
            $applicantUser->applicant()->save(factory(Applicant::class)->create([
143
                'user_id' => $applicantUser->id
144
            ]));
145
        }
146
147
        $newApplicantUser = User::where('email', $this->newApplicantEmail)->first();
148
        if ($newApplicantUser === null) {
149
            $newApplicantUser = factory(User::class)->state('applicant')->create([
150
                'email' => $this->newApplicantEmail
151
            ]);
152
            $newApplicantUser->applicant()->save(factory(Applicant::class)->create([
153
                'user_id' => $newApplicantUser->id
154
            ]));
155
        }
156
157
        // Add to application profile.
158
        $applicantUser->applicant->references()->saveMany(factory(Reference::class, 3)->create([
159
            'referenceable_id' => $applicantUser->applicant->id
160
        ]));
161
        $applicantUser->applicant->degrees()->saveMany(factory(Degree::class, 2)->create([
162
            'degreeable_id' => $applicantUser->applicant->id
163
        ]));
164
        $applicantUser->applicant->courses()->saveMany(factory(Course::class, 3)->create([
165
            'courseable_id' => $applicantUser->applicant->id
166
        ]));
167
        $applicantUser->applicant->work_experiences()->saveMany(factory(WorkExperience::class, 3)->create([
168
            'experienceable_id' => $applicantUser->applicant->id
169
        ]));
170
171
        // Create several applications for test user.
172
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 3)->create([
173
            'applicant_id' => $applicantUser->applicant->id,
174
        ]));
175
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 2)->state('draft')->create([
176
            'applicant_id' => $applicantUser->applicant->id,
177
        ]));
178
179
        // Ensure there are several jobs the hr advisor can claim.
180
        $hrDepartment = $hrUser->department_id;
181
        factory(JobPoster::class)->states(['byUpgradedManager', 'draft'])
182
            ->create(['department_id' => $hrDepartment]);
183
        factory(JobPoster::class)->states(['byUpgradedManager', 'review_requested'])
184
            ->create(['department_id' => $hrDepartment]);
185
        $hrOpenJob = factory(JobPoster::class)->states(['byUpgradedManager', 'live'])
186
            ->create(['department_id' => $hrDepartment]);
187
        $hrClosedJob = factory(JobPoster::class)->states(['byUpgradedManager', 'closed'])
188
            ->create(['department_id' => $hrDepartment]);
189
190
        $hrOpenJob->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
191
            'job_poster_id' => $hrOpenJob->id
192
        ]);
193
        $hrClosedJob->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
194
            'job_poster_id' => $hrClosedJob->id
195
        ]);
196
197
        // Ensure applicant user has some Experience relevant to a job
198
        foreach ($applicantUser->applicant->job_applications->where('application_status_id', 1) as $application) {
199
            $job = $application->job_poster;
200
            $criterion = $job->criteria->first();
201
            if ($criterion) {
202
                $work = factory(ExperienceWork::class)->create([
203
                    'experienceable_id' => $application->applicant_id,
204
                    'experienceable_type' => 'applicant',
205
                ]);
206
                factory(ExperienceSkill::class)->create([
207
                    'skill_id' => $criterion->skill_id,
208
                    'experience_type' => 'experience_work',
209
                    'experience_id' => $work->id,
210
                ]);
211
            }
212
        }
213
    }
214
}
215