Failed Conditions
Pull Request — master (#673)
by Jonathan
02:30 queued 31s
created

Configuration::isDryRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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