Passed
Push — master ( 314b9f...4e7aa2 )
by Reza
02:52
created

MakeCRUD::handle()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 17
c 2
b 1
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 14
nop 0
1
<?php
2
3
namespace EasyPanel\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Exception\CommandNotFoundException;
7
8
class MakeCRUD extends Command
9
{
10
11
    protected $signature = 'panel:crud {name?} {--f|force : Force mode}';
12
13
    protected $description = 'Create all action for CRUDs';
14
15
    public function handle()
16
    {
17
        $names = $this->argument('name') ? [$this->argument('name')] : array_keys(config('easy_panel.actions', []));
18
        if($names == null) {
19
            throw new CommandNotFoundException("There is no action in config file");
20
        }
21
        foreach ($names as $name) {
22
            $config = config('easy_panel.actions.' . $name);
23
            if (!$config) {
24
                throw new CommandNotFoundException("There is no {$name} in config file");
25
            }
26
27
            $this->modelNameIsCurrect($name, $config['model']);
28
29
            if (!$config['create']) {
30
                $this->warn('The create action is disabled');
31
            } else {
32
                $this->call('panel:create', ['name' => $name, '--force' => $this->option('force')]);
33
            }
34
35
            if (!$config['update']) {
36
                $this->warn('The update action is disabled');
37
            } else {
38
                $this->call('panel:update', ['name' => $name, '--force' => $this->option('force')]);
39
            }
40
41
            $this->call('panel:read', ['name' => $name, '--force' => $this->option('force')]);
42
        }
43
    }
44
45
    private function modelNameIsCurrect($name, $model)
46
    {
47
        $model = explode('\\', $model);
48
        $model = strtolower(end($model));
49
50
        if($model != $name){
51
            throw new CommandNotFoundException("Action key should be equal to model name, You are using {$name} as key name but your model name is {$model}");
52
        }
53
    }
54
55
}
56