Passed
Push — master ( d2ddcd...7d7316 )
by Reza
04:17
created

MakeCRUD::createActions()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 3
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace EasyPanel\Commands\Actions;
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
    protected $description = 'Create all action for CRUDs';
13
14
    public function handle()
15
    {
16
        $names = $this->argument('name') ? [$this->argument('name')] : config('easy_panel.actions', []);
17
18
        if(is_null($names)) {
19
            throw new CommandNotFoundException("There is no action in config file");
20
        }
21
22
        foreach ($names as $name) {
23
            $args = ['name' => $name, '--force' => $this->option('force')];
24
            $config = config("easy_panel.crud.$name");
25
26
            if (!$config) {
27
                throw new CommandNotFoundException("There is no {$name} in config file");
28
            }
29
30
            $this->modelNameIsCorrect($name, $config['model']);
31
            $this->createActions($config, $name, $args);
32
        }
33
    }
34
35
    private function modelNameIsCorrect($name, $model)
36
    {
37
        $model = explode('\\', $model);
38
        $model = strtolower(end($model));
39
40
        if($model != $name){
41
            throw new CommandNotFoundException("Action key should be equal to model name, You are using {$name} as key name but your model name is {$model}");
42
        }
43
    }
44
45
46
    private function createActions($config, $name, $args)
47
    {
48
        if (isset($config['create']) and $config['create']) {
49
            $this->call('panel:create', $args);
50
        } else {
51
            $this->warn("The create action is disabled for {$name}");
52
        }
53
54
        if (isset($config['update']) and $config['update']) {
55
            $this->call('panel:update', $args);
56
        } else {
57
            $this->warn("The update action is disabled for {$name}");
58
        }
59
60
        $this->call('panel:read', $args);
61
        $this->call('panel:single', $args);
62
    }
63
64
}
65