Passed
Pull Request — master (#739)
by Michael
02:38
created

AbstractCommand::getMigrationConfiguration()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 29
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.1448
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\DependencyFactory;
10
use Doctrine\Migrations\MigrationRepository;
11
use Doctrine\Migrations\Tools\Console\ConnectionLoader;
12
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelper;
13
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelperInterface;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\HelperSet;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use function escapeshellarg;
21
use function proc_open;
22
use function str_repeat;
23
use function strlen;
24
25
/**
26
 * The AbstractCommand class provides base functionality for the other migrations commands to extend from.
27
 */
28
abstract class AbstractCommand extends Command
29
{
30
    /** @var Configuration */
31
    protected $configuration;
32
33
    /** @var Connection */
34
    protected $connection;
35
36
    /** @var DependencyFactory */
37
    protected $dependencyFactory;
38
39
    /** @var MigrationRepository */
40
    protected $migrationRepository;
41
42
    /** @var Configuration|null */
43
    protected $migrationConfiguration;
44
45 22
    public function setMigrationConfiguration(Configuration $configuration) : void
46
    {
47 22
        $this->configuration = $configuration;
48
49 22
        $this->initializeDependencies();
50 22
    }
51
52 1
    public function setConnection(Connection $connection) : void
53
    {
54 1
        $this->connection = $connection;
55 1
    }
56
57 17
    public function setDependencyFactory(DependencyFactory $dependencyFactory) : void
58
    {
59 17
        $this->dependencyFactory = $dependencyFactory;
60 17
    }
61
62 23
    public function setMigrationRepository(MigrationRepository $migrationRepository) : void
63
    {
64 23
        $this->migrationRepository = $migrationRepository;
65 23
    }
66
67 24
    public function initialize(
68
        InputInterface $input,
69
        OutputInterface $output
70
    ) : void {
71 24
        $this->configuration = $this->getMigrationConfiguration($input, $output);
72
73 24
        $this->initializeDependencies();
74
75 24
        $this->configuration->validate();
76 24
        $this->configuration->createMigrationTable();
77 24
    }
78
79 59
    protected function configure() : void
80
    {
81 59
        $this->addOption(
82 59
            'configuration',
83 59
            null,
84 59
            InputOption::VALUE_OPTIONAL,
85 59
            'The path to a migrations configuration file.'
86
        );
87
88 59
        $this->addOption(
89 59
            'db-configuration',
90 59
            null,
91 59
            InputOption::VALUE_OPTIONAL,
92 59
            'The path to a database connection configuration file.'
93
        );
94 59
    }
95
96 13
    protected function outputHeader(
97
        OutputInterface $output
98
    ) : void {
99 13
        $name = $this->configuration->getName();
100 13
        $name = $name ?? 'Doctrine Database Migrations';
101 13
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
102 13
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
103 13
        $output->writeln('<question>' . $name . '</question>');
104 13
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
105 13
        $output->writeln('');
106 13
    }
107
108 20
    protected function getMigrationConfiguration(
109
        InputInterface $input,
110
        OutputInterface $output
111
    ) : Configuration {
112 20
        if ($this->migrationConfiguration === null) {
113 20
            if ($this->hasConfigurationHelper()) {
114
                /** @var ConfigurationHelper $configHelper */
115 1
                $configHelper = $this->getHelperSet()->get('configuration');
116
            } else {
117 19
                $configHelper = new ConfigurationHelper(
118 19
                    $this->getConnection($input),
119 18
                    $this->configuration
120
                );
121
            }
122
123 19
            $this->migrationConfiguration = $configHelper->getMigrationConfig($input);
124
125 19
            $this->migrationConfiguration->getOutputWriter()->setCallback(
126
                function (string $message) use ($output) : void {
127 1
                    $output->writeln($message);
128 19
                }
129
            );
130
        }
131
132 19
        if ($this->migrationConfiguration === null && $this->configuration !== null) {
133
            $this->migrationConfiguration = $this->configuration;
134
        }
135
136 19
        return $this->migrationConfiguration;
137
    }
138
139 1
    protected function askConfirmation(
140
        string $question,
141
        InputInterface $input,
142
        OutputInterface $output
143
    ) : bool {
144 1
        return $this->getHelper('question')->ask(
145 1
            $input,
146 1
            $output,
147 1
            new ConfirmationQuestion($question)
148
        );
149
    }
150
151 1
    protected function canExecute(
152
        string $question,
153
        InputInterface $input,
154
        OutputInterface $output
155
    ) : bool {
156 1
        if ($input->isInteractive() && ! $this->askConfirmation($question, $input, $output)) {
157
            return false;
158
        }
159
160 1
        return true;
161
    }
162
163 1
    protected function procOpen(string $editorCommand, string $path) : void
164
    {
165 1
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
166 1
    }
167
168 46
    private function initializeDependencies() : void
169
    {
170 46
        $this->connection          = $this->configuration->getConnection();
171 46
        $this->dependencyFactory   = $this->configuration->getDependencyFactory();
172 46
        $this->migrationRepository = $this->dependencyFactory->getMigrationRepository();
173 46
    }
174
175 20
    private function hasConfigurationHelper() : bool
176
    {
177
        /** @var HelperSet|null $helperSet */
178 20
        $helperSet = $this->getHelperSet();
179
180 20
        if ($helperSet === null) {
181 1
            return false;
182
        }
183
184 19
        if (! $helperSet->has('configuration')) {
185 18
            return false;
186
        }
187
188 1
        return $helperSet->get('configuration') instanceof ConfigurationHelperInterface;
189
    }
190
191 19
    private function getConnection(InputInterface $input) : Connection
192
    {
193 19
        if ($this->connection === null) {
194 14
            $this->connection = (new ConnectionLoader($this->configuration))
195 14
                ->getConnection($input, $this->getHelperSet());
196
        }
197
198 18
        return $this->connection;
199
    }
200
}
201