Test Failed
Push — master ( 06ba11...288ec3 )
by Mr
01:37
created

MigrationList::sortByVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Daikon\Dbal\Migration;
4
5
use Daikon\DataStructure\TypedListTrait;
6
7
final class MigrationList implements \IteratorAggregate, \Countable
8
{
9
    use TypedListTrait;
10
11
    public function __construct(iterable $migrations = [])
12
    {
13
        $this->init($migrations, MigrationInterface::class);
14
    }
15
16
    public function diff(self $migrationList)
17
    {
18
        return new self(
19
            $this->compositeVector->filter(function ($migration) use ($migrationList) {
20
                return !$migrationList->containsVersion($migration->getVersion());
21
            })
22
        );
23
    }
24
25
    public function merge(self $migrationList): self
26
    {
27
        return new self($this->compositeVector->merge($migrationList));
28
    }
29
30
    public function getPendingMigrations()
31
    {
32
        return new self(
33
            $this->compositeVector->filter(function ($migration) {
34
                return !$migration->hasExecuted();
35
            })
36
        );
37
    }
38
39
    public function getExecutedMigrations()
40
    {
41
        return new self(
42
            $this->compositeVector->filter(function ($migration) {
43
                return $migration->hasExecuted();
44
            })
45
        );
46
    }
47
48
    public function toArray(): array
49
    {
50
        $migrations = [];
51
        foreach ($this as $migration) {
52
            $migrations[] = $migration->toArray();
53
        }
54
        return $migrations;
55
    }
56
57
    public function containsVersion(int $version): bool
58
    {
59
        return $this->compositeVector->filter(function ($migration) use ($version) {
60
            return $migration->getVersion() === $version;
61
        })->count() === 1;
62
    }
63
64
    public function sortByVersion()
65
    {
66
        $copy = clone $this;
67
        $copy->compositeVector->sort(function ($a, $b) {
68
            return $a->getVersion() - $b->getVersion();
69
        });
70
        return $copy;
71
    }
72
}
73