Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Commands/ModelMakeCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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()
38
    {
39 9
        parent::handle();
40
41 9
        $this->handleOptionalMigrationOption();
42 9
    }
43
44
    /**
45
     * Create a proper migration name:
46
     * ProductDetail: product_details
47
     * Product: products
48
     * @return string
49
     */
50 3
    private function createMigrationName()
51
    {
52 3
        $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);
53
54 3
        $string = '';
55 3
        foreach ($pieces as $i => $piece) {
56 3
            if ($i+1 < count($pieces)) {
57 1
                $string .= strtolower($piece) . '_';
58
            } else {
59 3
                $string .= Str::plural(strtolower($piece));
60
            }
61
        }
62
63 3
        return $string;
64
    }
65
66
    /**
67
     * Get the console command arguments.
68
     *
69
     * @return array
70
     */
71 120
    protected function getArguments()
72
    {
73
        return [
74 120
            ['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 120
    protected function getOptions()
85
    {
86
        return [
87 120
            ['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 9
    private function handleOptionalMigrationOption()
96
    {
97 9
        if ($this->option('migration') === true) {
98 3
            $migrationName = 'create_' . $this->createMigrationName() . '_table';
99 3
            $this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
100
        }
101 9
    }
102
103
    /**
104
     * @return mixed
105
     */
106 9 View Code Duplication
    protected function getTemplateContents()
107
    {
108 9
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
109
110 9
        return (new Stub('/model.stub', [
111 9
            'NAME'              => $this->getModelName(),
112 9
            'FILLABLE'          => $this->getFillable(),
113 9
            'NAMESPACE'         => $this->getClassNamespace($module),
114 9
            'CLASS'             => $this->getClass(),
115 9
            'LOWER_NAME'        => $module->getLowerName(),
116 9
            'MODULE'            => $this->getModuleName(),
117 9
            'STUDLY_NAME'       => $module->getStudlyName(),
118 9
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
119 9
        ]))->render();
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125 9
    protected function getDestinationFilePath()
126
    {
127 9
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
128
129 9
        $modelPath = GenerateConfigReader::read('model');
130
131 9
        return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php';
132
    }
133
134
    /**
135
     * @return mixed|string
136
     */
137 9
    private function getModelName()
138
    {
139 9
        return Str::studly($this->argument('model'));
0 ignored issues
show
It seems like $this->argument('model') targeting Illuminate\Console\Command::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...
140
    }
141
142
    /**
143
     * @return string
144
     */
145 9
    private function getFillable()
146
    {
147 9
        $fillable = $this->option('fillable');
148
149 9
        if (!is_null($fillable)) {
150 1
            $arrays = explode(',', $fillable);
151
152 1
            return json_encode($arrays);
153
        }
154
155 8
        return '[]';
156
    }
157
158
    /**
159
     * Get default namespace.
160
     *
161
     * @return string
162
     */
163 9
    public function getDefaultNamespace() : string
164
    {
165 9
        $module = $this->laravel['modules'];
166
167 9
        return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Entities');
168
    }
169
}
170