Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

DoctrineCommand::initializeDependencies()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 42.3281

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 30
ccs 3
cts 16
cp 0.1875
crap 42.3281
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\DependencyFactory;
8
use Doctrine\Migrations\Tools\Console\ConnectionLoader;
9
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
10
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
11
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelper;
12
use Doctrine\Migrations\Tools\Console\Helper\MigrationsConfigurationHelper;
13
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\HelperSet;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use function escapeshellarg;
21
use function is_string;
22
use function proc_open;
23
use function str_repeat;
24
use function strlen;
25
26
/**
27
 * The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
28
 */
29
abstract class DoctrineCommand extends Command
30
{
31
    /** @var DependencyFactory|null */
32
    private $dependencyFactory;
33
34 38
    public function initialize(
35
        InputInterface $input,
36
        OutputInterface $output
37
    ) : void {
38 38
        $this->initializeDependencies($input, $output);
39 38
        $this->getDependencyFactory()->getConfiguration()->validate();
40 38
    }
41
42 43
    public function __construct(?string $name = null, ?DependencyFactory $dependencyFactory = null)
43
    {
44 43
        parent::__construct($name);
45 43
        $this->dependencyFactory = $dependencyFactory;
46 43
    }
47
48 43
    protected function configure() : void
49
    {
50 43
        $this->addOption(
51 43
            'configuration',
52 43
            null,
53 43
            InputOption::VALUE_OPTIONAL,
54 43
            'The path to a migrations configuration file.'
55
        );
56
57 43
        $this->addOption(
58 43
            'db-configuration',
59 43
            null,
60 43
            InputOption::VALUE_OPTIONAL,
61 43
            'The path to a database connection configuration file.'
62
        );
63 43
    }
64
65 12
    protected function outputHeader(
66
        OutputInterface $output
67
    ) : void {
68 12
        $name = $this->getDependencyFactory()->getConfiguration()->getName();
69 12
        $name = $name ?? 'Doctrine Database Migrations';
70 12
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
71 12
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
72 12
        $output->writeln('<question>' . $name . '</question>');
73 12
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
74 12
        $output->writeln('');
75 12
    }
76
77 38
    protected function initializeDependencies(
78
        InputInterface $input,
79
        OutputInterface $output
80
    ) : void {
81 38
        if ($this->dependencyFactory !== null) {
82 38
            return;
83
        }
84
        $helperSet = $this->getHelperSet() ?: new HelperSet();
85
86
        if ($helperSet->has('configuration') && $helperSet->get('configuration') instanceof ConfigurationHelper) {
87
            /** @var MigrationsConfigurationHelper $configHelper */
88
            $configHelper = $helperSet->get('configuration');
89
        } else {
90
            $configHelper = new MigrationsConfigurationHelper();
91
        }
92
93
        $configuration = $configHelper->getConfiguration($input);
94
95
        $dbConfig = is_string($input->getOption('db-configuration')) ? $input->getOption('db-configuration'): null;
96
97
        $connection = (new ConnectionLoader())
98
            ->getConnection($dbConfig, $helperSet);
99
100
        $em = null;
101
        if ($helperSet->has('em') && $helperSet->get('em') instanceof EntityManagerHelper) {
102
            $em = $helperSet->get('em')->getEntityManager();
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Doctrine\ORM\Tools\Conso...per\EntityManagerHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
            $em = $helperSet->get('em')->/** @scrutinizer ignore-call */ getEntityManager();
Loading history...
103
        }
104
105
        $logger                  = new ConsoleLogger($output);
106
        $this->dependencyFactory = new DependencyFactory($configuration, $connection, $em, $logger);
107
    }
108
109 43
    protected function getDependencyFactory() : DependencyFactory
110
    {
111 43
        if ($this->dependencyFactory === null) {
112
            throw DependenciesNotSatisfied::new();
113
        }
114
115 43
        return $this->dependencyFactory;
116
    }
117
118 2
    protected function askConfirmation(
119
        string $question,
120
        InputInterface $input,
121
        OutputInterface $output
122
    ) : bool {
123 2
        return $this->getHelper('question')->ask(
124 2
            $input,
125 2
            $output,
126 2
            new ConfirmationQuestion($question)
127
        );
128
    }
129
130
    protected function canExecute(
131
        string $question,
132
        InputInterface $input,
133
        OutputInterface $output
134
    ) : bool {
135
        return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
136
    }
137
138 1
    protected function procOpen(string $editorCommand, string $path) : void
139
    {
140 1
        proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
141 1
    }
142
}
143