Completed
Pull Request — master (#968)
by Edi
02:42
created

DoctrineCommand::askConfirmation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A DoctrineCommand::canExecute() 0 3 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\Style\StyleInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
use function is_string;
20
21
/**
22
 * The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
23
 */
24
abstract class DoctrineCommand extends Command
25
{
26
    /** @var DependencyFactory|null */
27
    private $dependencyFactory;
28
29
    /** @var StyleInterface */
30
    protected $io;
31
32 56
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
33
    {
34 56
        parent::__construct($name);
35 56
        $this->dependencyFactory = $dependencyFactory;
36 56
    }
37
38 56
    protected function configure() : void
39
    {
40 56
        $this->addOption(
41 56
            'configuration',
42 56
            null,
43 56
            InputOption::VALUE_REQUIRED,
44 56
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
45
        );
46
47 56
        if ($this->dependencyFactory !== null) {
48
            return;
49
        }
50
51 56
        $this->addOption(
52 56
            'db-configuration',
53 56
            null,
54 56
            InputOption::VALUE_REQUIRED,
55 56
            'The path to a database connection configuration file.',
56 56
            'migrations-db.php'
57
        );
58 56
    }
59
60 56
    protected function initialize(InputInterface $input, OutputInterface $output) : void
61
    {
62 56
        $this->io = new SymfonyStyle($input, $output);
63
64 56
        $configurationParameter = $input->getOption('configuration');
65 56
        if ($this->dependencyFactory === null) {
66
            $configurationLoader     = new ConfigurationFileWithFallback(
67
                is_string($configurationParameter)
68
                    ? $configurationParameter
69
                    : null
70
            );
71
            $connectionLoader        = new ConfigurationFile((string) $input->getOption('db-configuration'));
72
            $this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
73 56
        } elseif (is_string($configurationParameter)) {
74 1
            $configurationLoader = new ConfigurationFileWithFallback($configurationParameter);
75 1
            $this->dependencyFactory->setConfigurationLoader($configurationLoader);
76
        }
77
78 56
        if ($this->dependencyFactory->isFrozen()) {
79 26
            return;
80
        }
81
82 30
        $logger = new ConsoleLogger($output);
83 30
        $this->dependencyFactory->setService(LoggerInterface::class, $logger);
84 30
        $this->dependencyFactory->freeze();
85 30
    }
86
87 50
    protected function getDependencyFactory() : DependencyFactory
88
    {
89 50
        if ($this->dependencyFactory === null) {
90
            throw DependenciesNotSatisfied::new();
91
        }
92
93 50
        return $this->dependencyFactory;
94
    }
95
96 14
    protected function canExecute(string $question, InputInterface $input) : bool
97
    {
98 14
        return ! $input->isInteractive() || $this->io->confirm($question);
99
    }
100
}
101