MakeCRUDConfig::qualifyModel()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 10
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
        $name = $this->getNameInput();
24
25
        $path = base_path("app/CRUD/{$name}Component.php");
26
27
        if($this->files->exists($path) and !$this->option('force')){
28
            $this->warn("'{$name}Component.php' already exists in CRUD directory");
29
            return;
30
        }
31
32
        $this->makeDirectory($path);
33
34
        $stub = $this->files->get($this->getStub());
35
        $newStub = $this->parseStub($stub);
36
37
        $this->files->put($path, $newStub);
38
        $this->info("{$name} config file was created for {$this->getNameInput()} model\nYou can manage it in : app/CRUD/{$name}Component.php");
39
    }
40
41
    private function parseStub($stub)
42
    {
43
        $array = [
44
            '{{ modelNamespace }}' => $this->parseModel(),
45
            '{{ modelName }}' => $this->getNameInput(),
46
            '{{ withAuth }}' => $this->withAuth(),
47
            '{{ fields }}' => $this->parseSearchFields(),
48
        ];
49
50
        return str_replace(array_keys($array), array_values($array), $stub);
51
    }
52
53
    protected function getOptions()
54
    {
55
        return [
56
            ['force', 'f', InputOption::VALUE_NONE, 'force mode'],
57
        ];
58
    }
59
60
    private function parseModel()
61
    {
62
        $model = $this->qualifyModel($this->getNameInput());
63
64
        if(!class_exists($model)){
65
            $this->warn("Model option should be valid and model should be exist");
66
            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...
67
        }
68
69
        return $model;
70
    }
71
72
    private function withAuth()
73
    {
74
        $fillableList = $this->getFillableList();
75
        if(!in_array('user_id', $fillableList)){
76
            return 'true';
77
        }
78
79
        return 'false';
80
    }
81
82
    private function getFillableList()
83
    {
84
        $modelNamespace = $this->qualifyModel($this->getNameInput());
85
        $modelInstance = new $modelNamespace;
86
        return $modelInstance->getFillable();
87
    }
88
89
    private function parseSearchFields()
90
    {
91
        $fillableList = $this->getFillableList();
92
        $array = [];
93
        foreach ($fillableList as $fillable){
94
            if(!Str::contains($fillable, 'id')){
95
                $array[] = "'$fillable'";
96
            }
97
        }
98
99
        return implode(', ', $array);
100
    }
101
102
    private function resolveStubPath($stub)
103
    {
104
        return file_exists($customPath = base_path(trim("stubs/panel/".$stub, '/')))
105
            ? $customPath
106
            : __DIR__.'/../stub/'.$stub;
107
    }
108
109
    protected function qualifyModel($model)
110
    {
111
        $model = ltrim($model, '\\/');
112
113
        $model = str_replace('/', '\\', $model);
114
115
        $rootNamespace = $this->rootNamespace();
116
117
        if (Str::startsWith($model, $rootNamespace)) {
118
            return $model;
119
        }
120
121
        return is_dir(app_path('Models'))
122
            ? $rootNamespace.'Models\\'.$model
123
            : $rootNamespace.$model;
124
    }
125
126
}
127