ModelMakeCommand::getDefaultNamespace()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Stub;
8
use Rawilk\LaravelModules\Traits\ModuleCommands;
9
10
class ModelMakeCommand extends GeneratorCommand
11
{
12
    use ModuleCommands;
13
14
    /** @var string */
15
    protected $argumentName = 'model';
16
17
    /** @var string */
18
    protected $signature = 'module:make-model
19
                            {model : The name of the model}
20
                            {module? : The name of the module to create the model for}
21
                            {--fillable= : The fillable attributes}
22
                            {--base_class= : Override the default base model class (from config)}
23
                            {--table= : The name of the database table}
24
                            {--m|migration : Create a migration for the model as well}';
25
26
    /** @var string */
27
    protected $description = 'Create a new model for the specified module.';
28
29
    public function handle(): void
30
    {
31
        parent::handle();
32
33
        $this->handleOptionalMigration();
34
    }
35
36
    protected function getDefaultNamespace(): string
37
    {
38
        /** @var \Rawilk\LaravelModules\Contracts\Repository $module */
39
        $module = $this->laravel['modules'];
40
41
        return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Models');
42
    }
43
44
    protected function getDestinationFilePath(): string
45
    {
46
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
47
48
        $modelPath = GenerateConfigReader::read('model');
49
50
        return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php';
51
    }
52
53
    protected function getTemplateContents(): string
54
    {
55
        /** @var \Rawilk\LaravelModules\Module $module */
56
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
57
58
        return (new Stub('/model.stub', [
59
            'FILLABLE'         => $this->getFillable(),
60
            'NAME'             => $this->getModelName(),
61
            'NAMESPACE'        => $this->getClassNamespace($module),
62
            'CLASS'            => $this->getClass(),
63
            'LOWER_NAME'       => $module->getLowerName(),
64
            'STUDLY_NAME'      => $module->getStudlyName(),
65
            'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'),
66
            'TABLE'            => $this->createMigrationName(),
67
            'BASE_CLASS'       => $this->getBaseClass('model'),
68
            'BASE_CLASS_SHORT' => $this->getBaseClass('model', true),
69
        ]))->render();
70
    }
71
72
    private function createMigrationName(): string
73
    {
74
        if ($table = $this->option('table')) {
75
            return $table;
76
        }
77
78
        $pieces = preg_split(
79
            '/(?=[A-Z])/',
80
            class_basename($this->argument('model')),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            class_basename(/** @scrutinizer ignore-type */ $this->argument('model')),
Loading history...
81
            -1,
82
            PREG_SPLIT_NO_EMPTY
83
        );
84
85
        $name = '';
86
        $count = count($pieces);
0 ignored issues
show
Bug introduced by
It seems like $pieces can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $count = count(/** @scrutinizer ignore-type */ $pieces);
Loading history...
87
88
        foreach ($pieces as $index => $piece) {
89
            if ($index + 1 < $count) {
90
                $name .= strtolower($piece) . '_';
91
            } else {
92
                $name .= Str::plural(strtolower($piece));
93
            }
94
        }
95
96
        return $name;
97
    }
98
99
    private function getFillable(): string
100
    {
101
        $fillable = $this->option('fillable');
102
103
        if ($fillable !== null) {
0 ignored issues
show
introduced by
The condition $fillable !== null is always true.
Loading history...
104
            return str_replace(['"', ','], ["'", ', '], json_encode(explode(',', $fillable)));
105
        }
106
107
        return '[]';
108
    }
109
110
    private function getModelName(): string
111
    {
112
        return Str::studly($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument($this->argumentName));
Loading history...
113
    }
114
115
    private function handleOptionalMigration(): void
116
    {
117
        if ($this->option('migration')) {
118
            $migrationName = 'create_' . $this->createMigrationName() . '_table';
119
120
            $this->call('module:make-migration', [
121
                'name'   => $migrationName,
122
                'module' => $this->argument('module')
123
            ]);
124
        }
125
    }
126
}
127