Passed
Push — master ( b45e0e...135e97 )
by Reza
03:30
created

CommandBase::qualifyModel()   A

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\CRUDActions;
4
5
use EasyPanel\Parsers\StubParser;
6
use Illuminate\Console\GeneratorCommand;
7
use Symfony\Component\Console\Input\InputOption;
8
9
abstract class CommandBase extends GeneratorCommand
10
{
11
12
    /**
13
     * @var StubParser
14
     */
15
    private $stubParser;
16
17
    protected $path;
18
19
    public function getDefaultNamespace($rootNamespace)
20
    {
21
        $name = ucfirst($this->getNameInput());
22
        $this->path = parent::getDefaultNamespace($rootNamespace).DIRECTORY_SEPARATOR."Http".DIRECTORY_SEPARATOR."Livewire".DIRECTORY_SEPARATOR."Admin".DIRECTORY_SEPARATOR."$name";
23
24
        return $this->path;
25
    }
26
27
    protected function buildClass($name)
28
    {
29
        $stub = parent::buildClass($name);
30
        $stub = $this->stubParser->replaceModel($stub);
31
32
        return $stub;
33
    }
34
35
    protected function getPath($name)
36
    {
37
        $fileName = ucfirst($this->file);
38
        return "{$this->path}".DIRECTORY_SEPARATOR."{$fileName}.php";
39
    }
40
41
    protected function getStub()
42
    {
43
        return $this->resolveStubPath("{$this->file}.stub");
44
    }
45
46
    private function buildBlade()
47
    {
48
        $stub = $this->files->get($this->resolveStubPath("blade".DIRECTORY_SEPARATOR."{$this->file}.blade.stub"));
49
        $newStub = $this->stubParser->parseBlade($stub);
50
51
        $path = $this->viewPath("livewire".DIRECTORY_SEPARATOR."admin".DIRECTORY_SEPARATOR."{$this->getNameInput()}".DIRECTORY_SEPARATOR."{$this->file}.blade.php");
52
53
        if (! $this->files->isDirectory(dirname($path))) {
54
            $this->files->makeDirectory(dirname($path), 0755, true);
55
        }
56
57
        $this->files->put($path, $newStub);
58
    }
59
60
    public function handle()
61
    {
62
63
        $this->setStubParser();
64
65
        if ($this->isReservedName($this->getNameInput())) {
66
            $this->error("The name '{$this->getNameInput()}' is reserved by PHP.");
67
            return false;
68
        }
69
70
        $name = $this->qualifyClass($this->getNameInput());
71
        $path = $this->getPath($name);
72
        $path = str_replace('App', 'app', $path);
73
74
        if ($this->alreadyExists($this->getNameInput()) and !$this->option('force')) {
75
            $this->line("<options=bold,reverse;fg=red> • {$this->getNameInput()} {$this->type} already exists! </> \n");
76
77
            return false;
78
        }
79
80
        $this->makeDirectory($path);
81
82
        $this->files->put($path, $this->sortImports($this->buildClass($name)));
83
84
        $this->buildBlade();
85
        $this->line("<options=bold,reverse;fg=green> {$this->getNameInput()} {$this->type} created successfully. </> 🤙\n");
86
    }
87
88
    protected function getOptions()
89
    {
90
        return [
91
            ['force', 'f', InputOption::VALUE_NONE, 'force mode']
92
        ];
93
    }
94
95
    private function setStubParser()
96
    {
97
        $model = config("easy_panel.crud.{$this->getNameInput()}.model");
98
        $parsedModel = $this->qualifyModel($model);
99
        $this->stubParser = new StubParser($this->getNameInput(), $parsedModel);
100
    }
101
102
    private function resolveStubPath($stub)
103
    {
104
        return file_exists($customPath = $this->laravel->basePath(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)) {
0 ignored issues
show
Bug introduced by
The type EasyPanel\Commands\CRUDActions\Str 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...
118
            return $model;
119
        }
120
121
        return is_dir(app_path('Models'))
122
            ? $rootNamespace.'Models\\'.$model
123
            : $rootNamespace.$model;
124
    }
125
}
126