Passed
Push — master ( 618b98...bfb104 )
by Reza
02:45
created

MakeCRUD::modelNameIsCurrect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
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')] : 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.crud.' . $name);
23
            if (!$config) {
24
                throw new CommandNotFoundException("There is no {$name} in config file");
25
            }
26
27
            $this->modelNameIsCorrect($name, $config['model']);
28
29
            if (!$config['create']) {
30
                $this->warn("The create action is disabled for {$name}");
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 for {$name}");
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 modelNameIsCorrect($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