Completed
Pull Request — develop (#607)
by Tom
01:36
created

CliConfigurator::getObjectManagerName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule;
6
7
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
8
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
11
use Interop\Container\ContainerInterface;
12
use Laminas\Stdlib\ArrayUtils;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Helper\DialogHelper;
15
use Symfony\Component\Console\Helper\QuestionHelper;
16
use Symfony\Component\Console\Input\ArgvInput;
17
use Symfony\Component\Console\Input\InputOption;
18
use function class_exists;
19
20
class CliConfigurator
21
{
22
    /** @var string */
23
    private $defaultObjectManagerName = 'doctrine.entitymanager.orm_default';
24
25
    /** @var string[] */
26
    private $commands = [
27
        'doctrine.dbal_cmd.runsql',
28
        'doctrine.dbal_cmd.import',
29
        'doctrine.orm_cmd.clear_cache_metadata',
30
        'doctrine.orm_cmd.clear_cache_result',
31
        'doctrine.orm_cmd.clear_cache_query',
32
        'doctrine.orm_cmd.schema_tool_create',
33
        'doctrine.orm_cmd.schema_tool_update',
34
        'doctrine.orm_cmd.schema_tool_drop',
35
        'doctrine.orm_cmd.ensure_production_settings',
36
        'doctrine.orm_cmd.convert_d1_schema',
37
        'doctrine.orm_cmd.generate_repositories',
38
        'doctrine.orm_cmd.generate_entities',
39
        'doctrine.orm_cmd.generate_proxies',
40
        'doctrine.orm_cmd.convert_mapping',
41
        'doctrine.orm_cmd.run_dql',
42
        'doctrine.orm_cmd.validate_schema',
43
        'doctrine.orm_cmd.info',
44
    ];
45
46
    /** @var string[] */
47
    private $migrationCommands = [
48
        'doctrine.migrations_cmd.execute',
49
        'doctrine.migrations_cmd.generate',
50
        'doctrine.migrations_cmd.migrate',
51
        'doctrine.migrations_cmd.status',
52
        'doctrine.migrations_cmd.version',
53
        'doctrine.migrations_cmd.diff',
54
        'doctrine.migrations_cmd.latest',
55
    ];
56
57
    /** @var ContainerInterface */
58
    private $container;
59
60
    public function __construct(ContainerInterface $container)
61
    {
62
        $this->container = $container;
63
    }
64
65
    public function configure(Application $cli) : void
66
    {
67
        $commands = $this->getAvailableCommands();
68
        foreach ($commands as $commandName) {
69
            $command = $this->container->get($commandName);
70
            $command->getDefinition()->addOption($this->createObjectManagerInputOption());
71
72
            $cli->add($command);
73
        }
74
75
        $objectManager = $this->container->get($this->getObjectManagerName());
76
77
        $helpers = $this->getHelpers($objectManager);
78
        foreach ($helpers as $name => $instance) {
79
            $cli->getHelperSet()->set($instance, $name);
80
        }
81
    }
82
83
    /**
84
     * @return mixed[]
85
     */
86
    private function getHelpers(EntityManagerInterface $objectManager) : array
87
    {
88
        $helpers = [];
89
90
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
91
            $helpers['dialog'] = new QuestionHelper();
92
        } else {
93
            $helpers['dialog'] = new DialogHelper();
94
        }
95
96
        $helpers['db'] = new ConnectionHelper($objectManager->getConnection());
97
        $helpers['em'] = new EntityManagerHelper($objectManager);
98
99
        return $helpers;
100
    }
101
102
    private function createObjectManagerInputOption() : InputOption
103
    {
104
        return new InputOption(
105
            'object-manager',
106
            null,
107
            InputOption::VALUE_OPTIONAL,
108
            'The name of the object manager to use.',
109
            $this->defaultObjectManagerName
110
        );
111
    }
112
113
    private function getObjectManagerName() : string
114
    {
115
        $arguments = new ArgvInput();
116
117
        if (! $arguments->hasParameterOption('--object-manager')) {
118
            return $this->defaultObjectManagerName;
119
        }
120
121
        return $arguments->getParameterOption('--object-manager');
122
    }
123
124
    /**
125
     * @return mixed[]
126
     */
127
    private function getAvailableCommands() : array
128
    {
129
        if (class_exists(VersionCommand::class)) {
130
            return ArrayUtils::merge($this->commands, $this->migrationCommands);
131
        }
132
133
        return $this->commands;
134
    }
135
}
136