Completed
Pull Request — master (#969)
by Asmir
02:41
created

FilesystemMigrationsRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 97.92%

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 127
ccs 47
cts 48
cp 0.9792
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMigration() 0 9 2
A hasMigration() 0 5 1
A getMigrations() 0 5 1
A __construct() 0 11 1
A loadMigrationsFromDirectories() 0 16 3
A registerMigrations() 0 9 2
A ensureMigrationClassExists() 0 4 2
A registerMigrationInstance() 0 12 2
A registerMigration() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use Doctrine\Migrations\Exception\DuplicateMigrationVersion;
8
use Doctrine\Migrations\Exception\MigrationClassNotFound;
9
use Doctrine\Migrations\Exception\MigrationException;
10
use Doctrine\Migrations\Finder\MigrationFinder;
11
use Doctrine\Migrations\Metadata\AvailableMigration;
12
use Doctrine\Migrations\Metadata\AvailableMigrationsSet;
13
use Doctrine\Migrations\Version\MigrationFactory;
14
use Doctrine\Migrations\Version\Version;
15
use function class_exists;
16
17
/**
18
 * The FilesystemMigrationsRepository class is responsible for retrieving migrations, determining what the current migration
19
 * version, etc.
20
 *
21
 * @internal
22
 */
23
class FilesystemMigrationsRepository implements MigrationsRepository
24
{
25
    /** @var bool */
26
    private $migrationsLoaded = false;
27
28
    /** @var array<string, string> */
29
    private $migrationDirectories;
30
31
    /** @var MigrationFinder */
32
    private $migrationFinder;
33
34
    /** @var MigrationFactory */
35
    private $versionFactory;
36
37
    /** @var AvailableMigration[] */
38
    private $migrations = [];
39
40
    /**
41
     * @param string[]              $classes
42
     * @param array<string, string> $migrationDirectories
43
     */
44 74
    public function __construct(
45
        array $classes,
46
        array $migrationDirectories,
47
        MigrationFinder $migrationFinder,
48
        MigrationFactory $versionFactory
49
    ) {
50 74
        $this->migrationDirectories = $migrationDirectories;
51 74
        $this->migrationFinder      = $migrationFinder;
52 74
        $this->versionFactory       = $versionFactory;
53
54 74
        $this->registerMigrations($classes);
55 74
    }
56
57 57
    private function registerMigrationInstance(Version $version, AbstractMigration $migration) : AvailableMigration
58
    {
59 57
        if (isset($this->migrations[(string) $version])) {
60 1
            throw DuplicateMigrationVersion::new(
61 1
                (string) $version,
62 1
                (string) $version
63
            );
64
        }
65
66 57
        $this->migrations[(string) $version] = new AvailableMigration($version, $migration);
67
68 57
        return $this->migrations[(string) $version];
69
    }
70
71
    /** @throws MigrationException */
72 6
    public function registerMigration(string $migrationClassName) : AvailableMigration
73
    {
74 6
        $this->ensureMigrationClassExists($migrationClassName);
75
76 6
        $version   = new Version($migrationClassName);
77 6
        $migration = $this->versionFactory->createVersion($migrationClassName);
78
79 6
        return $this->registerMigrationInstance($version, $migration);
80
    }
81
82
    /**
83
     * @param string[] $migrations
84
     *
85
     * @return AvailableMigration[]
86
     */
87 74
    private function registerMigrations(array $migrations) : array
88
    {
89 74
        $versions = [];
90
91 74
        foreach ($migrations as $class) {
92 6
            $versions[] = $this->registerMigration($class);
93
        }
94
95 74
        return $versions;
96
    }
97
98 9
    public function hasMigration(string $version) : bool
99
    {
100 9
        $this->loadMigrationsFromDirectories();
101
102 9
        return isset($this->migrations[$version]);
103
    }
104
105 15
    public function getMigration(Version $version) : AvailableMigration
106
    {
107 15
        $this->loadMigrationsFromDirectories();
108
109 15
        if (! isset($this->migrations[(string) $version])) {
110 4
            throw MigrationClassNotFound::new((string) $version);
111
        }
112
113 12
        return $this->migrations[(string) $version];
114
    }
115
116
    /**
117
     * Returns a non-sorted set of migrations.
118
     */
119 57
    public function getMigrations() : AvailableMigrationsSet
120
    {
121 57
        $this->loadMigrationsFromDirectories();
122
123 57
        return new AvailableMigrationsSet($this->migrations);
124
    }
125
126
    /** @throws MigrationException */
127 6
    private function ensureMigrationClassExists(string $class) : void
128
    {
129 6
        if (! class_exists($class)) {
130
            throw MigrationClassNotFound::new($class);
131
        }
132 6
    }
133
134 60
    private function loadMigrationsFromDirectories() : void
135
    {
136 60
        $migrationDirectories = $this->migrationDirectories;
137
138 60
        if ($this->migrationsLoaded) {
139 37
            return;
140
        }
141
142 60
        $this->migrationsLoaded = true;
143
144 60
        foreach ($migrationDirectories as $namespace => $path) {
145 28
                $migrations = $this->migrationFinder->findMigrations(
146 28
                    $path,
147 28
                    $namespace
148
                );
149 28
                $this->registerMigrations($migrations);
150
        }
151 60
    }
152
}
153