Completed
Pull Request — master (#1710)
by Flávio
01:25
created

Environment::getCurrentVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 4
cts 4
cp 1
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Migration\Manager;
9
10
use PDO;
11
use Phinx\Db\Adapter\AdapterFactory;
12
use Phinx\Db\Adapter\AdapterInterface;
13
use Phinx\Migration\MigrationInterface;
14
use Phinx\Seed\SeedInterface;
15
use RuntimeException;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Environment
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * @var array
28
     */
29
    protected $options;
30
31
    /**
32
     * @var \Symfony\Component\Console\Input\InputInterface
33
     */
34
    protected $input;
35
36
    /**
37
     * @var \Symfony\Component\Console\Output\OutputInterface
38
     */
39
    protected $output;
40
41
    /**
42
     * @var int
43
     */
44
    protected $currentVersion;
45
46
    /**
47
     * @var string
48
     */
49
    protected $schemaTableName = 'phinxlog';
50
51
    /**
52
     * @var \Phinx\Db\Adapter\AdapterInterface
53
     */
54
    protected $adapter;
55
56
    /**
57
     * @param string $name Environment Name
58
     * @param array $options Options
59
     */
60
    public function __construct($name, $options)
61
    {
62
        $this->name = $name;
63
        $this->options = $options;
64
    }
65
66
    /**
67
     * Executes the specified migration on this environment.
68
     *
69
     * @param \Phinx\Migration\MigrationInterface $migration Migration
70
     * @param string $direction Direction
71
     * @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
72
     *
73
     * @return void
74
     */
75
    public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP, $fake = false)
76
    {
77
        $direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
78
        $migration->setMigratingUp($direction === MigrationInterface::UP);
79
80
        $startTime = time();
81
        $migration->setAdapter($this->getAdapter());
82 404
83
        if (!$fake) {
84 404
            // begin the transaction if the adapter supports it
85 404
            if ($this->getAdapter()->hasTransactions()) {
86 404
                $this->getAdapter()->beginTransaction();
87
            }
88
89
            // Run the migration
90
            if (method_exists($migration, MigrationInterface::CHANGE)) {
91
                if ($direction === MigrationInterface::DOWN) {
92
                    // Create an instance of the ProxyAdapter so we can record all
93
                    // of the migration commands for reverse playback
94
95 10
                    /** @var \Phinx\Db\Adapter\ProxyAdapter $proxyAdapter */
96
                    $proxyAdapter = AdapterFactory::instance()
97 10
                        ->getWrapper('proxy', $this->getAdapter());
98 10
                    $migration->setAdapter($proxyAdapter);
99
                    /** @noinspection PhpUndefinedMethodInspection */
100 10
                    $migration->change();
0 ignored issues
show
Bug introduced by
The method change() does not seem to exist on object<Phinx\Migration\MigrationInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101 10
                    $proxyAdapter->executeInvertedCommands();
102
                    $migration->setAdapter($this->getAdapter());
103
                } else {
104 10
                    /** @noinspection PhpUndefinedMethodInspection */
105 6
                    $migration->change();
0 ignored issues
show
Bug introduced by
The method change() does not seem to exist on object<Phinx\Migration\MigrationInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106 6
                }
107
            } else {
108
                $migration->{$direction}();
109 10
            }
110 5
111
            // commit the transaction if the adapter supports it
112
            if ($this->getAdapter()->hasTransactions()) {
113
                $this->getAdapter()->commitTransaction();
114
            }
115 4
        }
116 4
117 4
        // Record it in the database
118
        $this->getAdapter()->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
119 4
    }
120 4
121 4
    /**
122 4
     * Executes the specified seeder on this environment.
123
     *
124 4
     * @param \Phinx\Seed\SeedInterface $seed
125
     *
126 5
     * @return void
127 5
     */
128
    public function executeSeed(SeedInterface $seed)
129
    {
130
        $seed->setAdapter($this->getAdapter());
131 10
132 6
        // begin the transaction if the adapter supports it
133 6
        if ($this->getAdapter()->hasTransactions()) {
134
            $this->getAdapter()->beginTransaction();
135
        }
136 10
137 10
        // Run the seeder
138
        if (method_exists($seed, SeedInterface::RUN)) {
139
            $seed->run();
140
        }
141
142
        // commit the transaction if the adapter supports it
143
        if ($this->getAdapter()->hasTransactions()) {
144
            $this->getAdapter()->commitTransaction();
145
        }
146
    }
147
148
    /**
149
     * Sets the environment's name.
150
     *
151
     * @param string $name Environment Name
152
     *
153
     * @return $this
154
     */
155
    public function setName($name)
156
    {
157
        $this->name = $name;
158
159
        return $this;
160
    }
161
162
    /**
163
     * Gets the environment name.
164
     *
165
     * @return string
166
     */
167
    public function getName()
168
    {
169
        return $this->name;
170
    }
171 1
172
    /**
173 1
     * Sets the environment's options.
174 1
     *
175
     * @param array $options Environment Options
176
     *
177
     * @return $this
178
     */
179
    public function setOptions($options)
180
    {
181
        $this->options = $options;
182 3
183
        return $this;
184 3
    }
185
186
    /**
187
     * Gets the environment's options.
188
     *
189
     * @return array
190
     */
191
    public function getOptions()
192
    {
193 6
        return $this->options;
194
    }
