Passed
Push — master ( 6215ec...1255dc )
by Darío
01:47
created

ModuleFactory::create()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 2
dl 0
loc 26
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
use Drone\Mvc\Exception;
14
15
/**
16
 * ModuleFactory Class
17
 *
18
 * This class creates a module instance
19
 */
20
class ModuleFactory
21
{
22
    /**
23
     * Creates the module instance
24
     *
25
     * @param string             $module
26
     * @param array              $module_settings
27
     *
28
     * @return null
29
     */
30
    public static function create($module, Array $module_settings = [])
31
    {
32
        if (empty($module))
33
            throw new \RuntimeException("The module name must be specified");
34
35
        if (!is_string($module))
0 ignored issues
show
introduced by
The condition is_string($module) is always true.
Loading history...
36
            throw new \InvalidArgumentException("Invalid type given. string expected");
37
38
        /*
39
         * Module class instantiation
40
         *
41
         * Each module must have a class called Module in her namespace. This class
42
         * is initilized here and can change the behavior of a controller using
43
         * allowExecution() or disallowExecution().
44
         */
45
        $fqn_module = "\\" . $module . "\\Module";
46
47
        if (!class_exists($fqn_module))
48
            throw new Exception\ModuleNotFoundException("The module class '$fqn_module' does not exists!");
49
50
        $module = new $fqn_module($module);
51
52
        if (array_key_exists('config', $module_settings) && !is_null($module_settings["config"]))
53
            $module->setConfigFile($module_settings["config"]);
54
55
        return $module;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $module returns the type object which is incompatible with the documented return type null.
Loading history...
56
    }
57
}