Completed
Pull Request — master (#917)
by Asmir
02:37
created

DoctrineCommand::askConfirmation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\Configuration\Connection\ConfigurationFile;
8
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
9
use Doctrine\Migrations\DependencyFactory;
10
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
11
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
use function escapeshellarg;
19
use function is_string;
20
use function proc_open;
21
use function str_repeat;
22
use function strlen;
23
24
/**
25
 * The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
26
 */
27
abstract class DoctrineCommand extends Command
28
{
29
    /** @var DependencyFactory|null */
30
    private $dependencyFactory;
31
32 49
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
33
    {
34 49
        parent::__construct($name);
35 49
        $this->dependencyFactory = $dependencyFactory;
36 49
    }
37
38 49
    protected function configure() : void
39
    {
40 49
        if ($this->dependencyFactory !== null) {
41
            return;
42
        }
43
44 49
        $this->addOption(
45 49
            'configuration',
46 49
            null,
47 49
            InputOption::VALUE_REQUIRED,
48 49
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
49
        );
50
51 49
        $this->addOption(
52 49
            'db-configuration',
53 49
            null,
54 49
            InputOption::VALUE_REQUIRED,
55 49
            'The path to a database connection configuration file.',
56 49
            'migrations-db.php'
57
        );
58 49
    }
59
60 12
    protected function outputHeader(
61
        OutputInterface $output
62
    ) : void {
63 12
        $name = $this->getDependencyFactory()->getConfiguration()->getName();
64 12
        $name = $name ?? 'Doctrine Database Migrations';
65 12
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
66 12
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
67 12
        $output->writeln('<question>' . $name . '</question>');
68 12
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
69 12
        $output->writeln('');
70 12
    }
71
72 41
    protected function initialize(InputInterface $input, OutputInterface $output) : void
73
    {
74 41
        if ($this->dependencyFactory === null) {
75
            $configurationLoader     = new ConfigurationFileWithFallback(
76
                is_string($input->getOption('configuration'))
77
                    ? $input->getOption('configuration')
78
                    : null
79
            );
80
            $connectionLoader        = new ConfigurationFile((string) $input->getOption('db-configuration'));
81
            $this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
82
        }
83
84 41
        if ($this->dependencyFactory->isFrozen()) {
85 1
            return;
86
        }
87
88 41
        $logger = new ConsoleLogger($output);
89 41
        $this->dependencyFactory->setService(LoggerInterface::class, $logger);
90 41
        $this->dependencyFactory->freeze();
91 41
    }
92
93 43
    protected function getDependencyFactory() : DependencyFactory
94
    {
95 43
        if ($this->dependencyFactory === null) {
96
            throw DependenciesNotSatisfied::new();
97
        }
98
99 43
        return $this->dependencyFactory;
100
    }
101
102 4
    protected function askConfirmation(
103
        string $question,
104
        InputInterface $input,
105
        OutputInterface $output
106
    ) : bool {
107 4
        return $this->getHelper('question')->ask(
108 4
            $input,
109 4
            $output,
110 4
            new ConfirmationQuestion($question)
111
        );
112
    }
113
114 3
    protected function canExecute(
115
        string $question,
116
        InputInterface $input,
117
        OutputInterface $output
118
    ) : bool {
119 3
        return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
120
    }
121
122 1
    protected function procOpen(string $editorCommand, string $path) : void
123
    {
124 1
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
125 1
    }
126
}
127