Completed
Pull Request — master (#165)
by Paul
03:21
created

ModuleManagerFactory::createService()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 37
rs 8.8571
cc 3
eloc 25
nc 4
nop 1
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright   Copyright (c) 2012 Paul Dragoonis <[email protected]>
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link        http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\ServiceManager\Factory;
12
13
use PPI\Framework\Module\ModuleManager;
14
use Zend\ModuleManager\ModuleEvent;
15
use Zend\ServiceManager\FactoryInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
18
/**
19
 * ModuleManager Factory.
20
 *
21
 * @author     Vítor Brandão <[email protected]>
22
 */
23
class ModuleManagerFactory implements FactoryInterface
24
{
25
    /**
26
     * Creates and returns the module manager.
27
     *
28
     * Instantiates the default module listeners, providing them configuration
29
     * from the "module_listener_options" key of the ApplicationConfig
30
     * service. Also sets the default config glob path.
31
     *
32
     * Module manager is instantiated and provided with an EventManager, to which
33
     * the default listener aggregate is attached. The ModuleEvent is also created
34
     * and attached to the module manager.
35
     *
36
     * @param ServiceLocatorInterface $serviceLocator
37
     *
38
     * @return ModuleManager
39
     */
40
    public function createService(ServiceLocatorInterface $serviceLocator)
41
    {
42
        if (!$serviceLocator->has('ServiceListener')) {
43
            $serviceLocator->setFactory('ServiceListener', 'PPI\Framework\ServiceManager\Factory\ServiceListenerFactory');
44
        }
45
46
        $config           = $serviceLocator->get('ApplicationConfig');
47
        $defaultListeners = $serviceLocator->get('ModuleDefaultListener');
48
        $serviceListener  = $serviceLocator->get('ServiceListener');
49
50
        $serviceListener->addServiceManager(
51
            $serviceLocator,
52
            'service_manager',
53
            'Zend\ModuleManager\Feature\ServiceProviderInterface',
54
            'getServiceConfig'
55
        );
56
        $serviceListener->addServiceManager(
57
            'RoutePluginManager',
58
            'route_manager',
59
            'Zend\ModuleManager\Feature\RouteProviderInterface',
60
            'getRouteConfig'
61
        );
62
63
        $modules = isset($config['modules']) ? $config['modules'] : array();
64
65
        $events = $serviceLocator->get('EventManager');
66
        $events->attach($defaultListeners);
67
        $events->attach($serviceListener);
68
69
        $moduleEvent = new ModuleEvent();
70
        $moduleEvent->setParam('ServiceManager', $serviceLocator);
71
72
        $moduleManager = new ModuleManager($modules, $events);
0 ignored issues
show
Documentation introduced by
$events is of type object|array, but the function expects a null|object<Zend\EventMa...\EventManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        $moduleManager->setEvent($moduleEvent);
74
75
        return $moduleManager;
76
    }
77
}
78