Completed
Pull Request — master (#562)
by Oliver
04:48 queued 02:59
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

6 Methods

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