Passed
Pull Request — master (#30)
by Mathieu
17:27 queued 07:27
created

Migration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 90
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A commandMigrate() 0 4 1
A execute() 0 14 5
A commandInit() 0 15 4
A commandHelp() 0 12 1
A commandList() 0 15 4
A commandCreate() 0 11 2
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:
53
                echo '❌ Unsupported database type' . "\n";
54
                return 1;
55
        }
56
        return 1;
57
    }
58
59
    private function commandList(): int
60
    {
61
        $migrations = Suricate::Migration()->listMigrations();
62
        if (count($migrations) === 0) {
63
            echo "No migration\n";
64
            return 0;
65
        }
66
67
        echo str_repeat("-", 76) . "\n";
68
        foreach ($migrations as $migrationKey=>$migrationDate) {
69
            echo "| " . str_pad(trim($migrationKey), 50, ' ', STR_PAD_RIGHT) . " | " . ($migrationDate !== false ? $migrationDate : str_pad('-', 19, ' ', STR_PAD_BOTH)). " |\n";
70
        }
71
        echo str_repeat("-", 76) . "\n";
72
73
        return 0;
74
    }
75
76
    private function commandCreate(): int
77
    {
78
       $migrationName = Suricate::Migration()->createMigration();
79
       if ($migrationName === false) {
80
            echo "Failed to create migration file\n";
81
            return 1;
82
       }
83
84
        echo "Migration $migrationName created successfully\n";
85
        
86
        return 0;
87
    }
88
    private function commandHelp(): int
89
    {
90
        $str = "Help:" . "\n";
91
        $str .= Console::coloredString('init:', 'green') . "\n";
92
        $str .= "\tInitialize the migrations table\n";
93
        $str .= Console::coloredString('list:', 'green') . "\n";
94
        $str .= "\tList migrations\n";
95
        $str .= Console::coloredString('migrate:', 'green') . "\n";
96
        $str .= "\tExecute pending migrations\n";
97
98
        echo $str;
99
        return 0;
100
    }
101
}
102