Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

RefreshDatabase::handle()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 7
nop 0
dl 0
loc 34
ccs 0
cts 19
cp 0
crap 72
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Console;
4
5
use Thinktomorrow\Chief\Pages\Page;
6
use Thinktomorrow\Chief\Authorization\AuthorizationDefaults;
7
use Illuminate\Database\Eloquent\Factory as ModelFactory;
8
use Illuminate\Support\Facades\Artisan;
9
10
class RefreshDatabase extends BaseCommand
11
{
12
    protected $signature = 'chief:refresh {--force}';
13
    protected $description = 'This will clear the entire database and reseed with development defaults';
14
15
    public function handle()
16
    {
17
        if (app()->environment() != 'local' && !$this->option('force')) {
18
            throw new \Exception('Aborting. This command is dangerous and only meant for your local environment.');
19
        }
20
21
        if (app()->environment() != 'local' && $this->option('force')) {
22
            if (!$this->confirm('You are about to force refresh the database in the '.app()->environment().' environment! ARE YOU SURE?')) {
23
                $this->info('aborting.');
24
                return;
25
            }
26
27
            if (!$this->confirm('ARE YOU REALLY SURE? THIS DELETES EVERYTHING!!!!!')) {
28
                $this->info('pfew.');
29
                return;
30
            }
31
        }
32
33
        if ($this->option('force')) {
34
            $this->call('migrate:fresh', ['--force' => true]);
35
        } else {
36
            $this->call('migrate:fresh');
37
        }
38
39
        // Include Our Chief factories for this command
40
        app(ModelFactory::class)->load(realpath(dirname(__DIR__).'/../database/factories'));
41
42
        $this->settingPermissionsAndRoles();
43
        $this->settingUsers();
44
45
        $this->info('Scaffolding some entries...');
46
        factory(Page::class, 5)->create();
47
48
        $this->info('Great. We\'re done here. NOW START HACKING!');
49
    }
50
51
    private function settingPermissionsAndRoles()
52
    {
53
        AuthorizationDefaults::permissions()->each(function ($permissionName) {
54
            Artisan::call('chief:permission', ['name' => $permissionName]);
55
        });
56
57
        AuthorizationDefaults::roles()->each(function ($defaultPermissions, $roleName) {
58
            Artisan::call('chief:role', ['name' => $roleName, '--permissions' => implode(',', $defaultPermissions)]);
59
        });
60
61
        $this->info('Default permissions and roles');
62
    }
63
64
    private function settingUsers()
65
    {
66
        /**
67
         * The developer who is scaffolding this data is in charge of picking the default
68
         * password. This password is set for all dev accounts. On a staging or production
69
         * environment there should be an user invite sent instead.
70
         */
71
        $this->info('Now please set one password for all dev accounts.');
72
        $password = $this->askPassword();
73
74
        $admins = collect([
75
            ['Philippe', 'Damen', '[email protected]', $password],
76
            ['Ben', 'Cavens', '[email protected]', $password],
77
            ['Johnny', 'Berkmans', '[email protected]', $password],
78
            ['Json', 'Voorhees', '[email protected]', $password],
79
        ]);
80
81
        $admins->each(function ($admin) {
82
            $this->createUser($admin[0], $admin[1], $admin[2], $admin[3], 'developer');
83
            $this->info('Added '.$admin[0].' as developer role with your provided password.');
84
        });
85
    }
86
}
87