Completed
Push — master ( 5cf5b8...189f41 )
by Andreas
05:29 queued 05:22
created

CliConfigurator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 12
dl 0
loc 124
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 19 3
A getHelpers() 0 15 2
A createObjectManagerInputOption() 0 10 1
A getObjectManagerName() 0 10 2
A getAvailableCommands() 0 8 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule;
21
22
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
23
use Doctrine\ORM\EntityManagerInterface;
24
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
25
use Interop\Container\ContainerInterface;
26
use Symfony\Component\Console\Application;
27
use Symfony\Component\Console\Helper\DialogHelper;
28
use Symfony\Component\Console\Helper\QuestionHelper;
29
use Symfony\Component\Console\Input\ArgvInput;
30
use Symfony\Component\Console\Input\InputOption;
31
use Zend\Stdlib\ArrayUtils;
32
33
/**
34
 * @license MIT
35
 * @link    www.doctrine-project.org
36
 * @author  Nicolas Eeckeloo <[email protected]>
37
 */
38
class CliConfigurator
39
{
40
    private $defaultObjectManagerName = 'doctrine.entitymanager.orm_default';
41
42
    private $commands = [
43
        'doctrine.dbal_cmd.runsql',
44
        'doctrine.dbal_cmd.import',
45
        'doctrine.orm_cmd.clear_cache_metadata',
46
        'doctrine.orm_cmd.clear_cache_result',
47
        'doctrine.orm_cmd.clear_cache_query',
48
        'doctrine.orm_cmd.schema_tool_create',
49
        'doctrine.orm_cmd.schema_tool_update',
50
        'doctrine.orm_cmd.schema_tool_drop',
51
        'doctrine.orm_cmd.ensure_production_settings',
52
        'doctrine.orm_cmd.convert_d1_schema',
53
        'doctrine.orm_cmd.generate_repositories',
54
        'doctrine.orm_cmd.generate_entities',
55
        'doctrine.orm_cmd.generate_proxies',
56
        'doctrine.orm_cmd.convert_mapping',
57
        'doctrine.orm_cmd.run_dql',
58
        'doctrine.orm_cmd.validate_schema',
59
        'doctrine.orm_cmd.info',
60
    ];
61
62
    private $migrationCommands = [
63
        'doctrine.migrations_cmd.execute',
64
        'doctrine.migrations_cmd.generate',
65
        'doctrine.migrations_cmd.migrate',
66
        'doctrine.migrations_cmd.status',
67
        'doctrine.migrations_cmd.version',
68
        'doctrine.migrations_cmd.diff',
69
        'doctrine.migrations_cmd.latest',
70
    ];
71
72
    /**
73
     * @var ContainerInterface
74
     */
75
    private $container;
76
77 72
    public function __construct(ContainerInterface $container)
78
    {
79 72
        $this->container = $container;
80 72
    }
81
82 18
    public function configure(Application $cli)
83
    {
84 18
        $commands = $this->getAvailableCommands();
85 18
        foreach ($commands as $commandName) {
86
            /* @var $command \Symfony\Component\Console\Command\Command */
87 18
            $command = $this->container->get($commandName);
88 18
            $command->getDefinition()->addOption($this->createObjectManagerInputOption());
89
90 18
            $cli->add($command);
91 18
        }
92
93
        /* @var $objectManager \Doctrine\ORM\EntityManagerInterface */
94 18
        $objectManager = $this->container->get($this->getObjectManagerName());
95
96 18
        $helpers = $this->getHelpers($objectManager);
97 18
        foreach ($helpers as $name => $instance) {
98 18
            $cli->getHelperSet()->set($instance, $name);
99 18
        }
100 18
    }
101
102
    /**
103
     * @param EntityManagerInterface $objectManager
104
     * @return array
105
     */
106 18
    private function getHelpers(EntityManagerInterface $objectManager)
107
    {
108 18
        $helpers = [];
109
110 18
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
111 18
            $helpers['dialog'] = new QuestionHelper();
112 18
        } else {
113
            $helpers['dialog'] = new DialogHelper();
114
        }
115
116 18
        $helpers['db'] = new ConnectionHelper($objectManager->getConnection());
117 18
        $helpers['em'] = new EntityManagerHelper($objectManager);
118
119 18
        return $helpers;
120
    }
121
122
    /**
123
     * @return InputOption
124
     */
125 18
    private function createObjectManagerInputOption()
126
    {
127 18
        return new InputOption(
128 18
            'object-manager',
129 18
            null,
130 18
            InputOption::VALUE_OPTIONAL,
131 18
            'The name of the object manager to use.',
132 18
            $this->defaultObjectManagerName
133 18
        );
134
    }
135
136
    /**
137
     * @return string
138
     */
139 18
    private function getObjectManagerName()
140
    {
141 18
        $arguments = new ArgvInput();
142
143 18
        if (!$arguments->hasParameterOption('--object-manager')) {
144 17
            return $this->defaultObjectManagerName;
145
        }
146
147 1
        return $arguments->getParameterOption('--object-manager');
148
    }
149
150
    /**
151
     * @return array
152
     */
153 18
    private function getAvailableCommands()
154
    {
155 18
        if (class_exists('Doctrine\\DBAL\\Migrations\\Version')) {
156 18
            return ArrayUtils::merge($this->commands, $this->migrationCommands);
157
        }
158
159
        return $this->commands;
160
    }
161
}
162