Completed
Push — master ( 53133f...79ff97 )
by Asmir
16s queued 13s
created

DoctrineCommand::canExecute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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