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

AvailableMigrationsList   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 65
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 65
ccs 26
cts 26
cp 1
rs 10
wmc 15

7 Methods

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