Passed
Pull Request — master (#75)
by Korotkov
12:47
created

CreateMigrationCommand::createSqliteMigration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ship\Command;
4
5
use App\Ship\Utils\FileCreator;
6
use Rudra\Container\Facades\Rudra;
7
use Rudra\Cli\ConsoleFacade as Cli;
8
9
class CreateMigrationCommand extends FileCreator
10
{
11
    /**
12
     * Creates a file with Migration data
13
     * -----------------------------
14
     * Создает файл с данными Migration
15
     */
16
    public function actionIndex(): void
17
    {
18
        Cli::printer("Enter table name: ", "magneta");
19
        $table = str_replace(PHP_EOL, "", Cli::reader());
20
21
        Cli::printer("Enter container (empty for Ship): ", "magneta");
22
        $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
23
24
        $date      = date("_dmYHis");
25
        $className = ucfirst($table) . $date;
26
27
        if (!empty($container)) {
28
29
            $namespace = 'App\Containers\\' . $container . '\Migration';
30
31
            if (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "mysql") {
32
                $this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migration/"), "{$className}_migration.php"],
33
                    $this->createMysqlMigration($className, $table, $namespace)
34
                );
35
            } elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "pgsql") {
36
                $this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migration/"), "{$className}_migration.php"],
37
                    $this->createPgsqlMigration($className, $table, $namespace)
38
                );
39
            } elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "sqlite") {
40
                $this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migration/"), "{$className}_migration.php"],
41
                    $this->createSqliteMigration($className, $table, $namespace)
42
                );
43
            }
44
45
        } else {
46
47
            $namespace = "App\Ship\Migration";
48
49
            if (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "mysql") {
50
                $this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Ship/Migration/"), "{$className}_migration.php"],
51
                    $this->createMysqlMigration($className, $table, $namespace)
52
                );
53
            } elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "pgsql") {
54
                $this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Ship/Migration/"), "{$className}_migration.php"],
55
                    $this->createPgsqlMigration($className, $table, $namespace)
56
                );
57
            } elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "sqlite") {
58
                $this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Ship/Migration/"), "{$className}_migration.php"],
59
                    $this->createSqliteMigration($className, $table, $namespace)
60
                );
61
            }
62
        }
63
    }
64
65
    /**
66
     * Creates class data
67
     * ------------------
68
     * Создает данные класса
69
     *
70
     * @param string $className
71
     * @param string $table
72
     * @param string $namespace
73
     * @return string
74
     */
75
    private function createMysqlMigration(string $className, string $table, string $namespace): string
76
    {
77
        return <<<EOT
78
<?php
79
80
namespace $namespace;
81
82
use Rudra\Container\Facades\Rudra;
83
84
class {$className}_migration
85
{
86
    public function up(): void
87
    {
88
        \$table = "$table";
89
90
        \$query = Rudra::get("DSN")->prepare("
91
            CREATE TABLE {\$table} (
92
            `id` INT NOT NULL AUTO_INCREMENT ,
93
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
94
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
95
                PRIMARY KEY (`id`)
96
            ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci
97
        ");
98
99
        \$query->execute();
100
    }
101
}\r\n
102
EOT;
103
    }
104
105
    /**
106
     * Creates class data
107
     * ------------------
108
     * Создает данные класса
109
     *
110
     * @param string $className
111
     * @param string $table
112
     * @param string $namespace
113
     * @return string
114
     */
115
    private function createPgsqlMigration(string $className, string $table, string $namespace): string
116
    {
117
        return <<<EOT
118
<?php
119
120
namespace $namespace;
121
122
use Rudra\Container\Facades\Rudra;
123
124
class {$className}_migration
125
{
126
    public function up(): void
127
    {
128
        \$table = "$table";
129
130
        \$query = Rudra::get("DSN")->prepare("
131
132
            CREATE TABLE {\$table} (
133
                id serial PRIMARY KEY,
134
                created_at TIMESTAMP NOT NULL,
135
                updated_at TIMESTAMP NOT NULL
136
            );
137
        ");
138
139
        \$query->execute();
140
    }
141
}\r\n
142
EOT;
143
    }
144
145
    /**
146
     * Creates class data
147
     * ------------------
148
     * Создает данные класса
149
     *
150
     * @param string $className
151
     * @param string $table
152
     * @param string $namespace
153
     * @return string
154
     */
155
    private function createSqliteMigration(string $className, string $table, string $namespace): string
156
    {
157
        return <<<EOT
158
<?php
159
160
namespace $namespace;
161
162
use Rudra\Container\Facades\Rudra;
163
164
class {$className}_migration
165
{
166
    public function up(): void
167
    {
168
        \$table = "$table";
169
170
        \$query = Rudra::get("DSN")->prepare("
171
172
            CREATE TABLE IF NOT EXISTS {\$table} (
173
                id INTEGER PRIMARY KEY,
174
                created_at TEXT NOT NULL,
175
                updated_at TEXT NOT NULL
176
            );
177
        ");
178
179
        \$query->execute();
180
    }
181
}\r\n
182
EOT;
183
    }
184
}
185