Completed
Pull Request — develop (#274)
by
unknown
09:44
created

ModuleManager::validatePlugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * @filesource
4
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
5
 * @license MIT
6
 * @author Miroslav Fedeleš <[email protected]>
7
 * @since 0.27
8
 */
9
namespace Auth\Dependency;
10
11
use Zend\ServiceManager\AbstractPluginManager;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Zend\ServiceManager\Exception\RuntimeException;
14
use IteratorAggregate;
15
use Countable;
16
use ArrayIterator;
17
18
class ModuleManager extends AbstractPluginManager implements IteratorAggregate, Countable
19
{
20
    
21
    /**
22
     * @var array
23
     */
24
    protected $modules;
25
    
26
    /**
27
     * @param ServiceLocatorInterface $serviceLocator
28
     * @return \Auth\Dependency\ModuleManager
29
     */
30
    public static function factory(ServiceLocatorInterface $serviceLocator)
31
    {
32
        return new static($serviceLocator, $serviceLocator->get('Config')['auth_dependency_module_manager']);
33
    }
34
    
35
    /**
36
     * @see IteratorAggregate::getIterator()
37
     */
38
    public function getIterator()
39
    {
40
        return new ArrayIterator($this->getModules());
41
    }
42
    
43
    /**
44
     * @see Countable::count()
45
     */
46
    public function count()
47
    {
48
        return count($this->getModules());
49
    }
50
    
51
    /**
52
	 * @see \Zend\ServiceManager\AbstractPluginManager::validatePlugin()
53
	 */
54
	public function validatePlugin($plugin)
55
	{
56
		if (!$plugin instanceof ModuleInterface)
57
		{
58
		    throw new RuntimeException(sprintf('Plugin must be instance of %s', ModuleInterface::class));
59
		}
60
	}
61
	
62
	/**
63
	 * @return ModuleInterface[]
64
	 */
65
	protected function getModules()
66
	{
67
	    if (!isset($this->modules))
68
	    {
69
    	    $this->modules = [];
70
    	    $availablesServices = [];
71
    	    $registeredServices = [
72
    	        'invokableClasses' => array_keys($this->invokableClasses),
73
    	        'factories' => array_keys($this->factories)
74
    	    ];
75
    	
76
    	    foreach ($registeredServices as $services)
77
    	    {
78
    	        $availablesServices = array_merge($availablesServices, $services);
79
    	    }
80
    	
81
    	    foreach (array_unique($availablesServices) as $service)
82
    	    {
83
    	        $this->modules[$service] = $this->get($service);
84
    	    }
85
	    }
86
	
87
	    return $this->modules;
88
	}
89
}
90