ExecutedMigrationsList::getItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\MigrationNotExecuted;
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 executed migrations.
16
 * The migrations in this set might be not available anymore.
17
 */
18 View Code Duplication
final class ExecutedMigrationsList 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...
19
{
20
    /** @var ExecutedMigration[] */
21
    private $items = [];
22
23
    /**
24
     * @param ExecutedMigration[] $items
25
     */
26 87
    public function __construct(array $items)
27
    {
28 87
        $this->items = array_values($items);
29 87
    }
30
31
    /**
32
     * @return ExecutedMigration[]
33
     */
34 31
    public function getItems() : array
35
    {
36 31
        return $this->items;
37
    }
38
39 2
    public function getFirst(int $offset = 0) : ExecutedMigration
40
    {
41 2
        if (! isset($this->items[$offset])) {
42 1
            throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : ''));
43
        }
44
45 1
        return $this->items[$offset];
46
    }
47
48 24
    public function getLast(int $offset = 0) : ExecutedMigration
49
    {
50 24
        $offset = count($this->items) - 1 - (-1 * $offset);
51 24
        if (! isset($this->items[$offset])) {
52 7
            throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : ''));
53
        }
54
55 17
        return $this->items[$offset];
56
    }
57
58 28
    public function count() : int
59
    {
60 28
        return count($this->items);
61
    }
62
63 46
    public function hasMigration(Version $version) : bool
64
    {
65 46
        foreach ($this->items as $migration) {
66 28
            if ($migration->getVersion()->equals($version)) {
67 28
                return true;
68
            }
69
        }
70
71 35
        return false;
72
    }
73
74 5
    public function getMigration(Version $version) : ExecutedMigration
75
    {
76 5
        foreach ($this->items as $migration) {
77 5
            if ($migration->getVersion()->equals($version)) {
78 4
                return $migration;
79
            }
80
        }
81
82 1
        throw MigrationNotExecuted::new((string) $version);
83
    }
84
}
85