Completed
Push — master ( 90fd96...941231 )
by Jonathan
16s queued 11s
created

AbstractCommand::canExecute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 2
rs 10
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
                static 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
        return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
157
    }
158
159 1
    protected function procOpen(string $editorCommand, string $path) : void
160
    {
161 1
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
162 1
    }
163
164 46
    private function initializeDependencies() : void
165
    {
166 46
        $this->connection          = $this->configuration->getConnection();
167 46
        $this->dependencyFactory   = $this->configuration->getDependencyFactory();
168 46
        $this->migrationRepository = $this->dependencyFactory->getMigrationRepository();
169 46
    }
170
171 20
    private function hasConfigurationHelper() : bool
172
    {
173
        /** @var HelperSet|null $helperSet */
174 20
        $helperSet = $this->getHelperSet();
175
176 20
        if ($helperSet === null) {
177 1
            return false;
178
        }
179
180 19
        if (! $helperSet->has('configuration')) {
181 18
            return false;
182
        }
183
184 1
        return $helperSet->get('configuration') instanceof ConfigurationHelperInterface;
185
    }
186
187 19
    private function getConnection(InputInterface $input) : Connection
188
    {
189 19
        if ($this->connection === null) {
190 14
            $this->connection = (new ConnectionLoader($this->configuration))
191 14
                ->getConnection($input, $this->getHelperSet());
192
        }
193
194 18
        return $this->connection;
195
    }
196
}
197