Completed
Push — master ( 8f08c7...7bd638 )
by Mohamed
02:25
created

InstallCommand::publishesAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Validator;
8
use Microboard\Factory;
9
use Microboard\Models\Role;
10
11
class InstallCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'microboard:install';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'It\'s publish the package assets, and migrate the database.';
26
27
    /**
28
     * @var Factory
29
     */
30
    protected $microboard;
31
32
    public function __construct(Factory $microboard)
33
    {
34
        parent::__construct();
35
36
        $this->microboard = $microboard;
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     * @throws Exception
44
     */
45
    public function handle()
46
    {
47
        $this->output->progressStart(4);
48
        $this->output->newLine();
49
50
        $this->callSilent('storage:link');
51
52
        // Migrate the database
53
        $this->callSilent('migrate');
54
        $this->info('Database tables have migrated');
55
        $this->output->progressAdvance();
56
        $this->output->newLine();
57
58
        // Publishes the assets
59
        $this->publishesAssets();
60
        $this->output->progressAdvance();
61
        $this->output->newLine();
62
63
        // Create default roles and permissions
64
        $admin = $this->createDefaultRolesAndPermissions();
65
        $this->output->progressAdvance();
66
        $this->output->newLine();
67
68
        // Create new admin
69
        $this->askForCreatingNewAdmin($admin);
70
        $this->output->progressAdvance();
71
        $this->output->newLine();
72
73
        $this->info('All done.');
74
        $this->info('Do something awesome!');
75
    }
76
77
    /**
78
     *
79
     */
80
    private function publishesAssets()
81
    {
82
        $this->callSilent('vendor:publish', [
83
            '--provider' => 'Microboard\\Providers\\MicroboardServiceProvider'
84
        ]);
85
        $this->callSilent('vendor:publish', [
86
            '--provider' => 'Microboard\\Providers\\ViewServiceProvider'
87
        ]);
88
        $this->callSilent('vendor:publish', [
89
            '--provider' => 'Yajra\DataTables\ButtonsServiceProvider'
90
        ]);
91
        $this->callSilent('vendor:publish', [
92
            '--provider' => 'Spatie\MediaLibrary\MediaLibraryServiceProvider',
93
            '--tag' => 'config'
94
        ]);
95
96
        $this->info('Assets have published');
97
    }
98
99
    /**
100
     * Create default roles and permissions
101
     *
102
     * @throws Exception
103
     */
104
    private function createDefaultRolesAndPermissions()
105
    {
106
        $admin = Role::firstOrCreate([
107
            'name' => 'admin'
108
        ], ['display_name' => 'Administrator']);
109
110
        Role::firstOrCreate([
111
            'name' => 'user'
112
        ], ['display_name' => 'Normal user']);
113
114
        if (config('microboard.resources.default_role', 'admin') === 'admin') {
115
            $role = $admin;
116
        } else {
117
            $role = Role::where('name', config('microboard.roles.default', 'admin'))->firstOrFail();
118
        }
119
120
        $this->microboard->createResourcesPermissionsFor($role, [
121
            'dashboard' => ['viewAny'],
122
            'settings' => ['viewAny', 'create', 'update'],
123
            'users' => null,
124
            'roles' => null
125
        ]);
126
127
        $this->info('Roles and permissions have created');
128
129
        return $admin;
130
    }
131
132
    /**
133
     * ask for creating a new admin
134
     *
135
     * @param Role $role
136
     * @return void
137
     */
138
    private function askForCreatingNewAdmin(Role $role)
139
    {
140
        if ($this->confirm('Create new admin?', false)) {
141
            $data = [
142
                'name' => $this->ask('Name'),
143
                'email' => $this->ask('Email'),
144
                'password' => $this->secret('Password')
145
            ];
146
147
            $validator = Validator::make($data, [
148
                'name' => ['required', 'string', 'min:2', 'max:200'],
149
                'email' => ['required', 'string', 'email', 'unique:users,email'],
150
                'password' => ['required', 'string', 'min:4', 'max:200']
151
            ]);
152
153
            if ($validator->fails()) {
154
                $this->error('Admin was not created, see the following errors');
155
                $errors = [];
156
157
                foreach (['name', 'email', 'password'] as $field) {
158
                    if ($validator->errors()->has($field)) {
159
                        $errors[] = [ucfirst($field), $validator->errors()->first($field)];
160
                    }
161
                }
162
163
                $this->output->table(['Field', 'Message'], $errors);
164
165
                $this->askForCreatingNewAdmin($role);
166
167
                return;
168
            }
169
170
            $role->users()->create($data);
171
172
            $this->info('Admin has created');
173
        }
174
    }
175
}
176