Passed
Push — master ( 77cd29...c7d0d5 )
by Jonathan
02:04
created

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