Failed Conditions
Pull Request — master (#944)
by Asmir
02:33
created

DoctrineCommand::procOpen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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