Completed
Pull Request — master (#968)
by Edi
02:20
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\Question\ConfirmationQuestion;
0 ignored issues
show
introduced by
Type Symfony\Component\Console\Question\ConfirmationQuestion is not used in this file.
Loading history...
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 \Symfony\Component\Console\Style\StyleInterface */
0 ignored issues
show
introduced by
Class \Symfony\Component\Console\Style\StyleInterface should not be referenced via a fully qualified name, but via a use statement.
Loading history...
30
    protected $io;
31
32 55
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
33
    {
34 55
        parent::__construct($name);
35 55
        $this->dependencyFactory = $dependencyFactory;
36 55
    }
37
38 55
    protected function configure() : void
39
    {
40 55
        if ($this->dependencyFactory !== null) {
41
            return;
42
        }
43
44 55
        $this->addOption(
45 55
            'configuration',
46 55
            null,
47 55
            InputOption::VALUE_REQUIRED,
48 55
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
49
        );
50
51 55
        $this->addOption(
52 55
            'db-configuration',
53 55
            null,
54 55
            InputOption::VALUE_REQUIRED,
55 55
            'The path to a database connection configuration file.',
56 55
            'migrations-db.php'
57
        );
58 55
    }
59
60 55
    protected function initialize(InputInterface $input, OutputInterface $output) : void
61
    {
62 55
        $this->io = new SymfonyStyle($input, $output);
63
64 55
        if ($this->dependencyFactory === null) {
65
            $configurationLoader     = new ConfigurationFileWithFallback(
66
                is_string($input->getOption('configuration'))
67
                    ? $input->getOption('configuration')
68
                    : null
69
            );
70
            $connectionLoader        = new ConfigurationFile((string) $input->getOption('db-configuration'));
71
            $this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
72
        }
73
74 55
        if ($this->dependencyFactory->isFrozen()) {
75 1
            return;
76
        }
77
78 55
        $logger = new ConsoleLogger($output);
79 55
        $this->dependencyFactory->setService(LoggerInterface::class, $logger);
80 55
        $this->dependencyFactory->freeze();
81 55
    }
82
83 49
    protected function getDependencyFactory() : DependencyFactory
84
    {
85 49
        if ($this->dependencyFactory === null) {
86
            throw DependenciesNotSatisfied::new();
87
        }
88
89 49
        return $this->dependencyFactory;
90
    }
91
92 14
    protected function canExecute(string $question, InputInterface $input) : bool
93
    {
94 14
        return ! $input->isInteractive() || $this->io->confirm($question);
95
    }
96
}
97