Completed
Push — master ( 0d9d3f...531229 )
by Dmitry
03:18
created

Migrator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 8
c 7
b 1
f 1
lcom 1
cbo 3
dl 0
loc 30
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMigration() 0 8 2
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 41
    public function migrate(Contracts\Manager $manager)
23
    {
24 41
        if (!$manager->getSchema()->hasSpace('migrations')) {
25 41
            $instance = new Tracker();
26 41
            $instance->migrate($manager);
27
        }
28
29 41
        foreach ($this->migrations as $migration) {
30 3
            $name = is_object($migration) ? get_class($migration) : $migration;
31 3
            if (!$manager->get('migrations')->oneByName($name)) {
32 3
                $instance = is_object($migration) ? $migration : new $migration();
33 3
                $instance->migrate($manager);
34 3
                $manager->create('migrations', $name);
35
            }
36
        }
37 41
    }
38
}
39