GenerateModelCommand::handle()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 12
1
<?php
2
3
namespace LaravelModulize\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use LaravelModulize\Support\Entity;
7
8
class GenerateModelCommand extends BaseGeneratorCommand
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'modulize:make:model {module} {name} {--c|controller} {--m|migration}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Command to generate a Model inside a module';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return mixed
28
     */
29
    public function handle()
30
    {
31
        parent::handle();
32
33
        if ($this->option('controller')) {
34
            $this->call('modulize:make:controller', [
35
                'module' => $this->module,
36
                'name' => Entity::getClassName($this->getNameInput()) . 'Controller',
37
                '--model' => $this->getNameInput(),
38
            ]);
39
        }
40
41
        if ($this->option('migration')) {
42
            $tableName = Str::plural(Str::snake(Entity::getClassName($this->getNameInput())));
43
            $this->call('make:migration', [
44
                'name' => "create_{$tableName}_table",
45
                '--create' => $tableName
46
            ]);
47
        }
48
    }
49
50
    /**
51
     * Get the stub file for the generator.
52
     *
53
     * @return string
54
     */
55
    protected function getStub(): string
56
    {
57
        return __DIR__ . '/stubs/Model.stub';
58
    }
59
60
    /**
61
     * Get the destination class path.
62
     *
63
     * @param  string  $name
64
     * @return string
65
     */
66
    protected function getPath($name): string
67
    {
68
        return $this->repository->modelPath($this->module) . '/' . $this->getNameInput() . '.php';
69
    }
70
71
    /**
72
     * Build the class with the given name.
73
     *
74
     * @param  string  $name
75
     * @return string
76
     */
77
    protected function buildClass($name)
78
    {
79
        $dummies = [
80
            'DummyClass',
81
            'DummyModuleNamespace',
82
            'DummyModelNamespace',
83
        ];
84
85
        $replacements = [
86
            Entity::getClassName($this->getNameInput()),
87
            $this->repository->getModuleNamespace($this->module),
88
            Entity::convertPathToNamespace($this->getNameInput()),
89
        ];
90
91
        return str_replace($dummies, $replacements, parent::buildClass($name));
92
    }
93
94
    /**
95
     * Get the default namespace for the class.
96
     *
97
     * @param  string  $rootNamespace
98
     * @return string
99
     */
100
    protected function getDefaultNamespace($rootNamespace)
101
    {
102
        return $this->repository->getModuleNamespace($this->module);
103
    }
104
}
105