Passed
Pull Request — master (#959)
by Asmir
02:10
created

DoctrineCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 82.98%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 84
ccs 39
cts 47
cp 0.8298
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 19 2
A askConfirmation() 0 9 1
A canExecute() 0 6 2
A initialize() 0 22 5
A getDependencyFactory() 0 7 2
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 53
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
29
    {
30 53
        parent::__construct($name);
31 53
        $this->dependencyFactory = $dependencyFactory;
32 53
    }
33
34 53
    protected function configure() : void
35
    {
36 53
        $this->addOption(
37 53
            'configuration',
38 53
            null,
39 53
            InputOption::VALUE_REQUIRED,
40 53
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
41
        );
42
43 53
        if ($this->dependencyFactory !== null) {
44
            return;
45
        }
46
47 53
        $this->addOption(
48 53
            'db-configuration',
49 53
            null,
50 53
            InputOption::VALUE_REQUIRED,
51 53
            'The path to a database connection configuration file.',
52 53
            'migrations-db.php'
53
        );
54 53
    }
55
56 52
    protected function initialize(InputInterface $input, OutputInterface $output) : void
57
    {
58 52
        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 52
        } elseif (is_string($input->getOption('configuration'))) {
67 1
            $configurationLoader = new ConfigurationFileWithFallback($input->getOption('configuration'));
68 1
            $this->dependencyFactory->setConfigurationLoader($configurationLoader);
69
        }
70
71 52
        if ($this->dependencyFactory->isFrozen()) {
72 23
            return;
73
        }
74
75 29
        $logger = new ConsoleLogger($output);
76 29
        $this->dependencyFactory->setService(LoggerInterface::class, $logger);
77 29
        $this->dependencyFactory->freeze();
78 29
    }
79
80 47
    protected function getDependencyFactory() : DependencyFactory
81
    {
82 47
        if ($this->dependencyFactory === null) {
83
            throw DependenciesNotSatisfied::new();
84
        }
85
86 47
        return $this->dependencyFactory;
87
    }
88
89 14
    protected function askConfirmation(
90
        string $question,
91
        InputInterface $input,
92
        OutputInterface $output
93
    ) : bool {
94 14
        return $this->getHelper('question')->ask(
95 14
            $input,
96 14
            $output,
97 14
            new ConfirmationQuestion($question)
98
        );
99
    }
100
101 14
    protected function canExecute(
102
        string $question,
103
        InputInterface $input,
104
        OutputInterface $output
105
    ) : bool {
106 14
        return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
107
    }
108
}
109