Passed
Push — master ( bfb104...6487ed )
by Reza
02:47
created

MakeCRUDConfig::getStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace EasyPanel\Commands;
5
6
use Illuminate\Console\GeneratorCommand;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class MakeCRUDConfig extends GeneratorCommand
10
{
11
12
    protected $name = 'panel:config';
13
    protected $type = 'Create a config';
14
15
    protected $description = 'Make a crud config';
16
17
    protected function getStub()
18
    {
19
        return __DIR__.'/stub/crud.stub';
20
    }
21
22
    public function handle()
23
    {
24
        if(!$this->option()){
25
            $this->info("model option must have a value");
26
            return;
27
        }
28
29
        $name = strtolower($this->getNameInput());
30
31
        $stub = $this->files->get(__DIR__.'/stub/crud.stub');
32
        $newStub = $this->parseStub($stub);
33
34
        $path = resource_path("cruds/{$name}.php");
35
36
        if (! $this->files->isDirectory(dirname($path))) {
37
            $this->files->makeDirectory(dirname($path), 0755, true);
38
        }
39
40
        if($this->files->exists($path) and !$this->option('force')){
41
            $this->warn("'{$name}' exists in CRUDs config");
42
            return;
43
        }
44
45
        if($name != strtolower($this->option('model'))){
46
            $this->warn("'{$name}' must be equal to model name");
47
            return;
48
        }
49
50
        $this->files->put($path, $newStub);
51
        $this->info("{$name} option was created, You can manage it in : resources/cruds/{$name}.php");
52
    }
53
54
    private function parseStub($stub)
55
    {
56
        $model = $this->qualifyModel($this->option('model'));
0 ignored issues
show
Bug introduced by
The method qualifyModel() does not exist on EasyPanel\Commands\MakeCRUDConfig. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        /** @scrutinizer ignore-call */ 
57
        $model = $this->qualifyModel($this->option('model'));
Loading history...
57
58
        if(!class_exists($model)){
59
            $this->warn("Model option should be valid and model should be exist");
60
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
61
        }
62
63
        $array = [
64
            '{{ model }}' => $model,
65
        ];
66
67
        return str_replace(array_keys($array), array_values($array), $stub);
68
    }
69
70
    protected function getOptions()
71
    {
72
        return [
73
            ['force', 'f', InputOption::VALUE_NONE, 'force mode'],
74
            ['model', 'm', InputOption::VALUE_REQUIRED, 'model name'],
75
        ];
76
    }
77
78
}
79