Completed
Push — master ( 6b4c2a...15b2b3 )
by Nicolas
04:36
created

ModelCommand::createMigrationName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
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\Stub;
7
use Nwidart\Modules\Traits\ModuleCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class ModelCommand extends GeneratorCommand
12
{
13
    use ModuleCommandTrait;
14
15
    /**
16
     * The name of argument name.
17
     *
18
     * @var string
19
     */
20
    protected $argumentName = 'model';
21
22
    /**
23
     * The console command name.
24
     *
25
     * @var string
26
     */
27
    protected $name = 'module:make-model';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Generate new model for the specified module.';
35
36 6
    public function fire()
37
    {
38 6
        parent::fire();
39
40 6
        $this->handleOptionalMigrationOption();
41 6
    }
42
43
    /**
44
     * Create a proper migration name:
45
     * ProductDetail: product_details
46
     * Product: products
47
     * @return string
48
     */
49 3
    private function createMigrationName()
50
    {
51 3
        $pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);
52
53 3
        $string = '';
54 3
        foreach ($pieces as $i => $piece) {
55 3
            if ($i+1 < count($pieces)) {
56 1
                $string .= strtolower($piece) . '_';
57 1
            } else {
58 3
                $string .= Str::plural(strtolower($piece));
59
            }
60 3
        }
61
62 3
        return $string;
63
    }
64
65
    /**
66
     * Get the console command arguments.
67
     *
68
     * @return array
69
     */
70 56
    protected function getArguments()
71
    {
72
        return array(
73 56
            array('model', InputArgument::REQUIRED, 'The name of model will be created.'),
74 56
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
75 56
        );
76
    }
77
78
    /**
79
     * Get the console command options.
80
     *
81
     * @return array
82
     */
83 56
    protected function getOptions()
84
    {
85
        return array(
86 56
            array('fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null),
87 56
            array('migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null),
88 56
        );
89
    }
90
91
    /**
92
     * Create the migration file with the given model if migration flag was used
93
     */
94 6
    private function handleOptionalMigrationOption()
95
    {
96 6
        if ($this->option('migration') === true) {
97 3
            $migrationName = 'create_' . $this->createMigrationName() . '_table';
98 3
            $this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
99 3
        }
100 6
    }
101
102
    /**
103
     * @return mixed
104
     */
105 6
    protected function getTemplateContents()
106
    {
107 6
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
108
109 6
        return (new Stub('/model.stub', [
110 6
            'NAME'              => $this->getModelName(),
111 6
            'FILLABLE'          => $this->getFillable(),
112 6
            'NAMESPACE'         => $this->getClassNamespace($module),
113 6
            'CLASS'             => $this->getClass(),
114 6
            'LOWER_NAME'        => $module->getLowerName(),
115 6
            'MODULE'            => $this->getModuleName(),
116 6
            'STUDLY_NAME'       => $module->getStudlyName(),
117 6
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
118 6
        ]))->render();
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124 6
    protected function getDestinationFilePath()
125
    {
126 6
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
127
128 6
        $seederPath = $this->laravel['modules']->config('paths.generator.model');
129
130 6
        return $path . $seederPath . '/' . $this->getModelName() . '.php';
131
    }
132
133
    /**
134
     * @return mixed|string
135
     */
136 6
    private function getModelName()
137
    {
138 6
        return Str::studly($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') targeting Illuminate\Console\Command::argument() can also be of type array; 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...
139
    }
140
141
    /**
142
     * @return string
143
     */
144 6
    private function getFillable()
145
    {
146 6
        $fillable = $this->option('fillable');
147
148 6
        if (!is_null($fillable)) {
149 1
            $arrays = explode(',', $fillable);
150
151 1
            return json_encode($arrays);
152
        }
153
154 5
        return '[]';
155
    }
156
157
    /**
158
     * Get default namespace.
159
     *
160
     * @return string
161
     */
162 6
    public function getDefaultNamespace()
163
    {
164 6
        return $this->laravel['modules']->config('paths.generator.model');
165
    }
166
}
167