Completed
Push — master ( d691a6...bc5f12 )
by Nicolas
05:00
created

ModelCommand::handleOptionalMigrationOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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 5
    public function fire()
37
    {
38 5
        parent::fire();
39
40 5
        $this->handleOptionalMigrationOption();
41 5
    }
42
43
    /**
44
     * Get the console command arguments.
45
     *
46
     * @return array
47
     */
48 51
    protected function getArguments()
49
    {
50
        return array(
51 51
            array('model', InputArgument::REQUIRED, 'The name of model will be created.'),
52
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
53
        );
54
    }
55
56
    /**
57
     * Get the console command options.
58
     *
59
     * @return array
60
     */
61 51
    protected function getOptions()
62
    {
63
        return array(
64 51
            array('fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null),
65
            array('migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null),
66
        );
67
    }
68
69
    /**
70
     * Create the migration file with the given model if migration flag was used
71
     */
72 5
    private function handleOptionalMigrationOption()
73
    {
74 5
        if ($this->option('migration') === true) {
75 2
            $migrationName = 'create_' . strtolower($this->argument('model')) . '_table';
76 2
            $this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
77
        }
78 5
    }
79
80
    /**
81
     * @return mixed
82
     */
83 5
    protected function getTemplateContents()
84
    {
85 5
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
86
87 5
        return (new Stub('/model.stub', [
88 5
            'NAME'              => $this->getModelName(),
89 5
            'FILLABLE'          => $this->getFillable(),
90 5
            'NAMESPACE'         => $this->getClassNamespace($module),
91 5
            'CLASS'             => $this->getClass(),
92 5
            'LOWER_NAME'        => $module->getLowerName(),
93 5
            'MODULE'            => $this->getModuleName(),
94 5
            'STUDLY_NAME'       => $module->getStudlyName(),
95 5
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
96 5
        ]))->render();
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102 5
    protected function getDestinationFilePath()
103
    {
104 5
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
105
106 5
        $seederPath = $this->laravel['modules']->config('paths.generator.model');
107
108 5
        return $path . $seederPath . '/' . $this->getModelName() . '.php';
109
    }
110
111
    /**
112
     * @return mixed|string
113
     */
114 5
    private function getModelName()
115
    {
116 5
        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...
117
    }
118
119
    /**
120
     * @return string
121
     */
122 5
    private function getFillable()
123
    {
124 5
        $fillable = $this->option('fillable');
125
126 5
        if (!is_null($fillable)) {
127 1
            $arrays = explode(',', $fillable);
128
129 1
            return json_encode($arrays);
130
        }
131
132 4
        return '[]';
133
    }
134
135
    /**
136
     * Get default namespace.
137
     *
138
     * @return string
139
     */
140 5
    public function getDefaultNamespace()
141
    {
142 5
        return $this->laravel['modules']->config('paths.generator.model');
143
    }
144
}
145