Passed
Push — master ( f69d70...9ba8f9 )
by Mr
04:23
created

MigrationTarget::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/dbal project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Dbal\Migration;
10
11
use Assert\Assertion;
12
13
final class MigrationTarget implements MigrationTargetInterface
14
{
15
    private string $key;
16
17
    private bool $enabled;
18
19
    private MigrationAdapterInterface $migrationAdapter;
20
21
    private MigrationLoaderInterface $migrationLoader;
22
23
    public function __construct(
24
        string $key,
25
        bool $enabled,
26
        MigrationAdapterInterface $migrationAdapter,
27
        MigrationLoaderInterface $migrationLoader
28
    ) {
29
        $this->key = $key;
30
        $this->enabled = $enabled;
31
        $this->migrationAdapter = $migrationAdapter;
32
        $this->migrationLoader = $migrationLoader;
33
    }
34
35
    public function getKey(): string
36
    {
37
        return $this->key;
38
    }
39
40
    public function isEnabled(): bool
41
    {
42
        return $this->enabled === true;
43
    }
44
45
    public function getMigrationList(): MigrationList
0 ignored issues
show
Bug introduced by
The type Daikon\Dbal\Migration\MigrationList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
    {
47
        $availableMigrations = $this->migrationLoader->load();
48
        $executedMigrations = $this->migrationAdapter->read($this->key);
49
        $pendingMigrations = $availableMigrations->exclude($executedMigrations);
50
        return $executedMigrations->append($pendingMigrations);
51
    }
52
53
    public function migrate(string $direction, int $version = null): MigrationList
54
    {
55
        Assertion::true($this->isEnabled());
56
57
        if ($direction === MigrationInterface::MIGRATE_DOWN) {
58
            $executedMigrations = $this->migrateDown($version);
59
        } else {
60
            $executedMigrations = $this->migrateUp($version);
61
        }
62
63
        return $executedMigrations;
64
    }
65
66
    private function migrateUp(int $version = null): MigrationList
67
    {
68
        $migrationList = $this->getMigrationList();
69
        $executedMigrations = $migrationList->getExecutedMigrations();
70
        $pendingMigrations = $migrationList->getPendingMigrations()->findBeforeVersion($version);
71
72
        $connector = $this->migrationAdapter->getConnector();
73
        foreach ($pendingMigrations as $migration) {
74
            $migration($connector, MigrationInterface::MIGRATE_UP);
75
            $executedMigrations = $executedMigrations->push($migration);
76
            $this->migrationAdapter->write($this->key, $executedMigrations);
77
        }
78
79
        return $pendingMigrations;
80
    }
81
82
    private function migrateDown(int $version = null): MigrationList
83
    {
84
        $migrationList = $this->getMigrationList();
85
        $executedMigrations = $migrationList->getExecutedMigrations();
86
        $reversedMigrations = new MigrationList;
87
88
        $connector = $this->migrationAdapter->getConnector();
89
        foreach ($executedMigrations->findAfterVersion($version)->reverse() as $migration) {
90
            $migration($connector, MigrationInterface::MIGRATE_DOWN);
91
            $reversedMigrations = $reversedMigrations->push($migration);
92
            $this->migrationAdapter->write($this->key, $executedMigrations->exclude($reversedMigrations));
93
        }
94
95
        return $reversedMigrations;
96
    }
97
}
98