Passed
Push — main ( 5326f7...3bbf58 )
by Thomas
12:47
created

CreateMigrationsTable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 44
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 38 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Quma\Commands;
6
7
use Throwable;
8
9
class CreateMigrationsTable extends Command
10
{
11
    protected string $name = 'create-migrations-table';
12
    protected string $group = 'Migrations';
13
    protected string $description = 'Creates a migrations table';
14
15 10
    public function run(): string|int
16
    {
17 10
        $env = $this->env;
18
19 10
        if ($env->checkIfMigrationsTableExists($env->db)) {
20 6
            echo "Table '{$env->table}' already exists. Aborting\n";
21
22 6
            return 1;
23
        }
24 4
        $ddl = $env->getMigrationsTableDDL();
25
26 4
        if ($ddl) {
27
            try {
28 4
                $env->db->execute($ddl)->run();
29 4
                echo "\033[1;32mSuccess\033[0m: Created table '{$env->table}'\n";
30
31 4
                return 0;
32
                // Would require to create additional errornous DDL or to
33
                // setup a different test database. Too much effort.
34
                // @codeCoverageIgnoreStart
35
            } catch (Throwable $e) {
36
                echo "\033[1;31mError\033[0m: While trying to create table '{$env->table}'\n";
37
                echo $e->getMessage() . PHP_EOL;
38
39
                if ($env->showStacktrace) {
40
                    echo escapeshellarg($e->getTraceAsString()) . PHP_EOL;
41
                }
42
43
                return 1;
44
                // @codeCoverageIgnoreEnd
45
            }
46
        } else {
47
            // Cannot be reliably tested.
48
            // Would require an unsupported driver to be installed.
49
            // @codeCoverageIgnoreStart
50
            echo "PDO driver '{$env->driver}' not supported. Aborting\n";
51
52
            return 1;
53
            // @codeCoverageIgnoreEnd
54
        }
55
    }
56
}
57