Completed
Push — develop ( e16864...bade13 )
by Abdelrahman
09:13
created

SeedCommand::handle()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Console\Commands;
6
7
use Carbon\Carbon;
8
use Cortex\Fort\Models\Role;
9
use Cortex\Fort\Models\User;
10
use Illuminate\Console\Command;
11
use Cortex\Fort\Models\Ability;
12
use Illuminate\Support\Facades\DB;
13
use Illuminate\Support\Facades\Schema;
14
use Illuminate\Database\Eloquent\Model;
15
16
class SeedCommand extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'cortex:seed';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Seed default Cortex data.';
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return void
36
     */
37
    public function handle()
38
    {
39
        Model::unguard();
40
41
        // WARNING: This action will delete all users/roles/abilities data (Can NOT be UNDONE!)
42
        if ($this->isFirstRun() || $this->confirm('WARNING! Your database already have data, this action will delete all users/roles/abilities (Can NOT be UNDONE!). Are you sure you want to continue?')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 204 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
43
            Schema::disableForeignKeyConstraints();
44
            DB::table(config('rinvex.fort.tables.abilities'))->truncate();
45
            DB::table(config('rinvex.fort.tables.roles'))->truncate();
46
            DB::table(config('rinvex.fort.tables.users'))->truncate();
47
            DB::table(config('rinvex.fort.tables.ability_user'))->truncate();
48
            DB::table(config('rinvex.fort.tables.role_user'))->truncate();
49
            DB::table(config('rinvex.fort.tables.ability_role'))->truncate();
50
            DB::table(config('rinvex.fort.tables.email_verifications'))->truncate();
51
            DB::table(config('auth.passwords.'.config('auth.defaults.passwords').'.table'))->truncate();
52
            DB::table(config('rinvex.fort.tables.persistences'))->truncate();
53
            DB::table(config('rinvex.fort.tables.socialites'))->truncate();
54
            DB::table(config('session.table'))->truncate();
55
            Schema::enableForeignKeyConstraints();
56
57
            // Seed data
58
            $this->seedAbilities();
59
            $this->seedRoles();
60
            $this->seedUsers();
61
        }
62
63
        Model::reguard();
64
    }
65
66
    /**
67
     * Check if this is first seed run.
68
     *
69
     * @return bool
70
     */
71
    protected function isFirstRun()
72
    {
73
        $userCount = User::count();
74
        $roleCount = Role::count();
75
        $abilityCount = Ability::count();
76
77
        return ! $userCount && ! $roleCount && ! $abilityCount;
78
    }
79
80
    /**
81
     * Seed default abilities.
82
     *
83
     * @return void
84
     */
85
    protected function seedAbilities()
86
    {
87
        // Get abilities data
88
        $abilities = json_decode(file_get_contents(__DIR__.'/../../../resources/data/abilities.json'), true);
89
90
        // Create new abilities
91
        foreach ($abilities as $ability) {
92
            Ability::create($ability);
93
        }
94
95
        $this->info('Default abilities seeded successfully!');
96
    }
97
98
    /**
99
     * Seed default roles.
100
     *
101
     * @return void
102
     */
103
    protected function seedRoles()
104
    {
105
        // Get roles data
106
        $roles = json_decode(file_get_contents(__DIR__.'/../../../resources/data/roles.json'), true);
107
108
        // Create new roles
109
        foreach ($roles as $role) {
110
            Role::create($role);
111
        }
112
113
        // Grant abilities to roles
114
        Role::where('slug', 'operator')->first()->grantAbilities('superadmin', 'global');
115
116
        $this->info('Default roles seeded successfully!');
117
    }
118
119
    /**
120
     * Seed default users.
121
     *
122
     * @return void
123
     */
124
    protected function seedUsers()
125
    {
126
        $user = [
127
            'username' => 'Cortex',
128
            'email' => '[email protected]',
129
            'email_verified' => true,
130
            'email_verified_at' => Carbon::now(),
131
            'remember_token' => str_random(10),
132
            'password' => $password = str_random(),
133
            'active' => true,
134
        ];
135
136
        $user = User::create($user);
137
138
        // Assign roles to users
139
        $user->assignRoles('operator');
140
141
        $this->info('Default users seeded successfully!');
142
        $this->getOutput()->writeln("<comment>Username</comment>: {$user['username']} / <comment>Password</comment>: {$password}");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
143
    }
144
}
145