Migrate::execute()   F
last analyzed

Complexity

Conditions 13
Paths 1098

Size

Total Lines 79
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 14.458

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 79
c 0
b 0
f 0
ccs 31
cts 39
cp 0.7949
rs 2.45
cc 13
nc 1098
nop 2
crap 14.458

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
/**
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\Console\Command;
9
10
use DateTime;
11
use Exception;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Throwable;
16
17
class Migrate extends AbstractCommand
18
{
19
    /**
20
     * @var string
21
     */
22
    protected static $defaultName = 'migrate';
23
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @return void
28
     */
29
    protected function configure()
30
    {
31
        parent::configure();
32
33
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
34
35
        $this->setDescription('Migrate the database')
36
            ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to')
37
            ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to')
38
            ->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
39
            ->addOption('--fake', null, InputOption::VALUE_NONE, "Mark any migrations selected as run, but don't actually execute them")
40 35
            ->setHelp(
41
                <<<EOT
42 35
The <info>migrate</info> command runs all available migrations, optionally up to a specific version
43
44 35
<info>phinx migrate -e development</info>
45
<info>phinx migrate -e development -t 20110103081132</info>
46 35
<info>phinx migrate -e development -d 20110103</info>
47 35
<info>phinx migrate -e development -v</info>
48 35
49 35
EOT
50 35
            );
51 35
    }
52
53
    /**
54
     * Migrate the database.
55
     *
56
     * @param \Symfony\Component\Console\Input\InputInterface $input Input
57
     * @param \Symfony\Component\Console\Output\OutputInterface $output Output
58
     * @return int integer 0 on success, or an error code.
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output)
61 35
    {
62 35
        $this->bootstrap($input, $output);
63
64
        $version = $input->getOption('target');
65
        $environment = $input->getOption('environment');
66
        $date = $input->getOption('date');
67
        $fake = (bool)$input->getOption('fake');
68
69
        if ($environment === null) {
70
            $environment = $this->getConfig()->getDefaultEnvironment();
71 3
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment, $this->verbosityLevel);
72
        } else {
73 3
            $output->writeln('<info>using environment</info> ' . $environment, $this->verbosityLevel);
74
        }
75 3
76 3
        if (!$this->getConfig()->hasEnvironment($environment)) {
77 3
            $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
78
79 3
            return self::CODE_ERROR;
80 2
        }
81 2
82 2
        $envOptions = $this->getConfig()->getEnvironment($environment);
83 1
        if (isset($envOptions['adapter'])) {
84
            $output->writeln('<info>using adapter</info> ' . $envOptions['adapter'], $this->verbosityLevel);
85
        }
86 3
87 3
        if (isset($envOptions['wrapper'])) {
88 2
            $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper'], $this->verbosityLevel);
89 2
        }
90
91 3
        if (isset($envOptions['name'])) {
92
            $output->writeln('<info>using database</info> ' . $envOptions['name'], $this->verbosityLevel);
93
        } else {
94
            $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
95 3
96 2
            return self::CODE_ERROR;
97 2
        }
98 1
99 1
        if (isset($envOptions['table_prefix'])) {
100
            $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix'], $this->verbosityLevel);
101
        }
102 2
        if (isset($envOptions['table_suffix'])) {
103
            $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix'], $this->verbosityLevel);
104
        }
105 2
106
        $versionOrder = $this->getConfig()->getVersionOrder();
107
        $output->writeln('<info>ordering by</info> ' . $versionOrder . ' time', $this->verbosityLevel);
108
109
        if ($fake) {
110 2
            $output->writeln('<comment>warning</comment> performing fake migrations', $this->verbosityLevel);
111 2
        }
112
113
        try {
114 2
            // run the migrations
115
            $start = microtime(true);
116 2
            if ($date !== null) {
117
                $this->getManager()->migrateToDateTime($environment, new DateTime($date), $fake);
118 2
            } else {
119 2
                if ($version) {
120
                    $version = (int)$version;
121 2
                }
122
                $this->getManager()->migrate($environment, $version, $fake);
123
            }
124
            $end = microtime(true);
125
        } catch (Exception $e) {
126
            $output->writeln('<error>' . $e->__toString() . '</error>');
127
128
            return self::CODE_ERROR;
129
        } catch (Throwable $e) {
130
            $output->writeln('<error>' . $e->__toString() . '</error>');
131
132
            return self::CODE_ERROR;
133
        }
134
135
        $output->writeln('', $this->verbosityLevel);
136
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>', $this->verbosityLevel);
137
138
        return self::CODE_SUCCESS;
139
    }
140
}
141