Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

ExecutedMigrationsSet   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 64
ccs 26
cts 26
cp 1
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getItems() 0 3 1
A getLast() 0 8 3
A getMigration() 0 8 3
A getFirst() 0 7 3
A hasMigration() 0 9 3
A count() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Metadata;
6
7
use Countable;
8
use Doctrine\Migrations\Exception\MigrationNotExecuted;
9
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
10
use Doctrine\Migrations\Version\Version;
11
use function array_values;
12
use function count;
13
14
final class ExecutedMigrationsSet implements Countable
15
{
16
    /** @var ExecutedMigration[] */
17
    private $items = [];
18
19
    /**
20
     * @param ExecutedMigration[] $items
21
     */
22 73
    public function __construct(array $items)
23
    {
24 73
        $this->items = array_values($items);
25 73
    }
26
27
    /**
28
     * @return ExecutedMigration[]
29
     */
30 21
    public function getItems() : array
31
    {
32 21
        return $this->items;
33
    }
34
35 2
    public function getFirst(int $offset = 0) : ExecutedMigration
36
    {
37 2
        if (! isset($this->items[$offset])) {
38 1
            throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? ('+' . $offset) : ''));
39
        }
40
41 1
        return $this->items[$offset];
42
    }
43
44 16
    public function getLast(int $offset = 0) : ExecutedMigration
45
    {
46 16
        $offset = count($this->items) - 1 - (-1 * $offset);
47 16
        if (! isset($this->items[$offset])) {
48 5
            throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? ('+' . $offset) : ''));
49
        }
50
51 11
        return $this->items[$offset];
52
    }
53
54 19
    public function count() : int
55
    {
56 19
        return count($this->items);
57
    }
58
59 39
    public function hasMigration(Version $version) : bool
60
    {
61 39
        foreach ($this->items as $migration) {
62 25
            if ($migration->getVersion()->equals($version)) {
63 24
                return true;
64
            }
65
        }
66
67 32
        return false;
68
    }
69
70 4
    public function getMigration(Version $version) : ExecutedMigration
71
    {
72 4
        foreach ($this->items as $migration) {
73 4
            if ($migration->getVersion()->equals($version)) {
74 3
                return $migration;
75
            }
76
        }
77 1
        throw MigrationNotExecuted::new((string) $version);
78
    }
79
}
80