Completed
Pull Request — master (#1088)
by
unknown
02:55
created

ModelMakeCommand::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\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 GeneratorCommand
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 = 'module: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() : int
38
    {
39
        if (parent::handle() === E_ERROR) {
40
            return E_ERROR;
41
        }
42
43
        $this->handleOptionalMigrationOption();
44
45
        return 0;
46
    }
47
48
    /**
49
     * Create a proper migration name:
50
     * ProductDetail: product_details
51
     * Product: products
52
     * @return string
53
     */
54
    private function createMigrationName()
55
    {
56
        $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);
57
58
        $string = '';
59
        foreach ($pieces as $i => $piece) {
60
            if ($i+1 < count($pieces)) {
61
                $string .= strtolower($piece) . '_';
62
            } else {
63
                $string .= Str::plural(strtolower($piece));
64
            }
65
        }
66
67
        return $string;
68
    }
69
70
    /**
71
     * Get the console command arguments.
72
     *
73
     * @return array
74
     */
75 4
    protected function getArguments()
76
    {
77
        return [
78 4
            ['model', InputArgument::REQUIRED, 'The name of model will be created.'],
79
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
80
        ];
81
    }
82
83
    /**
84
     * Get the console command options.
85
     *
86
     * @return array
87
     */
88 4
    protected function getOptions()
89
    {
90
        return [
91 4
            ['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null],
92
            ['migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null],
93
        ];
94
    }
95
96
    /**
97
     * Create the migration file with the given model if migration flag was used
98
     */
99
    private function handleOptionalMigrationOption()
100
    {
101
        if ($this->option('migration') === true) {
102
            $migrationName = 'create_' . $this->createMigrationName() . '_table';
103
            $this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
104
        }
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110 View Code Duplication
    protected function getTemplateContents()
111
    {
112
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
113
114
        return (new Stub('/model.stub', [
115
            'NAME'              => $this->getModelName(),
116
            'FILLABLE'          => $this->getFillable(),
117
            'NAMESPACE'         => $this->getClassNamespace($module),
118
            'CLASS'             => $this->getClass(),
119
            'LOWER_NAME'        => $module->getLowerName(),
120
            'MODULE'            => $this->getModuleName(),
121
            'STUDLY_NAME'       => $module->getStudlyName(),
122
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
123
        ]))->render();
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    protected function getDestinationFilePath()
130
    {
131
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
132
133
        $modelPath = GenerateConfigReader::read('model');
134
135
        return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php';
136
    }
137
138
    /**
139
     * @return mixed|string
140
     */
141
    private function getModelName()
142
    {
143
        return Str::studly($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    private function getFillable()
150
    {
151
        $fillable = $this->option('fillable');
152
153
        if (!is_null($fillable)) {
154
            $arrays = explode(',', $fillable);
155
156
            return json_encode($arrays);
157
        }
158
159
        return '[]';
160
    }
161
162
    /**
163
     * Get default namespace.
164
     *
165
     * @return string
166
     */
167
    public function getDefaultNamespace() : string
168
    {
169
        $module = $this->laravel['modules'];
170
171
        return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Entities');
172
    }
173
}
174