Passed
Push — master ( b3b4c7...2fd2da )
by Arthur
24:27
created

MigrationMakeCommand::stubName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Foundation\Generator\Commands;
4
5
use Foundation\Generator\Abstracts\AbstractGeneratorCommand;
6
use function GuzzleHttp\Psr7\str;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class MigrationMakeCommand extends AbstractGeneratorCommand
11
{
12
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'larapi:make:migration';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new migration for the specified module.';
26
27
    /**
28
     * The name of the generated resource.
29
     *
30
     * @var string
31
     */
32
    protected $generatorName = 'migration';
33
34
    /**
35
     * The file path.
36
     *
37
     * @var string
38
     */
39
    protected $filePath = '/Database/Migrations';
40
41
    protected function stubOptions(): array
42
    {
43
        return [
44
            'CLASS' => $this->getClassName(),
45
            'NAMESPACE' => $this->getClassNamespace(),
46
            "TABLE" => $this->getTableName()
47
        ];
48
    }
49
50
    protected function getTableName(): string
51
    {
52
        return once(function () {
53
            return $this->option('table') ?? $this->ask('What is the name of the table/collection?', strtolower(Str::plural($this->getModuleName())));
54
        });
55
    }
56
57
    /**
58
     * Get the console command options.
59
     *
60
     * @return array
61
     */
62
    protected function getOptions()
63
    {
64
        return [
65
            ['mongo', null, InputOption::VALUE_OPTIONAL, 'Mongo migration.', null],
66
            ['table', null, InputOption::VALUE_OPTIONAL, 'Name of the table/collection.', null],
67
        ];
68
    }
69
70
    protected function isMongoMigration(): bool
71
    {
72
        return once(function () {
73
            $option = $this->option('mongo');
74
            if ($option !== null)
75
                $option = (bool)$option;
76
77
            return $option === null ? $this->confirm('Is this migration for a mongodb database?', false) : $option;
78
        });
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    protected function stubName(): string
85
    {
86
        if ($this->isMongoMigration()) {
87
            return 'migration-mongo.stub';
88
        }
89
90
        return 'migration.stub';
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    protected function getDestinationFilePath()
97
    {
98
        return $this->getModule()->getPath() . $this->filePath . '/' . $this->getDestinationFileName() . '.php';
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    private function getDestinationFileName()
105
    {
106
        return date('Y_m_d_His_') . $this->getSchemaName();
107
    }
108
109
    /**
110
     * @return array|string
111
     */
112
    private function getSchemaName()
113
    {
114
        $schemaName = "";
115
        $splittedInCapsName = $pieces = preg_split('/(?=[A-Z])/', $this->getClassName());
0 ignored issues
show
Unused Code introduced by
The assignment to $pieces is dead and can be removed.
Loading history...
116
        foreach ($splittedInCapsName as $word) {
117
            $schemaName = $schemaName . $word . '_';
118
        }
119
120
        return strtolower(rtrim($schemaName, '_'));
121
    }
122
}
123