Completed
Push — master ( 368556...6bf94b )
by Jonathan
11s
created

Migration::migrate()   C

Complexity

Conditions 15
Paths 42

Size

Total Lines 76
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 15.0044

Importance

Changes 0
Metric Value
cc 15
eloc 36
nc 42
nop 4
dl 0
loc 76
ccs 36
cts 37
cp 0.973
crap 15.0044
rs 5.257
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Exception\MigrationException;
9
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
10
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
11
use const COUNT_RECURSIVE;
12
use function count;
13
use function sprintf;
14
15
class Migration
16
{
17
    /** @var Configuration */
18
    private $configuration;
19
20
    /** @var MigrationRepository */
21
    private $migrationRepository;
22
23
    /** @var OutputWriter */
24
    private $outputWriter;
25
26
    /** @var bool */
27
    private $noMigrationException = false;
28
29 37
    public function __construct(
30
        Configuration $configuration,
31
        MigrationRepository $migrationRepository,
32
        OutputWriter $outputWriter
33
    ) {
34 37
        $this->configuration       = $configuration;
35 37
        $this->migrationRepository = $migrationRepository;
36 37
        $this->outputWriter        = $outputWriter;
37 37
    }
38
39 2
    public function setNoMigrationException(bool $noMigrationException = false) : void
40
    {
41 2
        $this->noMigrationException = $noMigrationException;
42 2
    }
43
44
    /** @return string[][] */
45 2
    public function getSql(?string $to = null) : array
46
    {
47 2
        return $this->migrate($to, true);
48
    }
49
50 6
    public function writeSqlFile(string $path, ?string $to = null) : bool
51
    {
52 6
        $sql = $this->getSql($to);
53
54 6
        $from = $this->migrationRepository->getCurrentVersion();
55
56 6
        if ($to === null) {
57 1
            $to = $this->migrationRepository->getLatestVersion();
58
        }
59
60 6
        $direction = $from > $to
61 1
            ? Version::DIRECTION_DOWN
62 6
            : Version::DIRECTION_UP;
63
64 6
        $this->outputWriter->write(
65 6
            sprintf("-- Migrating from %s to %s\n", $from, $to)
66
        );
67
68
        /**
69
         * Since the configuration object changes during the creation we cannot inject things
70
         * properly, so I had to violate LoD here (so please, let's find a way to solve it on v2).
71
         */
72 6
        return $this->configuration
73 6
            ->getQueryWriter()
74 6
            ->write($path, $direction, $sql);
75
    }
76
77
    /**
78
     * @throws MigrationException
79
     *
80
     * @return string[][]
81
     */
82 31
    public function migrate(
83
        ?string $to = null,
84
        bool $dryRun = false,
85
        bool $timeAllQueries = false,
86
        ?callable $confirm = null
87
    ) : array {
88 31
        if ($to === null) {
89 14
            $to = $this->migrationRepository->getLatestVersion();
90
        }
91
92 31
        $from = $this->migrationRepository->getCurrentVersion();
93 31
        $to   = $to;
94
95 31
        $versions = $this->migrationRepository->getMigrations();
96
97 31
        if (! isset($versions[$to]) && $to > 0) {
98 1
            throw UnknownMigrationVersion::new($to);
99
        }
100
101 30
        $direction = $from > $to
102 3
            ? Version::DIRECTION_DOWN
103 30
            : Version::DIRECTION_UP;
104
105 30
        $migrationsToExecute = $this->configuration
106 30
            ->getMigrationsToExecute($direction, $to);
107
108
        /**
109
         * If
110
         *  there are no migrations to execute
111
         *  and there are migrations,
112
         *  and the migration from and to are the same
113
         * means we are already at the destination return an empty array()
114
         * to signify that there is nothing left to do.
115
         */
116 30
        if ($from === $to && empty($migrationsToExecute) && ! empty($versions)) {
117 2
            return $this->noMigrations();
118
        }
119
120 29
        if (! $dryRun && $this->migrationsCanExecute($confirm) === false) {
121 1
            return [];
122
        }
123
124 28
        $output  = $dryRun ? 'Executing dry run of migration' : 'Migrating';
125 28
        $output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>';
126
127 28
        $this->outputWriter->write(sprintf($output, $direction, $to, $from));
128
129
        /**
130
         * If there are no migrations to execute throw an exception.
131
         */
132 28
        if (empty($migrationsToExecute) && ! $this->noMigrationException) {
133 1
            throw NoMigrationsToExecute::new();
134
        } elseif (empty($migrationsToExecute)) {
135 1
            return $this->noMigrations();
136
        }
137
138 26
        $this->configuration->dispatchMigrationEvent(Events::onMigrationsMigrating, $direction, $dryRun);
139
140 26
        $sql  = [];
141 26
        $time = 0;
142
143 26
        foreach ($migrationsToExecute as $version) {
144 26
            $versionExecutionResult = $version->execute($direction, $dryRun, $timeAllQueries);
145
146 26
            $sql[$version->getVersion()] = $versionExecutionResult->getSql();
147 26
            $time                       += $versionExecutionResult->getTime();
148
        }
149
150 26
        $this->configuration->dispatchMigrationEvent(Events::onMigrationsMigrated, $direction, $dryRun);
151
152 26
        $this->outputWriter->write("\n  <comment>------------------------</comment>\n");
153 26
        $this->outputWriter->write(sprintf('  <info>++</info> finished in %ss', $time));
154 26
        $this->outputWriter->write(sprintf('  <info>++</info> %s migrations executed', count($migrationsToExecute)));
155 26
        $this->outputWriter->write(sprintf('  <info>++</info> %s sql queries', count($sql, COUNT_RECURSIVE) - count($sql)));
156
157 26
        return $sql;
158
    }
159
160
    /** @return string[][] */
161 3
    private function noMigrations() : array
162
    {
163 3
        $this->outputWriter->write('<comment>No migrations to execute.</comment>');
164
165 3
        return [];
166
    }
167
168 27
    private function migrationsCanExecute(?callable $confirm = null) : bool
169
    {
170 27
        return $confirm === null ? true : (bool) $confirm();
171
    }
172
}
173