Passed
Push — master ( 705452...770324 )
by Mathieu
35:33 queued 25:29
created

Migration::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 12
rs 9.9332
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 'list':
26
                return $this->commandList();
27
            case 'create':
28
                return $this->commandCreate();
29
            default:
30
                return $this->commandHelp();
31
        }
32
    }
33
34
    private function commandMigrate()
35
    {
36
        Suricate::Migration()->doMigrations();
37
        return 0;
38
    }
39
40
    private function commandList(): int
41
    {
42
        $migrations = Suricate::Migration()->listMigrations();
43
        if (count($migrations) === 0) {
44
            echo "No migration\n";
45
            return 0;
46
        }
47
        
48
        foreach ($migrations as $configName => $currentConfigMigrations) {
49
            echo str_repeat("-", 13+strlen($configName)) . "\n";
50
            echo "| Config : $configName |\n";
51
            echo str_repeat("-", 76) . "\n";
52
            if (count($currentConfigMigrations) === 0) {
53
                echo "|  🎉 no migration\n";
54
            } else {
55
                foreach ($currentConfigMigrations as $migrationKey=>$migrationDate) {
56
                    echo "| " . str_pad(trim($migrationKey), 50, ' ', STR_PAD_RIGHT) . " | " . ($migrationDate !== false ? $migrationDate : str_pad('-', 19, ' ', STR_PAD_BOTH)). " |\n";
57
                }
58
            }
59
            echo str_repeat("-", 76) . "\n\n";
60
        }
61
62
        return 0;
63
    }
64
65
    private function commandCreate(): int
66
    {
67
       $migrationName = Suricate::Migration()->createMigration();
68
       if ($migrationName === false) {
69
            echo "Failed to create migration file\n";
70
            return 1;
71
       }
72
73
        echo "Migration $migrationName created successfully\n";
74
        
75
        return 0;
76
    }
77
    private function commandHelp(): int
78
    {
79
        $str = "Help:" . "\n";
80
        $str .= Console::coloredString('init:', 'green') . "\n";
81
        $str .= "\tInitialize the migrations table\n";
82
        $str .= Console::coloredString('list:', 'green') . "\n";
83
        $str .= "\tList migrations\n";
84
        $str .= Console::coloredString('migrate:', 'green') . "\n";
85
        $str .= "\tExecute pending migrations\n";
86
87
        echo $str;
88
        return 0;
89
    }
90
}
91