Passed
Push — master ( 33bfdb...f90911 )
by Reza
12:07
created

MakeCRUDConfig   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 3 1
A parseModel() 0 10 2
A parseStub() 0 9 1
A getOptions() 0 5 1
A parseSearchFields() 0 11 3
A withAuth() 0 8 2
A handle() 0 30 6
A getFillableList() 0 5 1
A resolveStubPath() 0 5 2
1
<?php
2
3
namespace EasyPanel\Commands\Actions;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
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
    protected $description = 'Make a crud config';
15
16
    protected function getStub()
17
    {
18
        return $this->resolveStubPath('crud.stub');
19
    }
20
21
    public function handle()
22
    {
23
        if(!$this->option('model')){
24
            $this->error("Model option must have a value");
25
            return;
26
        }
27
28
        $name = strtolower($this->getNameInput());
29
30
        if($name != strtolower($this->option('model'))){
31
            $this->warn("'{$name}' must be equal to model name");
32
            return;
33
        }
34
35
        $path = resource_path("cruds/{$name}.php");
36
37
        if($this->files->exists($path) and !$this->option('force')){
38
            $this->warn("'{$name}' already exists in CRUDs config");
39
            return;
40
        }
41
42
        if (! $this->files->isDirectory(dirname($path))) {
43
            $this->files->makeDirectory(dirname($path), 0755, true);
44
        }
45
46
        $stub = $this->files->get($this->getStub());
47
        $newStub = $this->parseStub($stub);
48
49
        $this->files->put($path, $newStub);
50
        $this->info("{$name} option was created, You can manage it in : resources/cruds/{$name}.php");
51
    }
52
53
    private function parseStub($stub)
54
    {
55
        $array = [
56
            '{{ model }}' => $this->parseModel(),
57
            '{{ withAuth }}' => $this->withAuth(),
58
            '{{ searchFields }}' => $this->parseSearchFields(),
59
        ];
60
61
        return str_replace(array_keys($array), array_values($array), $stub);
62
    }
63
64
    protected function getOptions()
65
    {
66
        return [
67
            ['model', 'm', InputOption::VALUE_REQUIRED, 'Model name'],
68
            ['force', 'f', InputOption::VALUE_NONE, 'force mode'],
69
        ];
70
    }
71
72
    private function parseModel()
73
    {
74
        $model = $this->qualifyModel($this->option('model'));
75
76
        if(!class_exists($model)){
77
            $this->warn("Model option should be valid and model should be exist");
78
            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...
79
        }
80
81
        return $model;
82
    }
83
84
    private function withAuth()
85
    {
86
        $fillableList = $this->getFillableList();
87
        if(!in_array('user_id', $fillableList)){
88
            return 'true';
89
        }
90
91
        return 'false';
92
    }
93
94
    private function getFillableList()
95
    {
96
        $modelNamespace = $this->qualifyModel($this->option('model'));
97
        $modelInstance = new $modelNamespace;
98
        return $modelInstance->getFillable();
99
    }
100
101
    private function parseSearchFields()
102
    {
103
        $fillableList = $this->getFillableList();
104
        $array = [];
105
        foreach ($fillableList as $fillable){
106
            if(!Str::contains($fillable, 'id')){
107
                $array[] = "'$fillable'";
108
            }
109
        }
110
111
        return implode(', ', $array);
112
    }
113
114
    private function resolveStubPath($stub)
115
    {
116
        return file_exists($customPath = $this->laravel->basePath(trim("stubs/panel/".$stub, '/')))
117
            ? $customPath
118
            : __DIR__.'/../stub/'.$stub;
119
    }
120
121
}
122