Passed
Push — master ( 139939...b4b8e8 )
by Arthur
21:54 queued 17s
created

MigrationMakeCommand::getDestinationFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Foundation\Generator\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 \Nwidart\Modules\Commands\MigrationMakeCommand
15
{
16
    use ModuleCommandTrait;
17
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'larapi: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
    protected function getArguments()
38
    {
39
        return [
40
            ['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
    protected function getOptions()
51
    {
52
        return [
53
            ['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
    public function getSchemaParser()
64
    {
65
        return new SchemaParser($this->option('fields'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('fields') can also be of type string[]; however, parameter $schema of Nwidart\Modules\Support\...maParser::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        return new SchemaParser(/** @scrutinizer ignore-type */ $this->option('fields'));
Loading history...
66
    }
67
68
    /**
69
     * @throws \InvalidArgumentException
70
     *
71
     * @return mixed
72
     */
73
    protected function getTemplateContents()
74
    {
75
        $parser = new NameParser($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $name of Nwidart\Modules\Support\...meParser::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $parser = new NameParser(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
76
77
        if ($parser->isCreate()) {
78
            return Stub::create('/migration/create.stub', [
79
                'class' => $this->getClass(),
80
                'table' => $parser->getTableName(),
81
                'fields' => $this->getSchemaParser()->render(),
82
            ]);
83
        } elseif ($parser->isAdd()) {
84
            return Stub::create('/migration/add.stub', [
85
                'class' => $this->getClass(),
86
                'table' => $parser->getTableName(),
87
                'fields_up' => $this->getSchemaParser()->up(),
88
                'fields_down' => $this->getSchemaParser()->down(),
89
            ]);
90
        } elseif ($parser->isDelete()) {
91
            return Stub::create('/migration/delete.stub', [
92
                'class' => $this->getClass(),
93
                'table' => $parser->getTableName(),
94
                'fields_down' => $this->getSchemaParser()->up(),
95
                'fields_up' => $this->getSchemaParser()->down(),
96
            ]);
97
        } elseif ($parser->isDrop()) {
98
            return Stub::create('/migration/drop.stub', [
99
                'class' => $this->getClass(),
100
                'table' => $parser->getTableName(),
101
                'fields' => $this->getSchemaParser()->render(),
102
            ]);
103
        }
104
105
        return Stub::create('/migration/plain.stub', [
106
            'class' => $this->getClass(),
107
        ]);
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113
    protected function getDestinationFilePath()
114
    {
115
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
116
117
        $generatorPath = GenerateConfigReader::read('migration');
118
119
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    private function getFileName()
126
    {
127
        return date('Y_m_d_His_') . $this->getSchemaName();
0 ignored issues
show
Bug introduced by
Are you sure $this->getSchemaName() of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
        return date('Y_m_d_His_') . /** @scrutinizer ignore-type */ $this->getSchemaName();
Loading history...
128
    }
129
130
    /**
131
     * @return array|string
132
     */
133
    private function getSchemaName()
134
    {
135
        return $this->argument('name');
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    private function getClassName()
142
    {
143
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
144
    }
145
146
    public function getClass()
147
    {
148
        return $this->getClassName();
149
    }
150
151
    /**
152
     * Run the command.
153
     */
154
    public function handle()
155
    {
156
        parent::handle();
157
158
        if (app()->environment() === 'testing') {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        if (app()->/** @scrutinizer ignore-call */ environment() === 'testing') {
Loading history...
159
            return;
160
        }
161
    }
162
}
163