Failed Conditions
Pull Request — master (#675)
by Jonathan
06:31
created

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