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

CommandBase   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 98
rs 10
c 1
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildBlade() 0 12 2
A getOptions() 0 4 1
A handle() 0 26 4
A buildClass() 0 6 1
A getPath() 0 4 1
A getDefaultNamespace() 0 6 1
A setStubParser() 0 5 1
A getStub() 0 3 1
A resolveStubPath() 0 5 2
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
}
110