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

AbstractModule::loader()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 16
nop 1
dl 0
loc 22
rs 9.5222
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
/**
14
 * AbstractModule class
15
 *
16
 * This is an abstract class to execute some code before method execution in a controller.
17
 */
18
abstract class AbstractModule
19
{
20
    /**
21
     * The module name
22
     *
23
     * @var string
24
     */
25
    protected $moduleName;
26
27
    /**
28
     * Config file for the module
29
     *
30
     * @var string
31
     */
32
    protected $configFile;
33
34
    /**
35
     * Defines method execution in the controller
36
     *
37
     * @var boolean
38
     */
39
    private $executionAllowed = true;
40
41
    /**
42
     * Returns the moduleName attribute
43
     *
44
     * @return string
45
     */
46
    public function getModuleName()
47
    {
48
        return $this->moduleName;
49
    }
50
51
    /**
52
     * Returns the configFile attribute
53
     *
54
     * @return string
55
     */
56
    public function getConfigFile()
57
    {
58
        return $this->configFile;
59
    }
60
61
    /**
62
     * Sets the moduleName attribute
63
     *
64
     * @param string $moduleName
65
     *
66
     * @return null
67
     */
68
    public function setModuleName($moduleName)
69
    {
70
        $this->moduleName = $moduleName;
71
    }
72
73
    /**
74
     * Sets the configFile attribute
75
     *
76
     * @param string $configFile
77
     *
78
     * @throws \RuntimeException
79
     *
80
     * @return null
81
     */
82
    public function setConfigFile($configFile)
83
    {
84
        $this->configFile = $configFile;
85
    }
86
87
    /**
88
     * Constructor
89
     *
90
     * @param string $moduleName
91
     */
92
    public function __construct($moduleName)
93
    {
94
        $this->moduleName = $moduleName;
95
        $this->init();
96
    }
97
98
    /**
99
     * Abstract method to be executed before each method execution in the controller
100
     *
101
     * @param AbstractController
102
     */
103
    public abstract function init();
104
105
    /**
106
     * Checks if executionAllowed is true
107
     *
108
     * @return null
109
     */
110
    public function executionIsAllowed()
111
    {
112
        return $this->executionAllowed;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->executionAllowed returns the type boolean which is incompatible with the documented return type null.
Loading history...
113
    }
114
115
    /**
116
     * Disallow the method execution in a controller
117
     *
118
     * @return null
119
     */
120
    public function disallowExecution()
121
    {
122
        $this->executionAllowed = false;
123
    }
124
125
    /**
126
     * Allow the method execution in a controller
127
     *
128
     * @return null
129
     */
130
    public function allowExecution()
131
    {
132
        $this->executionAllowed = true;
133
    }
134
135
    /**
136
     * Returns the module configuration
137
     *
138
     * @return array
139
     */
140
    public function getConfig()
141
    {
142
        $_file = $this->configFile;
143
144
        if (!file_exists($_file))
145
            throw new \RuntimeException("The config file '$_file' does not exists");
146
147
        return (array) include $this->configFile;
148
    }
149
}