Passed
Push — feature/job-builder/work-env-r... ( 3641b6...4a7aa4 )
by Tristan
22:01 queued 06:05
created

DevSeeder::run()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 85

Duplication

Lines 38
Ratio 44.71 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 0
dl 38
loc 85
rs 8.0161
c 0
b 0
f 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 Illuminate\Database\Seeder;
4
use App\Models\Manager;
5
use App\Models\User;
6
use App\Models\JobPoster;
7
use App\Models\Applicant;
8
use App\Models\JobApplication;
9
use App\Models\Reference;
10
use App\Models\Assessment;
11
12
class DevSeeder extends Seeder // phpcs:ignore
13
{
14
    /**
15
     * This seeder uses this email for an admin user.
16
     * Note: all seeded users have 'password' for a password.
17
     *
18
     * @var string
19
     */
20
    protected $adminEmail = '[email protected]';
21
22
    /**
23
     * This seeder connects all manager objects to this user.
24
     * Note: all seeded users have 'password' for a password.
25
     *
26
     * @var string
27
     */
28
    protected $managerEmail = '[email protected]';
29
30
    /**
31
     * This seeder attaches all applicant objects to this user.
32
     * Note: all seeded users have 'password' for a password.
33
     *
34
     * @var string
35
     */
36
    protected $applicantEmail = '[email protected]';
37
38
39
40
    /**
41
     * Run the database seeds.
42
     *
43
     * @return void
44
     */
45
    public function run() : void
46
    {
47
        $adminUser = User::where('email', $this->adminEmail)->first();
48
        if ($adminUser === null) {
49
            $adminUser = factory(User::class)->states('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...
50
        }
51
52
        $managerUser = User::where('email', $this->managerEmail)->first();
53
        // Create the test manager if it does not exist yet
54
        if ($managerUser === null) {
55
            $managerUser = factory(User::class)->states('manager')->create(['email' => $this->managerEmail]);
56
            $managerUser->manager()->save(factory(Manager::class)->create([
57
                'user_id' => $managerUser->id
58
            ]));
59
        }
60
61
        factory(JobPoster::class, 3)->states('published')->create([
62
            'manager_id' => $managerUser->manager->id
63
        ])->each(function ($job) : void {
64
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
65
                'job_poster_id' => $job->id
66
            ]);
67
            //Then create one application with a priority user
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// Then create one application with a priority user" but found "//Then create one application with a priority user"
Loading history...
68
            $job->job_applications()->save(factory(JobApplication::class)->create([
69
                'job_poster_id' => $job->id,
70
                'applicant_id' => factory(Applicant::class)->create([
71
                        'user_id' => factory(User::class)->states('priority')->create()->id
72
                    ])->id
73
            ]));
74
        });
75
        factory(JobPoster::class, 3)->states('closed')->create([
76
            'manager_id' => $managerUser->manager->id
77
        ])->each(function ($job) : void {
78
            $job->job_applications()->saveMany(factory(JobApplication::class, 5))->create([
79
                'job_poster_id' => $job->id
80
            ]);
81
            //Then create one application with a priority user
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// Then create one application with a priority user" but found "//Then create one application with a priority user"
Loading history...
82
            $job->job_applications()->save(factory(JobApplication::class)->create([
83
                'job_poster_id' => $job->id,
84
                'applicant_id' => factory(Applicant::class)->create([
85
                    'user_id' => factory(User::class)->state('priority')->create()->id
86
                ])->id
87
            ]));
88
        });
89
        factory(JobPoster::class, 3)->states('draft')->create([
90
            'manager_id' => $managerUser->manager->id
91
        ]);
92
        factory(JobPoster::class, 3)->states('review_requested')->create([
93
            'manager_id' => $managerUser->manager->id
94
        ]);
95
96
        // Create a Job Poster with an Assessment Plan
97
        $jobWithAssessment = factory(JobPoster::class)->states('draft')->create([
98
            'manager_id' => $managerUser->manager->id,
99
        ]);
100
        foreach ($jobWithAssessment->criteria as $criterion) {
101
            // Create an assessment for each criterion
102
            factory(Assessment::class)->states('withRatingGuide')->create([
103
                'criterion_id' => $criterion->id,
104
            ]);
105
        };
106
107
        $applicantUser = User::where('email', $this->applicantEmail)->first();
108
        if ($applicantUser === null) {
109
            $applicantUser = factory(User::class)->states('applicant')->create([
110
                'email' => $this->applicantEmail
111
            ]);
112
            $applicantUser->applicant()->save(factory(Applicant::class)->create([
113
                'user_id' => $applicantUser->id
114
            ]));
115
        }
116
117
        // Add to application profile
118
        $applicantUser->applicant->references()->saveMany(factory(Reference::class, 3)->create([
119
            'applicant_id' => $applicantUser->applicant->id
120
        ]));
121
122
        // Create several applications for test user.
123
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 3)->create([
124
            'applicant_id' => $applicantUser->applicant->id,
125
        ]));
126
        $applicantUser->applicant->job_applications()->saveMany(factory(JobApplication::class, 2)->states('draft')->create([
127
            'applicant_id' => $applicantUser->applicant->id,
128
        ]));
129
    }
130
}
131