Completed
Push — master ( 3e8f18...c69213 )
by Abdelrahman
02:02
created

SeedCommand::createMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Console\Commands;
6
7
use Cortex\Auth\Models\Admin;
8
use Cortex\Auth\Models\Member;
9
use Cortex\Auth\Models\Manager;
10
use Illuminate\Console\Command;
11
use Cortex\Auth\Models\Guardian;
12
13
class SeedCommand extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'cortex:seed:auth';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Seed Cortex Auth Data.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     */
34
    public function handle(): void
35
    {
36
        $this->warn($this->description);
37
38
        $this->call('db:seed', ['--class' => 'CortexAuthSeeder']);
39
40
        // Create models
41
        $admin = $this->createAdmin($adminPassword = str_random());
42
        $guardian = $this->createGuardian($guardianPassword = str_random());
43
        $manager = $this->createManager($managerPassword = str_random());
44
        $member = $this->createMember($memberPassword = str_random());
45
46
        // Assign roles
47
        $admin->assign('superadmin');
48
49
        $this->table(['Username', 'Password'], [
50
            ['username' => $admin['username'], 'password' => $adminPassword],
51
            ['username' => $guardian['username'], 'password' => $guardianPassword],
52
            ['username' => $manager['username'], 'password' => $managerPassword],
53
            ['username' => $member['username'], 'password' => $memberPassword],
54
        ]);
55
    }
56
57
    /**
58
     * Create admin model.
59
     *
60
     * @param string $password
61
     *
62
     * @return \Cortex\Auth\Models\Admin
63
     */
64
    protected function createAdmin(string $password): Admin
65
    {
66
        $admin = [
67
            'is_active' => true,
68
            'username' => 'Admin',
69
            'given_name' => 'Admin',
70
            'family_name' => 'User',
71
            'email' => '[email protected]',
72
        ];
73
74
        return tap(app('cortex.auth.admin')->firstOrNew($admin)->fill([
75
            'remember_token' => str_random(10),
76
            'email_verified_at' => now(),
77
            'password' => $password,
78
        ]), function ($instance) {
79
            $instance->save();
80
        });
81
    }
82
83
    /**
84
     * Create guardian model.
85
     *
86
     * @param string $password
87
     *
88
     * @return \Cortex\Auth\Models\Guardian
89
     */
90
    protected function createGuardian(string $password): Guardian
91
    {
92
        $guardian = [
93
            'is_active' => true,
94
            'username' => 'Guardian',
95
            'email' => '[email protected]',
96
        ];
97
98
        return tap(app('cortex.auth.guardian')->firstOrNew($guardian)->fill([
99
            'remember_token' => str_random(10),
100
            'password' => $password,
101
        ]), function ($instance) {
102
            $instance->save();
103
        });
104
    }
105
106
    /**
107
     * Create manager model.
108
     *
109
     * @param string $password
110
     *
111
     * @return \Cortex\Auth\Models\Manager
112
     */
113
    protected function createManager(string $password): Manager
114
    {
115
        $manager = [
116
            'is_active' => true,
117
            'username' => 'Manager',
118
            'given_name' => 'Manager',
119
            'family_name' => 'User',
120
            'email' => '[email protected]',
121
        ];
122
123
        return tap(app('cortex.auth.manager')->firstOrNew($manager)->fill([
124
            'remember_token' => str_random(10),
125
            'email_verified_at' => now(),
126
            'password' => $password,
127
        ]), function ($instance) {
128
            $instance->save();
129
        });
130
    }
131
132
    /**
133
     * Create member model.
134
     *
135
     * @param string $password
136
     *
137
     * @return \Cortex\Auth\Models\Member
138
     */
139
    protected function createMember(string $password): Member
140
    {
141
        $member = [
142
            'is_active' => true,
143
            'username' => 'Member',
144
            'given_name' => 'Member',
145
            'family_name' => 'User',
146
            'email' => '[email protected]',
147
        ];
148
149
        return tap(app('cortex.auth.member')->firstOrNew($member)->fill([
150
            'remember_token' => str_random(10),
151
            'email_verified_at' => now(),
152
            'password' => $password,
153
        ]), function ($instance) {
154
            $instance->save();
155
        });
156
    }
157
}
158