Passed
Push — master ( afe250...86c80a )
by Mr
08:24
created

MigrationList::diff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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 Daikon\DataStructure\TypedListInterface;
12
use Daikon\DataStructure\TypedListTrait;
13
use Daikon\Interop\ToNativeInterface;
14
15
final class MigrationList implements TypedListInterface, ToNativeInterface
16
{
17
    use TypedListTrait;
18
19
    public function __construct(iterable $migrations = [])
20
    {
21
        $this->init($migrations, [MigrationInterface::class]);
22
    }
23
24
    public function exclude(self $migrationList): self
25
    {
26
        return $this->filter(
27
            fn(MigrationInterface $migration): bool => !$migrationList->contains($migration)
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':', expecting T_DOUBLE_ARROW on line 27 at column 45
Loading history...
28
        );
29
    }
30
31
    public function getPendingMigrations(): self
32
    {
33
        return $this->filter(
34
            fn(MigrationInterface $migration): bool => !$migration->hasExecuted()
35
        );
36
    }
37
38
    public function getExecutedMigrations(): self
39
    {
40
        return $this->filter(
41
            fn(MigrationInterface $migration): bool => $migration->hasExecuted()
42
        );
43
    }
44
45
    public function contains(MigrationInterface $migration): bool
46
    {
47
        return $this->reduce(
48
            function (bool $carry, MigrationInterface $item) use ($migration): bool {
49
                return $carry
50
                    || $item->getName() === $migration->getName()
51
                    && $item->getVersion() === $migration->getVersion();
52
            },
53
            false
54
        );
55
    }
56
57
    public function sortByVersion(): self
58
    {
59
        return $this->sort(
60
            fn(MigrationInterface $a, MigrationInterface $b): int => $a->getVersion() - $b->getVersion()
61
        );
62
    }
63
64
    public function findBeforeVersion(int $version = null): self
65
    {
66
        return $this->filter(
67
            fn(MigrationInterface $migration): bool => !$version || $migration->getVersion() <= $version
68
        );
69
    }
70
71
    public function findAfterVersion(int $version = null): self
72
    {
73
        return $this->filter(
74
            fn(MigrationInterface $migration): bool => !$version || $migration->getVersion() > $version
75
        );
76
    }
77
78
    public function toNative()
79
    {
80
        $migrations = [];
81
        foreach ($this as $migration) {
82
            $migrations[] = $migration->toNative();
83
        }
84
        return $migrations;
85
    }
86
}
87