Completed
Push — master ( c1fc0d...a7d98d )
by Dmitry
28:31 queued 26:01
created

Migrator::migrate()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

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