Completed
Pull Request — master (#602)
by Tom
09:02
created

CliConfigurator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 12
dl 0
loc 124
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0
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\Command\Command;
15
use Symfony\Component\Console\Helper\DialogHelper;
16
use Symfony\Component\Console\Helper\QuestionHelper;
17
use Symfony\Component\Console\Input\ArgvInput;
18
use Symfony\Component\Console\Input\InputOption;
19
use function assert;
20
use function class_exists;
21
22
/**
23
 * @link    www.doctrine-project.org
24
 */
25
class CliConfigurator
26
{
27
    private $defaultObjectManagerName = 'doctrine.entitymanager.orm_default';
28
29
    private $commands = [
30
        'doctrine.dbal_cmd.runsql',
31
        'doctrine.dbal_cmd.import',
32
        'doctrine.orm_cmd.clear_cache_metadata',
33
        'doctrine.orm_cmd.clear_cache_result',
34
        'doctrine.orm_cmd.clear_cache_query',
35
        'doctrine.orm_cmd.schema_tool_create',
36
        'doctrine.orm_cmd.schema_tool_update',
37
        'doctrine.orm_cmd.schema_tool_drop',
38
        'doctrine.orm_cmd.ensure_production_settings',
39
        'doctrine.orm_cmd.convert_d1_schema',
40
        'doctrine.orm_cmd.generate_repositories',
41
        'doctrine.orm_cmd.generate_entities',
42
        'doctrine.orm_cmd.generate_proxies',
43
        'doctrine.orm_cmd.convert_mapping',
44
        'doctrine.orm_cmd.run_dql',
45
        'doctrine.orm_cmd.validate_schema',
46
        'doctrine.orm_cmd.info',
47
    ];
48
49
    private $migrationCommands = [
50
        'doctrine.migrations_cmd.execute',
51
        'doctrine.migrations_cmd.generate',
52
        'doctrine.migrations_cmd.migrate',
53
        'doctrine.migrations_cmd.status',
54
        'doctrine.migrations_cmd.version',
55
        'doctrine.migrations_cmd.diff',
56
        'doctrine.migrations_cmd.latest',
57
    ];
58
59
    private ContainerInterface $container;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
60
61 18
    public function __construct(ContainerInterface $container)
62
    {
63 18
        $this->container = $container;
64 18
    }
65
66 18
    public function configure(Application $cli) : void
67
    {
68 18
        $commands = $this->getAvailableCommands();
69 18
        foreach ($commands as $commandName) {
70
            $command = $this->container->get($commandName);
71 18
            assert($command instanceof Command);
72 18
            $command->getDefinition()->addOption($this->createObjectManagerInputOption());
73
74 18
            $cli->add($command);
75
        }
76
77
        $objectManager = $this->container->get($this->getObjectManagerName());
78 18
        assert($objectManager instanceof EntityManagerInterface);
79
80 18
        $helpers = $this->getHelpers($objectManager);
81 18
        foreach ($helpers as $name => $instance) {
82 18
            $cli->getHelperSet()->set($instance, $name);
83
        }
84 18
    }
85
86
    /**
87
     * @return array
88
     */
89
    private function getHelpers(EntityManagerInterface $objectManager) : array
90 18
    {
91
        $helpers = [];
92 18
93
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
94 18
            $helpers['dialog'] = new QuestionHelper();
95 18
        } else {
96
            $helpers['dialog'] = new DialogHelper();
97
        }
98
99
        $helpers['db'] = new ConnectionHelper($objectManager->getConnection());
100 18
        $helpers['em'] = new EntityManagerHelper($objectManager);
101 18
102
        return $helpers;
103 18
    }
104
105
    private function createObjectManagerInputOption() : InputOption
106
    {
107
        return new InputOption(
108
            'object-manager',
109 18
            null,
110
            InputOption::VALUE_OPTIONAL,
111 18
            'The name of the object manager to use.',
112 18
            $this->defaultObjectManagerName
113 18
        );
114 18
    }
115 18
116 18
    private function getObjectManagerName() : string
117
    {
118
        $arguments = new ArgvInput();
119
120
        if (! $arguments->hasParameterOption('--object-manager')) {
121
            return $this->defaultObjectManagerName;
122
        }
123 18
124
        return $arguments->getParameterOption('--object-manager');
125 18
    }
126
127 18
    /**
128 17
     * @return array
129
     */
130
    private function getAvailableCommands() : array
131 1
    {
132
        if (class_exists(VersionCommand::class)) {
133
            return ArrayUtils::merge($this->commands, $this->migrationCommands);
134
        }
135
136
        return $this->commands;
137 18
    }
138
}
139