Completed
Pull Request — master (#4)
by Timothy
03:57
created

Module::initializeConsole()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.7972
cc 4
eloc 12
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Abacaphiliac\DoctrineORMDiagnosticsModule;
4
5
use Symfony\Component\Console\Application;
6
use Symfony\Component\Console\Command\Command;
7
use Zend\EventManager\EventInterface;
8
use Zend\ModuleManager\Feature\ConfigProviderInterface;
9
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
10
use Zend\ModuleManager\Feature\InitProviderInterface;
11
use Zend\ModuleManager\ModuleManagerInterface;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
14
class Module implements ConfigProviderInterface, DependencyIndicatorInterface, InitProviderInterface
15
{
16
    /**
17
     * Returns configuration to merge with application configuration
18
     *
19
     * @return array|\Traversable
20
     */
21 6
    public function getConfig()
22
    {
23 6
        return require dirname(dirname(__DIR__)) . '/config/module.config.php';
24
    }
25
26
    /**
27
     * Expected to return an array of modules on which the current one depends on
28
     *
29
     * @return array
30
     */
31 6
    public function getModuleDependencies()
32
    {
33
        return [
34 6
            'DoctrineModule',
35 6
            'DoctrineORMModule',
36 6
            'ZFTool',
37 6
        ];
38
    }
39
    
40
    /**
41
     * @param ModuleManagerInterface $manager
42
     */
43 5
    public function init(ModuleManagerInterface $manager)
44
    {
45 5
        $events = $manager->getEventManager();
46 5
        $sharedEventManager = $events->getSharedManager();
47 5
        $sharedEventManager->attach('doctrine', 'loadCli.post', [$this, 'initializeConsole']);
48 5
    }
49
50
    /**
51
     * Initialize the console with additional commands from (optionally) doctrine\migrations
52
     * @param EventInterface $event
53
     * @return bool
54
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
55
     */
56 8
    public function initializeConsole(EventInterface $event)
57
    {
58 8
        $cli = $event->getTarget();
59 8
        if (!$cli instanceof Application) {
60 1
            return false;
61
        }
62
        
63 7
        $serviceLocator = $event->getParam('ServiceManager');
64 7
        if (!$serviceLocator instanceof ServiceLocatorInterface) {
65 1
            return false;
66
        }
67
68 6
        if (class_exists('Doctrine\DBAL\Migrations\Tools\Console\Command\UpToDateCommand')) {
69
            /** @var Command $upToDateCommand */
70 6
            $upToDateCommand = $serviceLocator->get('doctrine.migrations_cmd.uptodate');
71
            
72 6
            $cli->addCommands([
73 6
                $upToDateCommand,
74 6
            ]);
75 6
        }
76
        
77 6
        return true;
78
    }
79
}
80