AvailableMigrationsList::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
/**
15
 * Represents a sorted list of migrations that may or maybe not be already executed.
16
 */
17 View Code Duplication
final class AvailableMigrationsList implements Countable
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /** @var AvailableMigration[] */
20
    private $items = [];
21
22
    /**
23
     * @param AvailableMigration[] $items
24
     */
25 89
    public function __construct(array $items)
26
    {
27 89
        $this->items = array_values($items);
28 89
    }
29
30
    /**
31
     * @return AvailableMigration[]
32
     */
33 51
    public function getItems() : array
34
    {
35 51
        return $this->items;
36
    }
37
38 11
    public function getFirst(int $offset = 0) : AvailableMigration
39
    {
40 11
        if (! isset($this->items[$offset])) {
41 6
            throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : ''));
42
        }
43
44 5
        return $this->items[$offset];
45
    }
46
47 18
    public function getLast(int $offset = 0) : AvailableMigration
48
    {
49 18
        $offset = count($this->items) - 1 - (-1 * $offset);
50 18
        if (! isset($this->items[$offset])) {
51 2
            throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : ''));
52
        }
53
54 16
        return $this->items[$offset];
55
    }
56
57 13
    public function count() : int
58
    {
59 13
        return count($this->items);
60
    }
61
62 25
    public function hasMigration(Version $version) : bool
63
    {
64 25
        foreach ($this->items as $migration) {
65 22
            if ($migration->getVersion()->equals($version)) {
66 12
                return true;
67
            }
68
        }
69
70 17
        return false;
71
    }
72
73 7
    public function getMigration(Version $version) : AvailableMigration
74
    {
75 7
        foreach ($this->items as $migration) {
76 7
            if ($migration->getVersion()->equals($version)) {
77 6
                return $migration;
78
            }
79
        }
80
81 1
        throw MigrationNotAvailable::forVersion($version);
82
    }
83
}
84