@@ 17-83 (lines=67) @@ | ||
14 | /** |
|
15 | * Represents a sorted list of migrations that may or maybe not be already executed. |
|
16 | */ |
|
17 | final class AvailableMigrationsList implements Countable |
|
18 | { |
|
19 | /** @var AvailableMigration[] */ |
|
20 | private $items = []; |
|
21 | ||
22 | /** |
|
23 | * @param AvailableMigration[] $items |
|
24 | */ |
|
25 | public function __construct(array $items) |
|
26 | { |
|
27 | $this->items = array_values($items); |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @return AvailableMigration[] |
|
32 | */ |
|
33 | public function getItems() : array |
|
34 | { |
|
35 | return $this->items; |
|
36 | } |
|
37 | ||
38 | public function getFirst(int $offset = 0) : AvailableMigration |
|
39 | { |
|
40 | if (! isset($this->items[$offset])) { |
|
41 | throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : '')); |
|
42 | } |
|
43 | ||
44 | return $this->items[$offset]; |
|
45 | } |
|
46 | ||
47 | public function getLast(int $offset = 0) : AvailableMigration |
|
48 | { |
|
49 | $offset = count($this->items) - 1 - (-1 * $offset); |
|
50 | if (! isset($this->items[$offset])) { |
|
51 | throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : '')); |
|
52 | } |
|
53 | ||
54 | return $this->items[$offset]; |
|
55 | } |
|
56 | ||
57 | public function count() : int |
|
58 | { |
|
59 | return count($this->items); |
|
60 | } |
|
61 | ||
62 | public function hasMigration(Version $version) : bool |
|
63 | { |
|
64 | foreach ($this->items as $migration) { |
|
65 | if ($migration->getVersion()->equals($version)) { |
|
66 | return true; |
|
67 | } |
|
68 | } |
|
69 | ||
70 | return false; |
|
71 | } |
|
72 | ||
73 | public function getMigration(Version $version) : AvailableMigration |
|
74 | { |
|
75 | foreach ($this->items as $migration) { |
|
76 | if ($migration->getVersion()->equals($version)) { |
|
77 | return $migration; |
|
78 | } |
|
79 | } |
|
80 | ||
81 | throw MigrationNotAvailable::forVersion($version); |
|
82 | } |
|
83 | } |
|
84 |
@@ 18-84 (lines=67) @@ | ||
15 | * Represents a sorted list of executed migrations. |
|
16 | * The migrations in this set might be not available anymore. |
|
17 | */ |
|
18 | final class ExecutedMigrationsList implements Countable |
|
19 | { |
|
20 | /** @var ExecutedMigration[] */ |
|
21 | private $items = []; |
|
22 | ||
23 | /** |
|
24 | * @param ExecutedMigration[] $items |
|
25 | */ |
|
26 | public function __construct(array $items) |
|
27 | { |
|
28 | $this->items = array_values($items); |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * @return ExecutedMigration[] |
|
33 | */ |
|
34 | public function getItems() : array |
|
35 | { |
|
36 | return $this->items; |
|
37 | } |
|
38 | ||
39 | public function getFirst(int $offset = 0) : ExecutedMigration |
|
40 | { |
|
41 | if (! isset($this->items[$offset])) { |
|
42 | throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : '')); |
|
43 | } |
|
44 | ||
45 | return $this->items[$offset]; |
|
46 | } |
|
47 | ||
48 | public function getLast(int $offset = 0) : ExecutedMigration |
|
49 | { |
|
50 | $offset = count($this->items) - 1 - (-1 * $offset); |
|
51 | if (! isset($this->items[$offset])) { |
|
52 | throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : '')); |
|
53 | } |
|
54 | ||
55 | return $this->items[$offset]; |
|
56 | } |
|
57 | ||
58 | public function count() : int |
|
59 | { |
|
60 | return count($this->items); |
|
61 | } |
|
62 | ||
63 | public function hasMigration(Version $version) : bool |
|
64 | { |
|
65 | foreach ($this->items as $migration) { |
|
66 | if ($migration->getVersion()->equals($version)) { |
|
67 | return true; |
|
68 | } |
|
69 | } |
|
70 | ||
71 | return false; |
|
72 | } |
|
73 | ||
74 | public function getMigration(Version $version) : ExecutedMigration |
|
75 | { |
|
76 | foreach ($this->items as $migration) { |
|
77 | if ($migration->getVersion()->equals($version)) { |
|
78 | return $migration; |
|
79 | } |
|
80 | } |
|
81 | ||
82 | throw MigrationNotExecuted::new((string) $version); |
|
83 | } |
|
84 | } |
|
85 |