Passed
Push — feature/application-translatio... ( 2578af...96b410 )
by Tristan
06:21
created

DevSeeder::run()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 130
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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