Completed
Pull Request — master (#494)
by Thomas Mauro
28:51
created

Module::initializeConsole()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 57
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 3.0001

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 57
ccs 41
cts 42
cp 0.9762
rs 9.6818
cc 3
eloc 41
nc 4
nop 1
crap 3.0001

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Component\Console\Helper\DialogHelper;
23
use Symfony\Component\Console\Helper\QuestionHelper;
24
use Zend\ModuleManager\Feature\ControllerProviderInterface;
25
use Zend\ModuleManager\Feature\ConfigProviderInterface;
26
use Zend\ModuleManager\Feature\InitProviderInterface;
27
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
28
use Zend\ModuleManager\Feature\RouteProviderInterface;
29
use Zend\ModuleManager\ModuleManagerInterface;
30
use Zend\EventManager\EventInterface;
31
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
32
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
33
use Zend\Stdlib\ArrayUtils;
34
35
/**
36
 * Base module for Doctrine ORM.
37
 *
38
 * @license MIT
39
 * @link    www.doctrine-project.org
40
 * @author  Kyle Spraggs <[email protected]>
41
 * @author  Marco Pivetta <[email protected]>
42
 */
43
class Module implements
44
    ControllerProviderInterface,
45
    ConfigProviderInterface,
46
    InitProviderInterface,
47
    RouteProviderInterface,
48
    DependencyIndicatorInterface
49
{
50
    /**
51 56
     * {@inheritDoc}
52
     */
53 56
    public function init(ModuleManagerInterface $manager)
54
    {
55 56
        $events = $manager->getEventManager();
56 56
        // Initialize logger collector once the profiler is initialized itself
57
        $events->attach(
58
            'profiler_init',
59
            function () use ($manager) {
60 56
                $manager->getEvent()->getParam('ServiceManager')->get('doctrine.sql_logger_collector.orm_default');
0 ignored issues
show
Bug introduced by
The method getEvent() does not exist on Zend\ModuleManager\ModuleManagerInterface. Did you maybe mean getEventManager()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61 56
            }
62 56
        );
63
        $events->getSharedManager()->attach('doctrine', 'loadCli.post', array($this, 'initializeConsole'));
64
    }
65
66
    /**
67 56
     * {@inheritDoc}
68
     */
69 56
    public function getConfig()
70
    {
71
        return include __DIR__ . '/../../config/module.config.php';
72
    }
73
74
    /**
75 56
     * {@inheritDoc}
76
     */
77 56
    public function getControllerConfig()
78
    {
79
        return include __DIR__ . '/../../config/controllers.config.php';
80
    }
81
82
    /**
83 56
     * {@inheritDoc}
84
     */
85 56
    public function getModuleDependencies()
86
    {
87
        return array('DoctrineModule');
88
    }
89
90
    /**
91
     * Initializes the console with additional commands from the ORM, DBAL and (optionally) DBAL\Migrations
92
     *
93
     * @param \Zend\EventManager\EventInterface $event
94
     *
95 2
     * @return void
96
     */
97
    public function initializeConsole(EventInterface $event)
98 2
    {
99
        /* @var $cli \Symfony\Component\Console\Application */
100 2
        $cli            = $event->getTarget();
101
        /* @var $serviceLocator \Zend\ServiceManager\ServiceLocatorInterface */
102
        $serviceLocator = $event->getParam('ServiceManager');
103 2
104 2
        $commands = array(
105 2
            'doctrine.dbal_cmd.runsql',
106 2
            'doctrine.dbal_cmd.import',
107 2
            'doctrine.orm_cmd.clear_cache_metadata',
108 2
            'doctrine.orm_cmd.clear_cache_result',
109 2
            'doctrine.orm_cmd.clear_cache_query',
110 2
            'doctrine.orm_cmd.schema_tool_create',
111 2
            'doctrine.orm_cmd.schema_tool_update',
112 2
            'doctrine.orm_cmd.schema_tool_drop',
113 2
            'doctrine.orm_cmd.ensure_production_settings',
114 2
            'doctrine.orm_cmd.convert_d1_schema',
115 2
            'doctrine.orm_cmd.generate_repositories',
116 2
            'doctrine.orm_cmd.generate_entities',
117 2
            'doctrine.orm_cmd.generate_proxies',
118 2
            'doctrine.orm_cmd.convert_mapping',
119 2
            'doctrine.orm_cmd.run_dql',
120 2
            'doctrine.orm_cmd.validate_schema',
121
            'doctrine.orm_cmd.info',
122 2
        );
123 2
124 2
        if (class_exists('Doctrine\\DBAL\\Migrations\\Version')) {
125
            $commands = ArrayUtils::merge(
126 2
                $commands,
127 2
                array(
128 2
                    'doctrine.migrations_cmd.execute',
129 2
                    'doctrine.migrations_cmd.generate',
130 2
                    'doctrine.migrations_cmd.migrate',
131 2
                    'doctrine.migrations_cmd.status',
132 2
                    'doctrine.migrations_cmd.version',
133
                    'doctrine.migrations_cmd.diff',
134 2
                    'doctrine.migrations_cmd.latest',
135 2
                )
136
            );
137 2
        }
138
139
        $cli->addCommands(array_map(array($serviceLocator, 'get'), $commands));
140 2
141 2
        /* @var $entityManager \Doctrine\ORM\EntityManager */
142
        $entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
143 2
        $helperSet     = $cli->getHelperSet();
144 2
145 2
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
146
            $helperSet->set(new QuestionHelper(), 'dialog');
147
        } else {
148
            $helperSet->set(new DialogHelper(), 'dialog');
149 2
        }
150 2
151 2
        $helperSet->set(new ConnectionHelper($entityManager->getConnection()), 'db');
152
        $helperSet->set(new EntityManagerHelper($entityManager), 'em');
153
    }
154
155
    /**
156
     * Expected to return \Zend\ServiceManager\Config object or array to
157
     * seed such an object.
158
     *
159
     * @return array|\Zend\ServiceManager\Config
160
     */
161
    public function getRouteConfig()
162
    {
163
        $routeType = class_exists(\Zend\Mvc\Router\Http\Literal::class)
164
            ? \Zend\Mvc\Router\Http\Literal::class
165
            : \Zend\Router\Http\Literal::class;
166
        return [
167
            'router' => array(
168
                'routes' => array(
169
                    'doctrine_orm_module_yuml' => array(
170
                        'type' => $routeType,
171
                        'options' => array(
172
                            'route' => '/ocra_service_manager_yuml',
173
                            'defaults' => array(
174
                                'controller' => Yuml\YumlController::class,
175
                                'action'     => 'index',
176
                            ),
177
                        ),
178
                    ),
179
                ),
180
            ),
181
        ];
182
    }
183
}
184