Passed
Pull Request — master (#30)
by Mathieu
16:30 queued 06:21
created

Migration::commandInit()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 13
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Console;
6
7
use Suricate\Console;
8
use Suricate\Suricate;
9
10
class Migration
11
{
12
    protected $app;
13
14
    public function __construct(Suricate $app)
15
    {
16
        $this->app = $app;
17
    }
18
19
    public function execute(array $arguments): int
20
    {
21
        $command = $arguments[0] ?? '';
22
        switch ($command) {
23
            case 'migrate':
24
                return $this->commandMigrate();
25
            case 'init':
26
                return $this->commandInit();
27
            case 'list':
28
                return $this->commandList();
29
            case 'create':
30
                return $this->commandCreate();
31
            default:
32
                return $this->commandHelp();
33
        }
34
    }
35
36
    private function commandMigrate()
37
    {
38
        Suricate::Migration()->doMigrations();
39
        return 0;
40
    }
41
42
    private function commandInit(): int
43
    {
44
        $result = Suricate::Migration()->initMigrationTable();
45
        switch ($result) {
46
            case -1:
47
                echo '❌ Migration table already exists!' . "\n";
48
                return 1;
49
            case 0:
50
                echo '✅ Migration table created successfully' . "\n";
51
                return 0;
52
            case 1:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return integer. Consider adding a default case to the switch.
Loading history...
53
                echo '❌ Unsupported database type' . "\n";
54
                return 1;
55
        }
56
    }
57
58
    private function commandList(): int
59
    {
60
        $migrations = Suricate::Migration()->listMigrations();
61
        if (count($migrations) === 0) {
62
            echo "No migration\n";
63
            return 0;
64
        }
65
66
        echo str_repeat("-", 76) . "\n";
67
        foreach ($migrations as $migrationKey=>$migrationDate) {
68
            echo "| " . str_pad(trim($migrationKey), 50, ' ', STR_PAD_RIGHT) . " | " . ($migrationDate !== false ? $migrationDate : str_pad('-', 19, ' ', STR_PAD_BOTH)). " |\n";
69
        }
70
        echo str_repeat("-", 76) . "\n";
71
72
        return 0;
73
    }
74
75
    private function commandCreate(): int
76
    {
77
       $migrationName = Suricate::Migration()->createMigration();
78
       if ($migrationName === false) {
79
            echo "Failed to create migration file\n";
80
            return 1;
81
       }
82
83
        echo "Migration $migrationName created successfully\n";
84
        
85
        return 0;
86
    }
87
    private function commandHelp(): int
88
    {
89
        $str = "Help:" . "\n";
90
        $str .= Console::coloredString('init:', 'green') . "\n";
91
        $str .= "\tInitialize the migrations table\n";
92
        $str .= Console::coloredString('list:', 'green') . "\n";
93
        $str .= "\tList migrations\n";
94
        $str .= Console::coloredString('migrate:', 'green') . "\n";
95
        $str .= "\tExecute pending migrations\n";
96
97
        echo $str;
98
        return 0;
99
    }
100
}
101