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

DoctrineCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 38
c 3
b 0
f 0
dl 0
loc 75
ccs 36
cts 44
cp 0.8182
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A canExecute() 0 3 2
A configure() 0 19 2
A initialize() 0 25 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\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