Failed Conditions
Pull Request — master (#933)
by Asmir
03:12
created

DoctrineCommand::outputHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 8
cts 8
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
22
/**
23
 * The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
24
 */
25
abstract class DoctrineCommand extends Command
26
{
27
    /** @var DependencyFactory|null */
28
    private $dependencyFactory;
29
30 49
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
31
    {
32 49
        parent::__construct($name);
33 49
        $this->dependencyFactory = $dependencyFactory;
34 49
    }
35
36 49
    protected function configure() : void
37
    {
38 49
        if ($this->dependencyFactory !== null) {
39
            return;
40
        }
41
42 49
        $this->addOption(
43 49
            'configuration',
44 49
            null,
45 49
            InputOption::VALUE_REQUIRED,
46 49
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
47
        );
48
49 49
        $this->addOption(
50 49
            'db-configuration',
51 49
            null,
52 49
            InputOption::VALUE_REQUIRED,
53 49
            'The path to a database connection configuration file.',
54 49
            'migrations-db.php'
55
        );
56 49
    }
57
58 41
    protected function initialize(InputInterface $input, OutputInterface $output) : void
59
    {
60 41
        if ($this->dependencyFactory === null) {
61
            $configurationLoader     = new ConfigurationFileWithFallback(
62
                is_string($input->getOption('configuration'))
63
                    ? $input->getOption('configuration')
64
                    : null
65
            );
66
            $connectionLoader        = new ConfigurationFile((string) $input->getOption('db-configuration'));
67
            $this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
68
        }
69
70 41
        if ($this->dependencyFactory->isFrozen()) {
71 1
            return;
72
        }
73
74 41
        $logger = new ConsoleLogger($output);
75 41
        $this->dependencyFactory->setService(LoggerInterface::class, $logger);
76 41
        $this->dependencyFactory->freeze();
77 41
    }
78
79 43
    protected function getDependencyFactory() : DependencyFactory
80
    {
81 43
        if ($this->dependencyFactory === null) {
82
            throw DependenciesNotSatisfied::new();
83
        }
84
85 43
        return $this->dependencyFactory;
86
    }
87
88 4
    protected function askConfirmation(
89
        string $question,
90
        InputInterface $input,
91
        OutputInterface $output
92
    ) : bool {
93 4
        return $this->getHelper('question')->ask(
94 4
            $input,
95 4
            $output,
96 4
            new ConfirmationQuestion($question)
97
        );
98
    }
99
100 3
    protected function canExecute(
101
        string $question,
102
        InputInterface $input,
103
        OutputInterface $output
104
    ) : bool {
105 3
        return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
106
    }
107
108 1
    protected function procOpen(string $editorCommand, string $path) : void
109
    {
110 1
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
111 1
    }
112
}
113