Completed
Push — develop ( c763e2...508664 )
by Abdelrahman
16:47
created

SeedCommand::createAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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 Illuminate\Console\Command;
9
use Cortex\Auth\Models\Guardian;
10
11
class SeedCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'cortex:seed:auth';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Seed Cortex Auth Data.';
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return void
31
     */
32
    public function handle(): void
33
    {
34
        $this->warn($this->description);
35
36
        $this->call('db:seed', ['--class' => 'CortexAuthSeeder']);
37
38
        // Create models
39
        $admin = $this->createAdmin($adminPassword = str_random());
40
        $guardian = $this->createGuardian($guardianPassword = str_random());
41
42
        // Assign roles
43
        $admin->assign('superadmin');
44
45
        $this->table(['Username', 'Password'], [
46
            ['username' => $admin['username'], 'password' => $adminPassword],
47
            ['username' => $guardian['username'], 'password' => $guardianPassword],
48
        ]);
49
    }
50
51
    /**
52
     * Create admin model.
53
     *
54
     * @param string $password
55
     *
56
     * @return \Cortex\Auth\Models\Admin
57
     */
58
    protected function createAdmin(string $password): Admin
59
    {
60
        $admin = [
61
            'is_active' => true,
62
            'username' => 'Admin',
63
            'email_verified' => true,
64
            'email' => '[email protected]',
65
        ];
66
67
        return tap(app('cortex.auth.admin')->firstOrNew($admin)->fill([
68
            'remember_token' => str_random(10),
69
            'email_verified_at' => now(),
70
            'password' => $password,
71
        ]), function ($instance) {
72
            $instance->save();
73
        });
74
    }
75
76
    /**
77
     * Create guardian model.
78
     *
79
     * @param string $password
80
     *
81
     * @return \Cortex\Auth\Models\Guardian
82
     */
83
    protected function createGuardian(string $password): Guardian
84
    {
85
        $guardian = [
86
            'is_active' => true,
87
            'username' => 'Guardian',
88
            'email' => '[email protected]',
89
        ];
90
91
        return tap(app('cortex.auth.guardian')->firstOrNew($guardian)->fill([
92
            'remember_token' => str_random(10),
93
            'password' => $password,
94
        ]), function ($instance) {
95
            $instance->save();
96
        });
97
    }
98
}
99