Failed Conditions
Pull Request — master (#923)
by
unknown
11:18
created

DoctrineCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 78.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 98
ccs 43
cts 55
cp 0.7818
rs 10
c 1
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A askConfirmation() 0 9 1
A configure() 0 19 2
A outputHeader() 0 10 1
A getDependencyFactory() 0 7 2
A initialize() 0 19 4
A canExecute() 0 6 2
A procOpen() 0 3 1
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 42
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
33
    {
34 42
        parent::__construct($name);
35 42
        $this->dependencyFactory = $dependencyFactory;
36 42
    }
37
38 42
    protected function configure() : void
39
    {
40 42
        if ($this->dependencyFactory !== null) {
41
            return;
42
        }
43
44 42
        $this->addOption(
45 42
            'configuration',
46 42
            null,
47 42
            InputOption::VALUE_REQUIRED,
48 42
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
49
        );
50
51 42
        $this->addOption(
52 42
            'db-configuration',
53 42
            null,
54 42
            InputOption::VALUE_REQUIRED,
55 42
            'The path to a database connection configuration file.',
56 42
            'migrations-db.php'
57
        );
58 42
    }
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 36
    protected function getDependencyFactory() : DependencyFactory
94
    {
95 36
        if ($this->dependencyFactory === null) {
96
            throw DependenciesNotSatisfied::new();
97
        }
98
99 36
        return $this->dependencyFactory;
100
    }
101
102 2
    protected function askConfirmation(
103
        string $question,
104
        InputInterface $input,
105
        OutputInterface $output
106
    ) : bool {
107 2
        return $this->getHelper('question')->ask(
108 2
            $input,
109 2
            $output,
110 2
            new ConfirmationQuestion($question)
111
        );
112
    }
113
114
    protected function canExecute(
115
        string $question,
116
        InputInterface $input,
117
        OutputInterface $output
118
    ) : bool {
119
        return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
120
    }
121
122
    protected function procOpen(string $editorCommand, string $path) : void
123
    {
124
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
125
    }
126
}
127