ModuleLoader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 10
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 90
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 8 2
A remove() 0 9 2
A getModules() 0 3 1
A getModule() 0 7 2
A getModulesInfo() 0 12 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Alejandro Peña Florentín ([email protected]).
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace Tight\Modules;
28
29
/**
30
 * ModuleLoader class
31
 *
32
 * @author Alejandro Peña Florentín ([email protected])
33
 */
34
class ModuleLoader
35
{
36
37
    /**
38
     * @var array Modules added
39
     */
40
    private $modules = [];
41
42
    /**
43
     * @var \Tight\Tight Tight app
44
     */
45
    private $app;
46
47
    /**
48
     * Constructor
49
     * @param \Tight\Tight $app Tight app
50
     */
51
    public function __construct(\Tight\Tight $app) {
52
        $this->app = $app;
53
    }
54
55
    /**
56
     * Adds a new module
57
     * @param \Tight\Modules\AbstractModule $module Module
58
     * @throws \Tight\Exception\ModuleException If the module is already added
59
     */
60
    public function add(\Tight\Modules\AbstractModule $module) {
61
        if (!isset($this->modules[$module->getModuleName()])) {
62
            $this->modules[$module->getModuleName()] = $module;
63
            $module->onLoad();
64
        } else {
65
            throw new \Tight\Exception\ModuleException("Module <strong>" . $module->getModuleName() . "</strong> already exists");
66
        }
67
    }
68
69
    /**
70
     * Removes a module
71
     * @param string $moduleName Module name
72
     * @return boolean TRUE on success, FALSE if the module does not exists
73
     */
74
    public function remove($moduleName) {
75
        if (isset($this->modules[$moduleName])) {
76
            $this->modules[$moduleName]->onRemove();
77
            unset($this->modules[$moduleName]);
78
            return true;
79
        } else {
80
            return false;
81
        }
82
    }
83
84
    /**
85
     * Gets all the modules added
86
     * @return array Modules
87
     */
88
    public function getModules() {
89
        return $this->modules;
90
    }
91
92
    /**
93
     * Gets a single module
94
     * @param string $moduleName Module name
95
     * @return \Tight\Module\AbstractModule Module or null if the module name 
96
     * cant be found
97
     */
98
    public function getModule($moduleName) {
99
        if (isset($this->modules[$moduleName])) {
100
            return $this->modules[$moduleName];
101
        } else {
102
            return null;
103
        }
104
    }
105
106
    /**
107
     * Gets a module info
108
     * @return array Modules info
109
     */
110
    public function getModulesInfo() {
111
        $output = [];
112
        $modules = $this->getModules();
113
        foreach ($modules as $value) {
114
            $mod = [
115
                "name" => $value->getModuleName(),
116
                "version" => $value->getModuleVersion()
117
            ];
118
            $output[] = $mod;
119
        }
120
        return $output;
121
    }
122
123
}
124