195 6
196 6
    /**
197
     * Sets the console input.
198
     *
199
     * @param \Symfony\Component\Console\Input\InputInterface $input
200
     *
201
     * @return $this
202
     */
203
    public function setInput(InputInterface $input)
204 2
    {
205
        $this->input = $input;
206 2
207
        return $this;
208
    }
209
210
    /**
211
     * Gets the console input.
212
     *
213
     * @return \Symfony\Component\Console\Input\InputInterface
214
     */
215 7
    public function getInput()
216
    {
217 7
        return $this->input;
218 7
    }
219
220
    /**
221
     * Sets the console output.
222
     *
223
     * @param \Symfony\Component\Console\Output\OutputInterface $output Output
224
     *
225
     * @return $this
226 9
     */
227
    public function setOutput(OutputInterface $output)
228 9
    {
229
        $this->output = $output;
230
231
        return $this;
232
    }
233
234
    /**
235
     * Gets the console output.
236
     *
237 6
     * @return \Symfony\Component\Console\Output\OutputInterface
238
     */
239 6
    public function getOutput()
240 6
    {
241
        return $this->output;
242
    }
243
244
    /**
245
     * Gets all migrated version numbers.
246
     *
247
     * @return array
248 8
     */
249
    public function getVersions()
250 8
    {
251
        return $this->getAdapter()->getVersions();
252
    }
253
254
    /**
255
     * Get all migration log entries, indexed by version creation time and sorted in ascending order by the configuration's
256
     * version_order option
257
     *
258 6
     * @return array
259
     */
260 6
    public function getVersionLog()
261
    {
262
        return $this->getAdapter()->getVersionLog();
263
    }
264
265
    /**
266
     * Sets the current version of the environment.
267
     *
268
     * @param int $version Environment Version
269 5
     *
270
     * @return $this
271 5
     */
272
    public function setCurrentVersion($version)
273
    {
274
        $this->currentVersion = $version;
275
276
        return $this;
277
    }
278
279
    /**
280 6
     * Gets the current version of the environment.
281
     *
282 6
     * @return int
283 6
     */
284
    public function getCurrentVersion()
285
    {
286
        // We don't cache this code as the current version is pretty volatile.
287
        // TODO - that means they're no point in a setter then?
288
        // maybe we should cache and call a reset() method every time a migration is run
289
        $versions = $this->getVersions();
290
        $version = 0;
291 6
292
        if (!empty($versions)) {
293
            $version = end($versions);
294
        }
295
296 6
        $this->setCurrentVersion($version);
297 6
298
        return $this->currentVersion;
299 6
    }
300 1
301 1
    /**
302
     * Sets the database adapter.
303 6
     *
304 6
     * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
305
     *
306
     * @return $this
307
     */
308
    public function setAdapter(AdapterInterface $adapter)
309
    {
310
        $this->adapter = $adapter;
311
312
        return $this;
313 14
    }
314
315 14
    /**
316 14
     * Gets the database adapter.
317
     *
318
     * @throws \RuntimeException
319
     *
320
     * @return \Phinx\Db\Adapter\AdapterInterface
321
     */
322
    public function getAdapter()
323
    {
324 17
        if (isset($this->adapter)) {
325
            return $this->adapter;
326 17
        }
327 12
328
        $options = $this->getOptions();
329 11
        if (isset($options['connection'])) {
330 3
            if (!($options['connection'] instanceof PDO)) {
331 1
                throw new RuntimeException('The specified connection is not a PDO instance');
332
            }
333
334 2
            $options['connection']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
335 2
            $options['adapter'] = $options['connection']->getAttribute(PDO::ATTR_DRIVER_NAME);
336 2
        }
337 10
        if (!isset($options['adapter'])) {
338 1
            throw new RuntimeException('No adapter was specified for environment: ' . $this->getName());
339
        }
340
341 9
        $factory = AdapterFactory::instance();
342
        $adapter = $factory
343 9
            ->getAdapter($options['adapter'], $options);
344
345
        // Automatically time the executed commands
346 8
        $adapter = $factory->getWrapper('timed', $adapter);
347
348 8
        if (isset($options['wrapper'])) {
349
            $adapter = $factory
350
                ->getWrapper($options['wrapper'], $adapter);
351
        }
352
353 8
        if ($this->getInput()) {
354 5
            $adapter->setInput($this->getInput());
355 5
        }
356
357 8
        if ($this->getOutput()) {
358 5
            $adapter->setOutput($this->getOutput());
359 5
        }
360
361
        // Use the TablePrefixAdapter if table prefix/suffixes are in use
362 8
        if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
363 1
            $adapter = AdapterFactory::instance()
364 1
                ->getWrapper('prefix', $adapter);
365 1
        }
366
367 8
        $this->setAdapter($adapter);
368
369 8
        return $adapter;
370
    }
371
372
    /**
373
     * Sets the schema table name.
374
     *
375
     * @param string $schemaTableName Schema Table Name
376
     *
377
     * @return $this
378 1
     */
379
    public function setSchemaTableName($schemaTableName)
380 1
    {
381 1
        $this->schemaTableName = $schemaTableName;
382
383
        return $this;
384
    }
385
386
    /**
387
     * Gets the schema table name.
388
     *
389 1
     * @return string
390
     */
391 1
    public function getSchemaTableName()
392
    {
393
        return $this->schemaTableName;
394
    }
395
}
396