Completed
Pull Request — master (#602)
by Tom
06:21
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 string $defaultObjectManagerName = 'doctrine.entitymanager.orm_default';
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...
28
29
    /** @var string[] An array of functions. */
30
    private array $commands = [
31
        'doctrine.dbal_cmd.runsql',
32
        'doctrine.dbal_cmd.import',
33
        'doctrine.orm_cmd.clear_cache_metadata',
34
        'doctrine.orm_cmd.clear_cache_result',
35
        'doctrine.orm_cmd.clear_cache_query',
36
        'doctrine.orm_cmd.schema_tool_create',
37
        'doctrine.orm_cmd.schema_tool_update',
38
        'doctrine.orm_cmd.schema_tool_drop',
39
        'doctrine.orm_cmd.ensure_production_settings',
40
        'doctrine.orm_cmd.convert_d1_schema',
41
        'doctrine.orm_cmd.generate_repositories',
42
        'doctrine.orm_cmd.generate_entities',
43
        'doctrine.orm_cmd.generate_proxies',
44
        'doctrine.orm_cmd.convert_mapping',
45
        'doctrine.orm_cmd.run_dql',
46
        'doctrine.orm_cmd.validate_schema',
47
        'doctrine.orm_cmd.info',
48
    ];
49
50
    /** @var string[] An array of functions. */
51
    private array $migrationCommands = [
52
        'doctrine.migrations_cmd.execute',
53
        'doctrine.migrations_cmd.generate',
54
        'doctrine.migrations_cmd.migrate',
55
        'doctrine.migrations_cmd.status',
56
        'doctrine.migrations_cmd.version',
57
        'doctrine.migrations_cmd.diff',
58
        'doctrine.migrations_cmd.latest',
59
    ];
60
61 18
    private ContainerInterface $container;
62
63 18
    public function __construct(ContainerInterface $container)
64 18
    {
65
        $this->container = $container;
66 18
    }
67
68 18
    public function configure(Application $cli) : void
69 18
    {
70
        $commands = $this->getAvailableCommands();
71 18
        foreach ($commands as $commandName) {
72 18
            $command = $this->container->get($commandName);
73
            assert($command instanceof Command);
74 18
            $command->getDefinition()->addOption($this->createObjectManagerInputOption());
75
76
            $cli->add($command);
77
        }
78 18
79
        $objectManager = $this->container->get($this->getObjectManagerName());
80 18
        assert($objectManager instanceof EntityManagerInterface);
81 18
82 18
        $helpers = $this->getHelpers($objectManager);
83
        foreach ($helpers as $name => $instance) {
84 18
            $cli->getHelperSet()->set($instance, $name);
85
        }
86
    }
87
88
    /**
89
     * @return mixed[]
90 18
     */
91
    private function getHelpers(EntityManagerInterface $objectManager) : array
92 18
    {
93
        $helpers = [];
94 18
95 18
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
96
            $helpers['dialog'] = new QuestionHelper();
97
        } else {
98
            $helpers['dialog'] = new DialogHelper();
99
        }
100 18
101 18
        $helpers['db'] = new ConnectionHelper($objectManager->getConnection());
102
        $helpers['em'] = new EntityManagerHelper($objectManager);
103 18
104
        return $helpers;
105
    }
106
107
    private function createObjectManagerInputOption() : InputOption
108
    {
109 18
        return new InputOption(
110
            'object-manager',
111 18
            null,
112 18
            InputOption::VALUE_OPTIONAL,
113 18
            'The name of the object manager to use.',
114 18
            $this->defaultObjectManagerName
115 18
        );
116 18
    }
117
118
    private function getObjectManagerName() : string
119
    {
120
        $arguments = new ArgvInput();
121
122
        if (! $arguments->hasParameterOption('--object-manager')) {
123 18
            return $this->defaultObjectManagerName;
124
        }
125 18
126
        return $arguments->getParameterOption('--object-manager');
127 18
    }
128 17
129
    /**
130
     * @return string[]
131 1
     */
132
    private function getAvailableCommands() : array
133
    {
134
        if (class_exists(VersionCommand::class)) {
135
            return ArrayUtils::merge($this->commands, $this->migrationCommands);
136
        }
137 18
138
        return $this->commands;
139 18
    }
140
}
141