Completed
Push — master ( 5b35ba...648bc7 )
by Dmitry
02:58
created

Migrator::getMigrations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Tarantool\Mapper\Migrations;
4
5
use Tarantool\Mapper\Contracts;
6
use InvalidArgumentException;
7
use ReflectionClass;
8
9
class Migrator implements Contracts\Migration
10
{
11
    protected $migrations = [];
12
13 1
    public function registerMigration($class)
14
    {
15 1
        $reflection = new ReflectionClass($class);
16 1
        if (!$reflection->implementsInterface(Contracts\Migration::class)) {
17 1
            throw new InvalidArgumentException('Register only Migration classes');
18
        }
19
        $this->migrations[] = $class;
20
    }
21
22
    public function getMigrations()
23
    {
24
        return $this->migrations;
25
    }
26
27
    public function migrate(Contracts\Manager $manager)
28
    {
29
        if (!$manager->getSchema()->hasSpace('migrations')) {
30
            $instance = new Tracker();
31
            $instance->migrate($manager);
32
        }
33
34
        foreach ($this->migrations as $migration) {
35
            $name = is_object($migration) ? get_class($migration) : $migration;
36
            if (!$manager->get('migrations')->oneByName($name)) {
37
                $instance = is_object($migration) ? $migration : new $migration();
38
                $instance->migrate($manager);
39
                $manager->create('migrations', $name);
40
            }
41
        }
42
    }
43
}
44