Completed
Push — master ( 368556...6bf94b )
by Jonathan
11s
created

AbstractCommand::canExecute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

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