Migrate::setMigrations()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 12
nop 1
dl 0
loc 24
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Framy Framework
4
 *
5
 * @copyright Copyright Framy
6
 * @Author  Marco Bier <[email protected]>
7
 */
8
9
namespace app\framework\Component\Database\Commands;
10
11
use app\framework\Component\Console\Command\Command;
12
use app\framework\Component\Console\Input\InputArgument;
13
use app\framework\Component\Console\Input\InputDefinition;
14
use app\framework\Component\Console\Input\InputInterface;
15
use app\framework\Component\Console\Output\ConsoleOutput;
16
use app\framework\Component\Database\Migrations\Migration;
17
use app\framework\Component\StdLib\StdObject\ArrayObject\ArrayObject;
18
19
class Migrate extends Command
20
{
21
    private $namespace = "app\\custom\\Database\\migrations\\";
22
23
    /**
24
     * @var Migration[] $migrations
25
     */
26
    private $migrations = [];
27
28
    protected function configure()
29
    {
30
        $this->setName("migrate")
31
            ->setDescription("Migrate your database.")
32
            ->setDefinition(new InputDefinition([
33
                new InputArgument("migration", InputArgument::OPTIONAL, "Specify a single migration")
34
            ]));
35
    }
36
37
    protected function execute(InputInterface $input, ConsoleOutput $output)
38
    {
39
        $migration = $input->getArgument("migration");
40
41
        if (isset($migration)) {
42
            $this->setMigration($migration, $output);
43
        } else {
44
            $this->setMigrations($output);
45
        }
46
47
        foreach ($this->migrations as $class) {
48
            $output->writeln("<info>Migrating: ".get_class($class)."</info>");
49
            $output->writeln("<comment>Running down!</comment>");
50
            $class->down();
51
            $output->writeln("<comment>Running up!</comment>");
52
            $class->up();
53
        }
54
55
        if (count($this->migrations) > 0)
56
            $output->writeln("Migration successful");
57
    }
58
59
    private function setMigrations(ConsoleOutput $output)
60
    {
61
        // get all migrations
62
        $classes   = [];
63
        $namespace = $this->namespace;
64
65
        $path = str_replace("\\","/", $namespace);
66
        $filesInNamespace = new ArrayObject(scandir(ROOT_PATH."/".$path."/"));
67
        $filesInNamespace->removeFirst()->removeFirst();
68
69
        for ($i = 0; $i <= $filesInNamespace->count()-1; $i++)
70
            $filesInNamespace->key($i, explode(".php", $filesInNamespace->key($i))[0]);
71
72
        foreach ($filesInNamespace->val() as $value) {
73
            $class = $namespace.$value;
74
            if(class_exists($class) && is_subclass_of($class,'app\framework\Component\Database\Migrations\Migration'))
75
                $classes[$value] = new $class();
76
        }
77
78
        // tell user so if there are no migrations
79
        if($classes == []) {
80
            $output->writeln("No migrations found! You can Create migrations via the make:migration command.");
81
        } else {
82
            $this->migrations = $classes;
83
        }
84
    }
85
86
    private function setMigration($migration, ConsoleOutput $output)
87
    {
88
        $migrationInstance = null;
89
        $class = $this->namespace.$migration;
90
        if(class_exists($class) && is_subclass_of($class,'app\framework\Component\Database\Migrations\Migration'))
91
            $migrationInstance = new $class();
92
93
        if($migrationInstance == null) {
94
            $output->writeln("Migration not found");
95
        } else {
96
            // to make sure that only one element is in array
97
            $this->migrations = [];
98
            $this->migrations[] = $migrationInstance;
99
        }
100
    }
101
}
102