Completed
Push — master ( cafb47...1e1424 )
by Nicolas
16s queued 11s
created

MigrationMakeCommand::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
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\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Migrations\NameParser;
8
use Nwidart\Modules\Support\Migrations\SchemaParser;
9
use Nwidart\Modules\Support\Stub;
10
use Nwidart\Modules\Traits\ModuleCommandTrait;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputOption;
13
14
class MigrationMakeCommand extends GeneratorCommand
15
{
16
    use ModuleCommandTrait;
17
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'module:make-migration';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new migration for the specified module.';
31
32
    /**
33
     * Get the console command arguments.
34
     *
35
     * @return array
36
     */
37 128
    protected function getArguments()
38
    {
39
        return [
40 128
            ['name', InputArgument::REQUIRED, 'The migration name will be created.'],
41
            ['module', InputArgument::OPTIONAL, 'The name of module will be created.'],
42
        ];
43
    }
44
45
    /**
46
     * Get the console command options.
47
     *
48
     * @return array
49
     */
50 128 View Code Duplication
    protected function getOptions()
51
    {
52
        return [
53 128
            ['fields', null, InputOption::VALUE_OPTIONAL, 'The specified fields table.', null],
54
            ['plain', null, InputOption::VALUE_NONE, 'Create plain migration.'],
55
        ];
56
    }
57
58
    /**
59
     * Get schema parser.
60
     *
61
     * @return SchemaParser
62
     */
63 10
    public function getSchemaParser()
64
    {
65 10
        return new SchemaParser($this->option('fields'));
66
    }
67
68
    /**
69
     * @throws \InvalidArgumentException
70
     *
71
     * @return mixed
72
     */
73 11
    protected function getTemplateContents()
74
    {
75 11
        $parser = new NameParser($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; 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...
76
77 11
        if ($parser->isCreate()) {
78 7
            return Stub::create('/migration/create.stub', [
79 7
                'class' => $this->getClass(),
80 7
                'table' => $parser->getTableName(),
81 7
                'fields' => $this->getSchemaParser()->render(),
82
            ]);
83 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...
84 1
            return Stub::create('/migration/add.stub', [
85 1
                'class' => $this->getClass(),
86 1
                'table' => $parser->getTableName(),
87 1
                'fields_up' => $this->getSchemaParser()->up(),
88 1
                'fields_down' => $this->getSchemaParser()->down(),
89
            ]);
90 3
        } elseif ($parser->isDelete()) {
91 1
            return Stub::create('/migration/delete.stub', [
92 1
                'class' => $this->getClass(),
93 1
                'table' => $parser->getTableName(),
94 1
                'fields_down' => $this->getSchemaParser()->up(),
95 1
                'fields_up' => $this->getSchemaParser()->down(),
96
            ]);
97 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...
98 1
            return Stub::create('/migration/drop.stub', [
99 1
                'class' => $this->getClass(),
100 1
                'table' => $parser->getTableName(),
101 1
                'fields' => $this->getSchemaParser()->render(),
102
            ]);
103
        }
104
105 1
        return Stub::create('/migration/plain.stub', [
106 1
            'class' => $this->getClass(),
107
        ]);
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113 11
    protected function getDestinationFilePath()
114
    {
115 11
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
116
117 11
        $generatorPath = GenerateConfigReader::read('migration');
118
119 11
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
120
    }
121
122
    /**
123
     * @return string
124
     */
125 11
    private function getFileName()
126
    {
127 11
        return date('Y_m_d_His_') . $this->getSchemaName();
128
    }
129
130
    /**
131
     * @return array|string
132
     */
133 11
    private function getSchemaName()
134
    {
135 11
        return $this->argument('name');
136
    }
137
138
    /**
139
     * @return string
140
     */
141 11
    private function getClassName()
142
    {
143 11
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') 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...
144
    }
145
146 11
    public function getClass()
147
    {
148 11
        return $this->getClassName();
149
    }
150
151
    /**
152
     * Run the command.
153
     */
154 11
    public function handle() : int
155
    {
156 11
        if (parent::handle() === E_ERROR) {
157
            return E_ERROR;
158
        }
159
160 11
        if (app()->environment() === 'testing') {
161 11
            return 0;
162
        }
163
164
        return 0;
165
    }
166
}
167