Completed
Pull Request — master (#1185)
by
unknown
01:47
created

ModelMakeCommand::handleOptionalFactoryOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
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 9
    public function handle() : int
38
    {
39 9
        if (parent::handle() === E_ERROR) {
40 1
            return E_ERROR;
41
        }
42
43 9
        $this->handleOptionalMigrationOption();
44
        $this->handleOptionalFactoryOption();
45 9
46
        return 0;
47
    }
48
49
    /**
50
     * Create a proper migration name:
51
     * ProductDetail: product_details
52
     * Product: products
53
     * @return string
54 3
     */
55
    private function createMigrationName()
56 3
    {
57
        $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);
58 3
59 3
        $string = '';
60 3
        foreach ($pieces as $i => $piece) {
61 1
            if ($i+1 < count($pieces)) {
62
                $string .= strtolower($piece) . '_';
63 3
            } else {
64
                $string .= Str::plural(strtolower($piece));
65
            }
66
        }
67 3
68
        return $string;
69
    }
70
71
    /**
72
     * Get the console command arguments.
73
     *
74
     * @return array
75 130
     */
76
    protected function getArguments()
77
    {
78 130
        return [
79
            ['model', InputArgument::REQUIRED, 'The name of model will be created.'],
80
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
81
        ];
82
    }
83
84
    /**
85
     * Get the console command options.
86
     *
87
     * @return array
88 130
     */
89
    protected function getOptions()
90
    {
91 130
        return [
92
            ['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null],
93
            ['migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null],
94
            ['factory', 'f', InputOption::VALUE_NONE, 'Flag to create associated factory', null]
95
        ];
96
    }
97
98
    /**
99 9
     * Create the migration file with the given model if migration flag was used
100
     */
101 9
    private function handleOptionalMigrationOption()
102 3
    {
103 3
        if ($this->option('migration') === true) {
104
            $migrationName = 'create_' . $this->createMigrationName() . '_table';
105 9
            $this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
106
        }
107
    }
108
109
    /**
110 9
     * Create the factory with the given model if factory flag was used
111
     */
112 9
    private function handleOptionalFactoryOption()
113
    {
114 9
        if ($this->option('factory') === true) {
115 9
            $this->call('module:make-factory', ['name' => $this->getModelName(), 'module' => $this->argument('module')]);
116 9
        }
117 9
    }
118 9
119 9
    /**
120 9
     * @return mixed
121 9
     */
122 9
    protected function getTemplateContents()
123 9
    {
124
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
125
126
        $stubFile = '/model.stub';
127
        if ($this->option('factory') === true) {
128
            $stubFile = '/model-with-factory.stub';
129 9
        }
130
131 9
        return (new Stub($stubFile, [
132
            'NAME'              => $this->getModelName(),
133 9
            'FILLABLE'          => $this->getFillable(),
134
            'NAMESPACE'         => $this->getClassNamespace($module),
135 9
            'CLASS'             => $this->getClass(),
136
            'LOWER_NAME'        => $module->getLowerName(),
137
            'MODULE'            => $this->getModuleName(),
138
            'STUDLY_NAME'       => $module->getStudlyName(),
139
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
140
        ]))->render();
141 9
    }
142
143 9
    /**
144
     * @return mixed
145
     */
146
    protected function getDestinationFilePath()
147
    {
148
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
149 9
150
        $modelPath = GenerateConfigReader::read('model');
151 9
152
        return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php';
153 9
    }
154 1
155
    /**
156 1
     * @return mixed|string
157
     */
158
    private function getModelName()
159 8
    {
160
        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...
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    private function getFillable()
167 9
    {
168
        $fillable = $this->option('fillable');
169 9
170
        if (!is_null($fillable)) {
171 9
            $arrays = explode(',', $fillable);
172
173
            return json_encode($arrays);
174
        }
175
176
        return '[]';
177
    }
178
179
    /**
180
     * Get default namespace.
181
     *
182
     * @return string
183
     */
184
    public function getDefaultNamespace() : string
185
    {
186
        $module = $this->laravel['modules'];
187
188
        return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Entities');
189
    }
190
}
191