Failed Conditions
Push — master ( 0469de...12f4fc )
by Jonathan
02:19
created

Version::getMigration()   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;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Exception\MigrationNotConvertibleToSql;
10
use function assert;
11
use function count;
12
use function in_array;
13
14
class Version
15
{
16
    public const STATE_NONE = 0;
17
    public const STATE_PRE  = 1;
18
    public const STATE_EXEC = 2;
19
    public const STATE_POST = 3;
20
21
    public const DIRECTION_UP   = 'up';
22
    public const DIRECTION_DOWN = 'down';
23
24
    private const STATES = [
25
        self::STATE_NONE,
26
        self::STATE_PRE,
27
        self::STATE_EXEC,
28
        self::STATE_POST,
29
    ];
30
31
    /** @var Configuration */
32
    private $configuration;
33
34
    /** @var OutputWriter */
35
    private $outputWriter;
36
37
    /** @var string */
38
    private $version;
39
40
    /** @var AbstractMigration */
41
    private $migration;
42
43
    /** @var Connection */
44
    private $connection;
45
46
    /** @var string */
47
    private $class;
48
49
    /** @var int */
50
    private $state = self::STATE_NONE;
51
52
    /** @var VersionExecutorInterface */
53
    private $versionExecutor;
54
55 123
    public function __construct(
56
        Configuration $configuration,
57
        string $version,
58
        string $class,
59
        VersionExecutorInterface $versionExecutor
60
    ) {
61 123
        $this->configuration   = $configuration;
62 123
        $this->outputWriter    = $configuration->getOutputWriter();
63 123
        $this->class           = $class;
64 123
        $this->connection      = $configuration->getConnection();
65 123
        $this->migration       = new $class($this);
66 123
        $this->version         = $version;
67 123
        $this->versionExecutor = $versionExecutor;
68 123
    }
69
70 53
    public function __toString() : string
71
    {
72 53
        return $this->version;
73
    }
74
75 79
    public function getVersion() : string
76
    {
77 79
        return $this->version;
78
    }
79
80 123
    public function getConfiguration() : Configuration
81
    {
82 123
        return $this->configuration;
83
    }
84
85 12
    public function getMigration() : AbstractMigration
86
    {
87 12
        return $this->migration;
88
    }
89
90 15
    public function isMigrated() : bool
91
    {
92 15
        return $this->configuration->hasVersionMigrated($this);
93
    }
94
95 53
    public function setState(int $state) : void
96
    {
97 53
        assert(in_array($state, self::STATES, true));
98
99 53
        $this->state = $state;
100 53
    }
101
102 10
    public function getExecutionState() : string
103
    {
104 10
        switch ($this->state) {
105 10
            case self::STATE_PRE:
106 1
                return 'Pre-Checks';
107
108 9
            case self::STATE_POST:
109 1
                return 'Post-Checks';
110
111 8
            case self::STATE_EXEC:
112 2
                return 'Execution';
113
114
            default:
115 6
                return 'No State';
116
        }
117
    }
118
119
    /**
120
     * @param mixed[] $params
121
     * @param mixed[] $types
122
     */
123 32
    public function addSql(string $sql, array $params = [], array $types = []) : void
124
    {
125 32
        $this->versionExecutor->addSql($sql, $params, $types);
126 32
    }
127
128 9
    public function writeSqlFile(
129
        string $path,
130
        string $direction = self::DIRECTION_UP
131
    ) : bool {
132 9
        $versionExecutionResult = $this->execute($direction, true);
133
134 9
        if (count($versionExecutionResult->getParams()) !== 0) {
135 1
            throw MigrationNotConvertibleToSql::new($this->class);
136
        }
137
138 8
        $this->outputWriter->write("\n-- Version " . $this->version . "\n");
139
140 8
        $sqlQueries = [$this->version => $versionExecutionResult->getSql()];
141
142
        /*
143
         * Since the configuration object changes during the creation we cannot inject things
144
         * properly, so I had to violate LoD here (so please, let's find a way to solve it on v2).
145
         */
146 8
        return $this->configuration
147 8
            ->getQueryWriter()
148 8
            ->write($path, $direction, $sqlQueries);
149
    }
150
151 51
    public function execute(
152
        string $direction,
153
        bool $dryRun = false,
154
        bool $timeAllQueries = false
155
    ) : VersionExecutionResult {
156 51
        return $this->versionExecutor->execute(
157 51
            $this,
158 51
            $this->migration,
159 51
            $direction,
160 51
            $dryRun,
161 51
            $timeAllQueries
162
        );
163
    }
164
165 11
    public function markMigrated() : void
166
    {
167 11
        $this->markVersion(self::DIRECTION_UP);
168 11
    }
169
170 3
    public function markNotMigrated() : void
171
    {
172 3
        $this->markVersion(self::DIRECTION_DOWN);
173 3
    }
174
175 45
    public function markVersion(string $direction) : void
176
    {
177 45
        $this->configuration->createMigrationTable();
178
179 45
        $migrationsColumnName = $this->configuration
180 45
            ->getQuotedMigrationsColumnName();
181
182 45
        if ($direction === self::DIRECTION_UP) {
183 44
            $this->connection->insert(
184 44
                $this->configuration->getMigrationsTableName(),
185
                [
186 44
                    $migrationsColumnName => $this->version,
187
                ]
188
            );
189
        } else {
190 10
            $this->connection->delete(
191 10
                $this->configuration->getMigrationsTableName(),
192
                [
193 10
                    $migrationsColumnName => $this->version,
194
                ]
195
            );
196
        }
197 45
    }
198
}
199