Completed
Push — master ( 39c620...12e227 )
by Mike
03:57
created

AbstractCommand   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 89.83%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 21
lcom 1
cbo 8
dl 0
loc 148
c 6
b 3
f 1
ccs 53
cts 59
cp 0.8983
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A outputHeader() 0 10 2
A setMigrationConfiguration() 0 4 1
A getMigrationConfiguration() 0 14 4
A askConfirmation() 0 8 2
A getOutputWriter() 0 10 2
D getConnection() 0 30 9
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
21
22
use Doctrine\DBAL\DriverManager;
23
use Doctrine\DBAL\Migrations\Configuration\Configuration;
24
use Doctrine\DBAL\Migrations\OutputWriter;
25
use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper;
26
use Symfony\Component\Console\Command\Command;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Question\ConfirmationQuestion;
31
32
/**
33
 * CLI Command for adding and deleting migration versions from the version table.
34
 *
35
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
36
 * @link    www.doctrine-project.org
37
 * @since   2.0
38
 * @author  Jonathan Wage <[email protected]>
39
 */
40
abstract class AbstractCommand extends Command
41
{
42
    /**
43
     * The configuration property only contains the configuration injected by the setter.
44
     *
45
     * @var Configuration
46
     */
47
    private $configuration;
48
49
    /**
50
     * The migrationConfiguration property contains the configuration
51
     * created taking into account the command line options.
52
     *
53
     * @var Configuration
54
     */
55
    private $migrationConfiguration;
56
57
    /**
58
     * @var OutputWriter
59
     */
60
    private $outputWriter;
61
62
    /**
63
     * @var \Doctrine\DBAL\Connection
64
     */
65
    private $connection;
66
67 33
    protected function configure()
68
    {
69 33
        $this->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.');
70 33
        $this->addOption('db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.');
71 33
    }
72
73 1
    protected function outputHeader(Configuration $configuration, OutputInterface $output)
74
    {
75 1
        $name = $configuration->getName();
76 1
        $name = $name ? $name : 'Doctrine Database Migrations';
77 1
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
78 1
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
79 1
        $output->writeln('<question>' . $name . '</question>');
80 1
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
81 1
        $output->writeln('');
82 1
    }
83
84 4
    public function setMigrationConfiguration(Configuration $config)
85
    {
86 4
        $this->configuration = $config;
87 4
    }
88
89
    /**
90
     * When any (config) command line option is passed to the migration the migrationConfiguration
91
     * property is set with the new generated configuration.
92
     * If no (config) option is passed the migrationConfiguration property is set to the value
93
     * of the configuration one (if any).
94
     * Else a new configuration is created and assigned to the migrationConfiguration property.
95
     *
96
     * @param InputInterface  $input
97
     * @param OutputInterface $output
98
     *
99
     * @return Configuration
100
     */
101 16
    protected function getMigrationConfiguration(InputInterface $input, OutputInterface $output)
102
    {
103 16
        if (!$this->migrationConfiguration) {
104 16
            if ($this->getHelperSet()->has('configuration')
105 16
                && $this->getHelperSet()->get('configuration') instanceof ConfigurationHelper) {
106 1
                $configHelper = $this->getHelperSet()->get('configuration');
107 1
            } else {
108 15
                $configHelper = new ConfigurationHelper($this->getConnection($input), $this->configuration);
109
            }
110 15
            $this->migrationConfiguration = $configHelper->getMigrationConfig($input, $this->getOutputWriter($output));
111 15
        }
112
113 15
        return $this->migrationConfiguration;
114
    }
115
116
    /**
117
     * This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
118
     * but use the ConfirmationQuestion when available.
119
     *
120
     * @param $question
121
     * @param InputInterface $input
122
     * @param OutputInterface $output
123
     * @return mixed
124
     */
125 2
    protected function askConfirmation($question, InputInterface $input, OutputInterface $output)
126
    {
127 2
        if (!$this->getHelperSet()->has('question')) {
128 1
            return $this->getHelper('dialog')->askConfirmation($output, '<question>' . $question . '</question>', false);
129
        }
130
131 2
        return $this->getHelper('question')->ask($input, $output, new ConfirmationQuestion($question));
132
    }
133
134
    /**
135
     * @param \Symfony\Component\Console\Output\OutputInterface $output
136
     *
137
     * @return \Doctrine\DBAL\Migrations\OutputWriter
138
     */
139 15
    private function getOutputWriter(OutputInterface $output)
140
    {
141 15
        if (!$this->outputWriter) {
142 15
            $this->outputWriter = new OutputWriter(function($message) use ($output) {
143 13
                return $output->writeln($message);
144 15
            });
145 15
        }
146
147 15
        return $this->outputWriter;
148
    }
149
150
    /**
151
     * @param \Symfony\Component\Console\Input\InputInterface $input
152
     *
153
     * @return \Doctrine\DBAL\Connection
154
     * @throws \Doctrine\DBAL\DBALException
155
     */
156 15
    private function getConnection(InputInterface $input)
157
    {
158 15
        if (!$this->connection) {
159 15
            if ($input->getOption('db-configuration')) {
160 1
                if (!file_exists($input->getOption('db-configuration'))) {
161
                    throw new \InvalidArgumentException("The specified connection file is not a valid file.");
162
                }
163
164 1
                $params = include $input->getOption('db-configuration');
165 1
                if (!is_array($params)) {
166
                    throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
167
                }
168 1
                $this->connection = DriverManager::getConnection($params);
169 15
            } elseif (file_exists('migrations-db.php')) {
170
                $params = include 'migrations-db.php';
171
                if (!is_array($params)) {
172
                    throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
173
                }
174
                $this->connection = DriverManager::getConnection($params);
175 14
            } elseif ($this->getHelperSet()->has('connection')) {
176 12
                $this->connection = $this->getHelper('connection')->getConnection();
177 14
            } elseif ($this->configuration) {
178 1
                $this->connection = $this->configuration->getConnection();
179 1
            } else {
180 1
                throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.');
181
            }
182 14
        }
183
184 14
        return $this->connection;
185
    }
186
187
}
188