Failed Conditions
Push — master ( 51f284...368556 )
by Jonathan
02:26
created

Configuration::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration;
6
7
use DateTime;
8
use DateTimeInterface;
9
use DateTimeZone;
10
use Doctrine\Common\EventArgs;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\Connections\MasterSlaveConnection;
13
use Doctrine\Migrations\Configuration\Exception\MigrationsNamespaceRequired;
14
use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder;
15
use Doctrine\Migrations\DependencyFactory;
16
use Doctrine\Migrations\Exception\MigrationException;
17
use Doctrine\Migrations\Exception\MigrationsDirectoryRequired;
18
use Doctrine\Migrations\Finder\MigrationDeepFinder;
19
use Doctrine\Migrations\Finder\MigrationFinder;
20
use Doctrine\Migrations\OutputWriter;
21
use Doctrine\Migrations\QueryWriter;
22
use Doctrine\Migrations\Version;
23
use function str_replace;
24
25
class Configuration
26
{
27
    public const VERSIONS_ORGANIZATION_BY_YEAR = 'year';
28
29
    public const VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH = 'year_and_month';
30
31
    public const VERSION_FORMAT = 'YmdHis';
32
33
    /** @var string|null */
34
    private $name;
35
36
    /** @var string */
37
    private $migrationsTableName = 'doctrine_migration_versions';
38
39
    /** @var string */
40
    private $migrationsColumnName = 'version';
41
42
    /** @var string|null */
43
    private $migrationsDirectory;
44
45
    /** @var string|null */
46
    private $migrationsNamespace;
47
48
    /** @var bool */
49
    private $migrationsAreOrganizedByYear = false;
50
51
    /** @var bool */
52
    private $migrationsAreOrganizedByYearAndMonth = false;
53
54
    /** @var string|null */
55
    private $customTemplate;
56
57
    /** @var bool */
58
    private $isDryRun = false;
59
60
    /** @var Connection */
61
    private $connection;
62
63
    /** @var OutputWriter|null */
64
    private $outputWriter;
65
66
    /** @var MigrationFinder|null */
67
    private $migrationFinder;
68
69
    /** @var QueryWriter|null */
70
    private $queryWriter;
71
72
    /** @var DependencyFactory */
73
    private $dependencyFactory;
74
75 259
    public function __construct(
76
        Connection $connection,
77
        ?OutputWriter $outputWriter = null,
78
        ?MigrationFinder $migrationFinder = null,
79
        ?QueryWriter $queryWriter = null
80
    ) {
81 259
        $this->connection      = $connection;
82 259
        $this->outputWriter    = $outputWriter;
83 259
        $this->migrationFinder = $migrationFinder;
84 259
        $this->queryWriter     = $queryWriter;
85 259
    }
86
87 63
    public function setName(string $name) : void
88
    {
89 63
        $this->name = $name;
90 63
    }
91
92 16
    public function getName() : ?string
93
    {
94 16
        return $this->name;
95
    }
96
97 183
    public function getConnection() : Connection
98
    {
99 183
        return $this->connection;
100
    }
101
102 86
    public function setMigrationsTableName(string $tableName) : void
103
    {
104 86
        $this->migrationsTableName = $tableName;
105 86
    }
106
107 82
    public function getMigrationsTableName() : string
108
    {
109 82
        return $this->migrationsTableName;
110
    }
111
112 57
    public function setMigrationsColumnName(string $columnName) : void
113
    {
114 57
        $this->migrationsColumnName = $columnName;
115 57
    }
116
117 76
    public function getMigrationsColumnName() : string
118
    {
119 76
        return $this->migrationsColumnName;
120
    }
121
122 71
    public function getQuotedMigrationsColumnName() : string
123
    {
124 71
        return $this->getDependencyFactory()
125 71
            ->getMigrationTableCreator()
126 71
            ->getMigrationsColumn()
127 71
            ->getQuotedName($this->connection->getDatabasePlatform());
128
    }
129
130 179
    public function setMigrationsDirectory(string $migrationsDirectory) : void
131
    {
132 179
        $this->migrationsDirectory = $migrationsDirectory;
133 179
    }
134
135 81
    public function getMigrationsDirectory() : ?string
136
    {
137 81
        return $this->migrationsDirectory;
138
    }
139
140 182
    public function setMigrationsNamespace(string $migrationsNamespace) : void
141
    {
142 182
        $this->migrationsNamespace = $migrationsNamespace;
143 182
    }
144
145 93
    public function getMigrationsNamespace() : ?string
146
    {
147 93
        return $this->migrationsNamespace;
148
    }
149
150 5
    public function setCustomTemplate(?string $customTemplate) : void
151
    {
152 5
        $this->customTemplate = $customTemplate;
153 5
    }
154
155 9
    public function getCustomTemplate() : ?string
156
    {
157 9
        return $this->customTemplate;
158
    }
159
160 22
    public function areMigrationsOrganizedByYear() : bool
161
    {
162 22
        return $this->migrationsAreOrganizedByYear;
163
    }
164
165
    /**
166
     * @throws MigrationException
167
     */
168 9
    public function setMigrationsAreOrganizedByYear(
169
        bool $migrationsAreOrganizedByYear = true
170
    ) : void {
171 9
        $this->ensureOrganizeMigrationsIsCompatibleWithFinder();
172
173 5
        $this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYear;
174 5
    }
175
176
    /**
177
     * @throws MigrationException
178
     */
179 10
    public function setMigrationsAreOrganizedByYearAndMonth(
180
        bool $migrationsAreOrganizedByYearAndMonth = true
181
    ) : void {
182 10
        $this->ensureOrganizeMigrationsIsCompatibleWithFinder();
183
184 10
        $this->migrationsAreOrganizedByYear         = $migrationsAreOrganizedByYearAndMonth;
185 10
        $this->migrationsAreOrganizedByYearAndMonth = $migrationsAreOrganizedByYearAndMonth;
186 10
    }
187
188 22
    public function areMigrationsOrganizedByYearAndMonth() : bool
189
    {
190 22
        return $this->migrationsAreOrganizedByYearAndMonth;
191
    }
192
193
    /** @throws MigrationException */
194 8
    public function setMigrationsFinder(MigrationFinder $migrationFinder) : void
195
    {
196 8
        if (($this->migrationsAreOrganizedByYear || $this->migrationsAreOrganizedByYearAndMonth)
197 8
            && ! ($migrationFinder instanceof MigrationDeepFinder)) {
198 4
            throw ParameterIncompatibleWithFinder::new(
199 4
                'organize-migrations',
200 4
                $migrationFinder
201
            );
202
        }
203
204 4
        $this->migrationFinder = $migrationFinder;
205 4
    }
206
207 149
    public function getMigrationsFinder() : MigrationFinder
208
    {
209 149
        if ($this->migrationFinder === null) {
210 145
            $this->migrationFinder = $this->getDependencyFactory()->getRecursiveRegexFinder();
211
        }
212
213 149
        return $this->migrationFinder;
214
    }
215
216
    /** @throws MigrationException */
217 118
    public function validate() : void
218
    {
219 118
        if ($this->migrationsNamespace === null) {
220 1
            throw MigrationsNamespaceRequired::new();
221
        }
222
223 117
        if ($this->migrationsDirectory === null) {
224 1
            throw MigrationsDirectoryRequired::new();
225
        }
226 116
    }
227
228 21
    public function hasVersionMigrated(Version $version) : bool
229
    {
230 21
        return $this->getDependencyFactory()->getMigrationRepository()->hasVersionMigrated($version);
231
    }
232
233 9
    public function resolveVersionAlias(string $alias) : ?string
234
    {
235 9
        return $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($alias);
236
    }
237
238 2
    public function setIsDryRun(bool $isDryRun) : void
239
    {
240 2
        $this->isDryRun = $isDryRun;
241 2
    }
242
243 65
    public function isDryRun() : bool
244
    {
245 65
        return $this->isDryRun;
246
    }
247
248 48
    public function isMigrationTableCreated() : bool
249
    {
250 48
        return $this->getDependencyFactory()->getMigrationTableCreator()->isMigrationTableCreated();
251
    }
252
253 65
    public function createMigrationTable() : bool
254
    {
255 65
        return $this->getDependencyFactory()->getMigrationTableCreator()->createMigrationTable();
256
    }
257
258 14
    public function getDateTime(string $version) : string
259
    {
260 14
        $datetime = str_replace('Version', '', $version);
261 14
        $datetime = DateTime::createFromFormat('YmdHis', $datetime);
262
263 14
        if ($datetime === false) {
264 4
            return '';
265
        }
266
267 11
        return $datetime->format('Y-m-d H:i:s');
268
    }
269
270 9
    public function generateVersionNumber(?DateTimeInterface $now = null) : string
271
    {
272 9
        $now = $now ?: new DateTime('now', new DateTimeZone('UTC'));
273
274 9
        return $now->format(self::VERSION_FORMAT);
275
    }
276
277
    /**
278
     * Explicitely opens the database connection. This is done to play nice
279
     * with DBAL's MasterSlaveConnection. Which, in some cases, connects to a
280
     * follower when fetching the executed migrations. If a follower is lagging
281
     * significantly behind that means the migrations system may see unexecuted
282
     * migrations that were actually executed earlier.
283
     */
284 65
    public function connect() : bool
285
    {
286 65
        if ($this->connection instanceof MasterSlaveConnection) {
287 1
            return $this->connection->connect('master');
288
        }
289
290 64
        return $this->connection->connect();
291
    }
292
293 26
    public function dispatchMigrationEvent(string $eventName, string $direction, bool $dryRun) : void
294
    {
295 26
        $this->getDependencyFactory()->getEventDispatcher()->dispatchMigrationEvent(
296 26
            $eventName,
297 26
            $direction,
298 26
            $dryRun
299
        );
300 26
    }
301
302 51
    public function dispatchVersionEvent(
303
        Version $version,
304
        string $eventName,
305
        string $direction,
306
        bool $dryRun
307
    ) : void {
308 51
        $this->getDependencyFactory()->getEventDispatcher()->dispatchVersionEvent(
309 51
            $version,
310 51
            $eventName,
311 51
            $direction,
312 51
            $dryRun
313
        );
314 51
    }
315
316 1
    public function dispatchEvent(string $eventName, ?EventArgs $args = null) : void
317
    {
318 1
        $this->getDependencyFactory()->getEventDispatcher()->dispatchEvent(
319 1
            $eventName,
320 1
            $args
321
        );
322 1
    }
323
324 1
    public function getNumberOfExecutedMigrations() : int
325
    {
326 1
        return $this->getDependencyFactory()->getMigrationRepository()->getNumberOfExecutedMigrations();
327
    }
328
329 3
    public function getNumberOfAvailableMigrations() : int
330
    {
331 3
        return $this->getDependencyFactory()->getMigrationRepository()->getNumberOfAvailableMigrations();
332
    }
333
334 19
    public function getLatestVersion() : string
335
    {
336 19
        return $this->getDependencyFactory()->getMigrationRepository()->getLatestVersion();
337
    }
338
339
    /** @return string[] */
340 10
    public function getMigratedVersions() : array
341
    {
342 10
        return $this->getDependencyFactory()->getMigrationRepository()->getMigratedVersions();
343
    }
344
345
    /** @return string[] */
346 14
    public function getAvailableVersions() : array
347
    {
348 14
        return $this->getDependencyFactory()->getMigrationRepository()->getAvailableVersions();
349
    }
350
351 35
    public function getCurrentVersion() : string
352
    {
353 35
        return $this->getDependencyFactory()->getMigrationRepository()->getCurrentVersion();
354
    }
355
356
    /** @return Version[] */
357 56
    public function registerMigrationsFromDirectory(string $path) : array
358
    {
359 56
        $this->validate();
360
361 56
        return $this->getDependencyFactory()->getMigrationRepository()->registerMigrationsFromDirectory($path);
362
    }
363
364
    /** @throws MigrationException */
365 40
    public function registerMigration(string $version, string $class) : Version
366
    {
367 40
        return $this->getDependencyFactory()->getMigrationRepository()->registerMigration($version, $class);
368
    }
369
370
    /**
371
     * @param string[] $migrations
372
     *
373
     * @return Version[]
374
     */
375 5
    public function registerMigrations(array $migrations) : array
376
    {
377 5
        return $this->getDependencyFactory()->getMigrationRepository()->registerMigrations($migrations);
378
    }
379
380
    /**
381
     * @return Version[]
382
     */
383 35
    public function getMigrations() : array
384
    {
385 35
        return $this->getDependencyFactory()->getMigrationRepository()->getMigrations();
386
    }
387
388 21
    public function getVersion(string $version) : Version
389
    {
390 21
        return $this->getDependencyFactory()->getMigrationRepository()->getVersion($version);
391
    }
392
393 11
    public function hasVersion(string $version) : bool
394
    {
395 11
        return $this->getDependencyFactory()->getMigrationRepository()->hasVersion($version);
396
    }
397
398
    /** @return Version[] */
399 33
    public function getMigrationsToExecute(string $direction, string $to) : array
400
    {
401 33
        return $this->getDependencyFactory()->getMigrationPlanCalculator()->getMigrationsToExecute($direction, $to);
402
    }
403
404 2
    public function getPrevVersion() : ?string
405
    {
406 2
        return $this->getDependencyFactory()->getMigrationRepository()->getPrevVersion();
407
    }
408
409 3
    public function getNextVersion() : ?string
410
    {
411 3
        return $this->getDependencyFactory()->getMigrationRepository()->getNextVersion();
412
    }
413
414 3
    public function getRelativeVersion(string $version, int $delta) : ?string
415
    {
416 3
        return $this->getDependencyFactory()->getMigrationRepository()->getRelativeVersion($version, $delta);
417
    }
418
419 1
    public function getDeltaVersion(string $delta) : ?string
420
    {
421 1
        return $this->getDependencyFactory()->getMigrationRepository()->getDeltaVersion($delta);
422
    }
423
424 5
    public function setOutputWriter(OutputWriter $outputWriter) : void
425
    {
426 5
        $this->outputWriter = $outputWriter;
427 5
    }
428
429 123
    public function getOutputWriter() : OutputWriter
430
    {
431 123
        if ($this->outputWriter === null) {
432 82
            $this->outputWriter = $this->getDependencyFactory()->getOutputWriter();
433
        }
434
435 123
        return $this->outputWriter;
436
    }
437
438 8
    public function getQueryWriter() : QueryWriter
439
    {
440 8
        if ($this->queryWriter === null) {
441 7
            $this->queryWriter = $this->getDependencyFactory()->getQueryWriter();
442
        }
443
444 8
        return $this->queryWriter;
445
    }
446
447
    /**
448
     * @throws MigrationException
449
     */
450 19
    private function ensureOrganizeMigrationsIsCompatibleWithFinder() : void
451
    {
452 19
        if (! ($this->getMigrationsFinder() instanceof MigrationDeepFinder)) {
453 4
            throw ParameterIncompatibleWithFinder::new(
454 4
                'organize-migrations',
455 4
                $this->getMigrationsFinder()
456
            );
457
        }
458 15
    }
459
460 180
    private function getDependencyFactory() : DependencyFactory
461
    {
462 180
        if ($this->dependencyFactory === null) {
463 180
            $this->dependencyFactory = new DependencyFactory($this);
464
        }
465
466 180
        return $this->dependencyFactory;
467
    }
468
}
469