Passed
Push — master ( 139939...b4b8e8 )
by Arthur
21:54 queued 17s
created

ModelMakeCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A handleOptionalMigrationOption() 0 5 2
A getFillable() 0 11 2
A getTemplateContents() 0 14 1
A createMigrationName() 0 14 3
A getModelName() 0 3 1
A getArguments() 0 5 1
A getDefaultNamespace() 0 3 1
A handle() 0 5 1
A getDestinationFilePath() 0 7 1
A getOptions() 0 5 1
1
<?php
2
3
namespace Foundation\Generator\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ModelMakeCommand extends \Nwidart\Modules\Commands\ModelMakeCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The name of argument name.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'model';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'larapi:make-model';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Create a new model for the specified module.';
36
37
    public function handle()
38
    {
39
        parent::handle();
40
41
        $this->handleOptionalMigrationOption();
42
    }
43
44
    /**
45
     * Create a proper migration name:
46
     * ProductDetail: product_details
47
     * Product: products
48
     * @return string
49
     */
50
    private function createMigrationName()
51
    {
52
        $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') can also be of type string[]; however, parameter $subject of preg_split() 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

52
        $pieces = preg_split('/(?=[A-Z])/', /** @scrutinizer ignore-type */ $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);
Loading history...
53
54
        $string = '';
55
        foreach ($pieces as $i => $piece) {
56
            if ($i+1 < count($pieces)) {
57
                $string .= strtolower($piece) . '_';
58
            } else {
59
                $string .= Str::plural(strtolower($piece));
60
            }
61
        }
62
63
        return $string;
64
    }
65
66
    /**
67
     * Get the console command arguments.
68
     *
69
     * @return array
70
     */
71
    protected function getArguments()
72
    {
73
        return [
74
            ['model', InputArgument::REQUIRED, 'The name of model will be created.'],
75
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
76
        ];
77
    }
78
79
    /**
80
     * Get the console command options.
81
     *
82
     * @return array
83
     */
84
    protected function getOptions()
85
    {
86
        return [
87
            ['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null],
88
            ['migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null],
89
        ];
90
    }
91
92
    /**
93
     * Create the migration file with the given model if migration flag was used
94
     */
95
    private function handleOptionalMigrationOption()
96
    {
97
        if ($this->option('migration') === true) {
98
            $migrationName = 'create_' . $this->createMigrationName() . '_table';
99
            $this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
100
        }
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106
    protected function getTemplateContents()
107
    {
108
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
109
110
        return (new Stub('/model.stub', [
111
            'NAME'              => $this->getModelName(),
112
            'FILLABLE'          => $this->getFillable(),
113
            'NAMESPACE'         => $this->getClassNamespace($module),
114
            'CLASS'             => $this->getClass(),
115
            'LOWER_NAME'        => $module->getLowerName(),
116
            'MODULE'            => $this->getModuleName(),
117
            'STUDLY_NAME'       => $module->getStudlyName(),
118
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
119
        ]))->render();
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    protected function getDestinationFilePath()
126
    {
127
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
128
129
        $modelPath = GenerateConfigReader::read('model');
130
131
        return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php';
132
    }
133
134
    /**
135
     * @return mixed|string
136
     */
137
    private function getModelName()
138
    {
139
        return Str::studly($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') 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

139
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('model'));
Loading history...
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    private function getFillable()
146
    {
147
        $fillable = $this->option('fillable');
148
149
        if (!is_null($fillable)) {
150
            $arrays = explode(',', $fillable);
0 ignored issues
show
Bug introduced by
It seems like $fillable can also be of type string[]; however, parameter $string of explode() 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

150
            $arrays = explode(',', /** @scrutinizer ignore-type */ $fillable);
Loading history...
151
152
            return json_encode($arrays);
153
        }
154
155
        return '[]';
156
    }
157
158
    /**
159
     * Get default namespace.
160
     *
161
     * @return string
162
     */
163
    public function getDefaultNamespace() : string
164
    {
165
        return $this->laravel['modules']->config('paths.generator.model.path', 'Entities');
166
    }
167
}
168