Completed
Branch master (0bf65c)
by Dmitry
02:31
created

Migrator::migrate()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.0534
cc 4
eloc 13
nc 6
nop 1
crap 4
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 2
    public function registerMigration($class)
14
    {
15 2
        $reflection = new ReflectionClass($class);
16 2
        if (!$reflection->implementsInterface(Contracts\Migration::class)) {
17
            throw new InvalidArgumentException("Register only Migration classes");
18
        }
19 2
        $this->migrations[] = $class;
20 2
    }
21
22 4
    public function migrate(Contracts\Manager $manager)
23
    {
24 4
        if (!$manager->getSchema()->hasSpace('migrations')) {
25 4
            $instance = new Tracker();
26 4
            $instance->migrate($manager);
27 4
            $manager->save($manager->get('migrations')->make(['name' => Migration::class]));
0 ignored issues
show
Unused Code introduced by
The call to Repository::make() has too many arguments starting with array('name' => \Taranto...tions\Migration::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28 4
        }
29
30 4
        $repository = $manager->get('migrations');
31
32 4
        foreach ($this->migrations as $migration) {
33
            $row = [
34
                'name' => $migration
35 2
            ];
36 2
            if (!$repository->find($row, true)) {
37 2
                $instance = new $migration;
38 2
                $instance->migrate($manager);
39 2
                $manager->save($repository->make($row));
0 ignored issues
show
Unused Code introduced by
The call to Repository::make() has too many arguments starting with $row.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40 2
            }
41 4
        }
42 4
    }
43
}
44