Completed
Push — master ( 14f560...15f305 )
by
unknown
08:01
created

ComposerModuleManager::getModule()   C

Complexity

Conditions 12
Paths 91

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 26
c 0
b 0
f 0
nc 91
nop 1
dl 0
loc 46
rs 5.15

How to fix   Complexity   

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 declare(strict_types=1);
2
3
namespace samsonphp\core\loader\module;
4
5
use samsonframework\resource\ResourceMap;
6
use samsonphp\event\Event;
7
8
/**
9
 * Class ComposerModuleManager implementation
10
 *
11
 * @package samsonphp\core\loader\module
12
 */
13
class ComposerModuleManager implements ModuleManagerInterface
14
{
15
    /** Composer precontainer id */
16
    const PRE_CONTAINER_ID = 'samsonframework_precontainer';
17
18
    /** Composer compressable module id */
19
    const COMPRESSABLE_MODULE_ID = 'samsonphp_package_compressable';
20
21
    /** @var array List of modules */
22
    protected $composerModules = [];
23
24
    /** @var string Application path */
25
    protected $appPath;
26
27
    /** @var string Vendor dir */
28
    protected $vendorDir;
29
30
    /** @var array Collection of interface => [classes]*/
31
    public $implements = [];
32
    /** @var array Collection of class => class */
33
    protected $extends = [];
34
35
    /**
36
     * ComposerModuleManager constructor.
37
     *
38
     * @param string $path
39
     * @param string $vendorDir
40
     */
41
    public function __construct(string $path, string $vendorDir)
42
    {
43
        $this->appPath = $path;
44
        $this->vendorDir = $vendorDir;
45
46
        // TODO Move this somewhere
47
        Event::fire('core.composer.create', [
48
            &$this->composerModules,
49
            $this->appPath, [
50
                'vendorsList' => ['samsonphp/', 'samsonos/', 'samsoncms/', 'samsonjavascript/'],
51
                'ignoreKey' => 'samson_module_ignore',
52
                'includeKey' => 'samson_module_include'
53
            ]
54
        ]);
55
    }
56
57
    /** {@inheritdoc} */
58
    public function getRegisteredModules() : array
59
    {
60
        $list = [];
61
        // Get all modules by their names
62
        foreach (array_keys($this->composerModules) as $moduleName) {
63
            $list[$moduleName] = $this->getModule($moduleName);
64
        }
65
        return $list;
66
    }
67
68
    /** {@inheritdoc} */
69
    public function getModule(string $moduleName) : Module
70
    {
71
        // Check if module exists
72
        if (!array_key_exists($moduleName, $this->composerModules)) {
73
            throw new \Exception(sprintf('Module with name "%s" not found', $moduleName));
74
        }
75
76
        $composerModule = $this->composerModules[$moduleName];
77
        $modulePath = $this->appPath . $this->vendorDir . $moduleName;
78
79
        // Convert resource map to module type
80
        $resourceMap = ResourceMap::get($modulePath);
81
82
        // Get all module classes
83
        $classes = [];
84
        foreach ($resourceMap->classData as $classData) {
85
            $classes[$classData['path']] = $classData['className'];
86
            if (array_key_exists('implements', $classData)) {
87
                foreach ($classData['implements'] as $interfaceName) {
88
                    $this->implements[$interfaceName] = $classData['className'];
89
                }
90
            }
91
            if (array_key_exists('extends', $classData) && isset($classData['extends']{0})) {
92
                $this->extends[$classData['extends']] = $classData['className'];
93
            }
94
        }
95
96
        foreach ($this->implements as $interface => $class) {
97
            if (array_key_exists($class, $this->extends)) {
98
                $this->implements[$interface] = $this->extends[$class];
99
            }
100
        }
101
102
        // Create new module
103
        $module = new Module($moduleName, $modulePath, $classes);
104
        $module->composerParameters = $composerModule;
105
        if (isset($resourceMap->module[0])) {
106
            $module->className = $resourceMap->module[0];
107
            $module->pathName = $resourceMap->module[1];
108
        }
109
        $module->isVirtualModule = !array_key_exists(0, $resourceMap->module) &&
0 ignored issues
show
Bug introduced by
The property isVirtualModule does not seem to exist in samsonphp\core\loader\module\Module.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
            array_key_exists(self::COMPRESSABLE_MODULE_ID, $composerModule) &&
111
            (bool)$composerModule[self::COMPRESSABLE_MODULE_ID] === true;
112
113
        return $module;
114
    }
115
}
116