Completed
Push — master ( dbd067...624c9b )
by Nicolas
03:00
created

MigrationMakeCommand::getFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\Migrations\NameParser;
7
use Nwidart\Modules\Support\Migrations\SchemaParser;
8
use Nwidart\Modules\Support\Stub;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class MigrationMakeCommand extends GeneratorCommand
14
{
15
    use ModuleCommandTrait;
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'module:make-migration';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create a new migration for the specified module.';
30
31
    /**
32
     * Get the console command arguments.
33
     *
34
     * @return array
35
     */
36 60
    protected function getArguments()
37
    {
38
        return [
39 60
            ['name', InputArgument::REQUIRED, 'The migration name will be created.'],
40
            ['module', InputArgument::OPTIONAL, 'The name of module will be created.'],
41
        ];
42
    }
43
44
    /**
45
     * Get the console command options.
46
     *
47
     * @return array
48
     */
49 60
    protected function getOptions()
50
    {
51
        return [
52 60
            ['fields', null, InputOption::VALUE_OPTIONAL, 'The specified fields table.', null],
53
            ['plain', null, InputOption::VALUE_NONE, 'Create plain migration.'],
54
        ];
55
    }
56
57
    /**
58
     * Get schema parser.
59
     *
60
     * @return SchemaParser
61
     */
62 9
    public function getSchemaParser()
63
    {
64 9
        return new SchemaParser($this->option('fields'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('fields') targeting Illuminate\Console\Command::option() can also be of type array; however, Nwidart\Modules\Support\...maParser::__construct() does only seem to accept string|null, 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...
65
    }
66
67
    /**
68
     * @throws \InvalidArgumentException
69
     *
70
     * @return mixed
71
     */
72 10
    protected function getTemplateContents()
73
    {
74 10
        $parser = new NameParser($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; however, Nwidart\Modules\Support\...meParser::__construct() 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...
75
76 10
        if ($parser->isCreate()) {
77 6
            return Stub::create('/migration/create.stub', [
78 6
                'class' => $this->getClass(),
79 6
                'table' => $parser->getTableName(),
80 6
                'fields' => $this->getSchemaParser()->render(),
81
            ]);
82 4 View Code Duplication
        } elseif ($parser->isAdd()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83 1
            return Stub::create('/migration/add.stub', [
84 1
                'class' => $this->getClass(),
85 1
                'table' => $parser->getTableName(),
86 1
                'fields_up' => $this->getSchemaParser()->up(),
87 1
                'fields_down' => $this->getSchemaParser()->down(),
88
            ]);
89 3
        } elseif ($parser->isDelete()) {
90 1
            return Stub::create('/migration/delete.stub', [
91 1
                'class' => $this->getClass(),
92 1
                'table' => $parser->getTableName(),
93 1
                'fields_down' => $this->getSchemaParser()->up(),
94 1
                'fields_up' => $this->getSchemaParser()->down(),
95
            ]);
96 2 View Code Duplication
        } elseif ($parser->isDrop()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97 1
            return Stub::create('/migration/drop.stub', [
98 1
                'class' => $this->getClass(),
99 1
                'table' => $parser->getTableName(),
100 1
                'fields' => $this->getSchemaParser()->render(),
101
            ]);
102
        }
103
104 1
        return Stub::create('/migration/plain.stub', [
105 1
            'class' => $this->getClass(),
106
        ]);
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112 10
    protected function getDestinationFilePath()
113
    {
114 10
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
115
116 10
        $generatorPath = $this->laravel['modules']->config('paths.generator.migration');
117
118 10
        return $path . $generatorPath . '/' . $this->getFileName() . '.php';
119
    }
120
121
    /**
122
     * @return string
123
     */
124 10
    private function getFileName()
125
    {
126 10
        return date('Y_m_d_His_') . $this->getSchemaName();
127
    }
128
129
    /**
130
     * @return array|string
131
     */
132 10
    private function getSchemaName()
133
    {
134 10
        return $this->argument('name');
135
    }
136
137
    /**
138
     * @return string
139
     */
140 10
    private function getClassName()
141
    {
142 10
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') 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...
143
    }
144
145 10
    public function getClass()
146
    {
147 10
        return $this->getClassName();
148
    }
149
150
    /**
151
     * Run the command.
152
     */
153 10
    public function handle()
154
    {
155 10
        parent::handle();
156
157 10
        if (app()->environment() === 'testing') {
158 10
            return;
159
        }
160
        $this->call('optimize');
161
    }
162
}
163