Completed
Push — master ( c28f13...ab7ebf )
by Asmir
21s queued 11s
created

DoctrineCommand::canExecute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 2
rs 10
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 48
    public function __construct(?string $name = null, ?DependencyFactory $dependencyFactory = null)
35
    {
36 48
        parent::__construct($name);
37 48
        $this->dependencyFactory = $dependencyFactory;
38 48
    }
39
40 48
    protected function configure() : void
41
    {
42 48
        $this->addOption(
43 48
            'configuration',
44 48
            null,
45 48
            InputOption::VALUE_OPTIONAL,
46 48
            'The path to a migrations configuration file.'
47
        );
48
49 48
        $this->addOption(
50 48
            'db-configuration',
51 48
            null,
52 48
            InputOption::VALUE_OPTIONAL,
53 48
            'The path to a database connection configuration file.'
54
        );
55 48
    }
56
57 12
    protected function outputHeader(
58
        OutputInterface $output
59
    ) : void {
60 12
        $name = $this->getDependencyFactory()->getConfiguration()->getName();
61 12
        $name = $name ?? 'Doctrine Database Migrations';
62 12
        $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
63 12
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
64 12
        $output->writeln('<question>' . $name . '</question>');
65 12
        $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
66 12
        $output->writeln('');
67 12
    }
68
69 40
    protected function initialize(InputInterface $input, OutputInterface $output) : void
70
    {
71 40
        if ($this->dependencyFactory !== null) {
72 40
            $this->dependencyFactory->freeze();
73
74 40
            return;
75
        }
76
77
        $helperSet = $this->getHelperSet() ?: new HelperSet();
78
79
        if ($helperSet->has('configuration') && $helperSet->get('configuration') instanceof ConfigurationHelper) {
80
            /** @var MigrationsConfigurationHelper $configHelper */
81
            $configHelper = $helperSet->get('configuration');
82
        } else {
83
            $configHelper = new MigrationsConfigurationHelper();
84
        }
85
86
        $configuration = $configHelper->getConfiguration($input);
87
88
        $dbConfig = is_string($input->getOption('db-configuration')) ? $input->getOption('db-configuration'): null;
89
90
        $connection = (new ConnectionLoader())
91
            ->getConnection($dbConfig, $helperSet);
92
93
        $em = null;
94
        if ($helperSet->has('em') && $helperSet->get('em') instanceof EntityManagerHelper) {
95
            $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

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