Passed
Push — master ( 6e1263...e73ba8 )
by Reza
03:36
created

MakeCRUDConfig::parseModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
4
namespace EasyPanel\Commands;
5
6
use App\Models\Article;
0 ignored issues
show
Bug introduced by
The type App\Models\Article was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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