Completed
Push — feature/screening-plan-redux ( d8e1fd...f4e53a )
by Tristan
20:23 queued 20:19
created

DevSeeder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 53
dl 0
loc 116
rs 10
c 3
b 0
f 0

1 Method

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