Completed
Push — master ( 1793b5...2f89bf )
by Dmitry
03:50
created

Migrator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 9
c 8
b 1
f 1
lcom 1
cbo 3
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMigration() 0 8 2
A getMigrations() 0 4 1
B migrate() 0 16 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 60
    public function migrate(Contracts\Manager $manager)
28
    {
29 60
        if (!$manager->getSchema()->hasSpace('migrations')) {
30 60
            $instance = new Tracker();
31 60
            $instance->migrate($manager);
32
        }
33
34 60
        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
            }
41
        }
42 60
    }
43
}
44