Completed
Push — develop ( b6f3ee...cff587 )
by Abdelrahman
01:51
created

SeedCommand::createManager()   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
44
        // Assign roles
45
        $admin->assign('superadmin');
46
47
        $this->table(['Username', 'Password'], [
48
            ['username' => $admin['username'], 'password' => $adminPassword],
49
            ['username' => $guardian['username'], 'password' => $guardianPassword],
50
        ]);
51
    }
52
53
    /**
54
     * Create admin model.
55
     *
56
     * @param string $password
57
     *
58
     * @return \Cortex\Auth\Models\Admin
59
     */
60
    protected function createAdmin(string $password): Admin
61
    {
62
        $admin = [
63
            'is_active' => true,
64
            'username' => 'Admin',
65
            'given_name' => 'Admin',
66
            'family_name' => 'User',
67
            'email' => '[email protected]',
68
        ];
69
70
        return tap(app('cortex.auth.admin')->firstOrNew($admin)->fill([
71
            'remember_token' => str_random(10),
72
            'email_verified_at' => now(),
73
            'password' => $password,
74
        ]), function ($instance) {
75
            $instance->save();
76
        });
77
    }
78
79
    /**
80
     * Create guardian model.
81
     *
82
     * @param string $password
83
     *
84
     * @return \Cortex\Auth\Models\Guardian
85
     */
86
    protected function createGuardian(string $password): Guardian
87
    {
88
        $guardian = [
89
            'is_active' => true,
90
            'username' => 'Guardian',
91
            'email' => '[email protected]',
92
        ];
93
94
        return tap(app('cortex.auth.guardian')->firstOrNew($guardian)->fill([
95
            'remember_token' => str_random(10),
96
            'password' => $password,
97
        ]), function ($instance) {
98
            $instance->save();
99
        });
100
    }
101
}
102