GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.0 ( 09574d...fe672e )
by Vermeulen
02:00
created

Module::addDependency()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
use \stdClass;
7
8
/**
9
 * Class to manage a module
10
 */
11
class Module
12
{
13
    /**
14
     * @var string $pathName Module's name
15
     */
16
    protected $pathName = '';
17
18
    /**
19
     * @var \BFW\Config $config Config object for this module
20
     */
21
    protected $config;
22
23
    /**
24
     * @var \stdClass $installInfos All install informations for this module
25
     */
26
    protected $installInfos;
27
28
    /**
29
     * @var \stdClass $loadInfos All informations about the module running
30
     */
31
    protected $loadInfos;
32
33
    /**
34
     *
35
     * @var \stdClass $status Load and run status
36
     */
37
    protected $status;
38
39
    /**
40
     * Constructor
41
     * Load all informations if $loadModule is true
42
     * 
43
     * @param string $pathName Module name
44
     * @param boolean $loadModule (default true) If run load information
45
     */
46
    public function __construct($pathName, $loadModule = true)
47
    {
48
        $this->pathName = $pathName;
49
        
50
        if ($loadModule === true) {
51
            $this->loadModule();
52
        }
53
    }
54
    
55
    /**
56
     * Load informations about the module
57
     * 
58
     * @return void
59
     */
60
    public function loadModule()
61
    {
62
        $this->status       = new stdClass();
63
        $this->status->load = false;
64
        $this->status->run  = false;
65
66
        $this->loadConfig();
67
        $this->loadModuleInfos();
68
69
        $this->status->load = true;
70
    }
71
72
    /**
73
     * Get installation informations
74
     * 
75
     * @param string $sourceFiles Path to module source (in vendor)
76
     * 
77
     * @return \stdClass
78
     */
79
    public static function installInfos($sourceFiles)
80
    {
81
        $currentClass = get_called_class(); //Allow extends
82
        return $currentClass::loadJsonFile($sourceFiles.'/bfwModulesInfos.json');
83
    }
84
85
    /**
86
     * Get the module's name
87
     * 
88
     * @return string
89
     */
90
    public function getPathName()
91
    {
92
        return $this->pathName;
93
    }
94
95
    /**
96
     * Get the Config object which have config for this module
97
     * 
98
     * @return \BFW\Config
99
     */
100
    public function getConfig()
101
    {
102
        return $this->config;
103
    }
104
105
    /**
106
     * Get the installation informations
107
     * 
108
     * @return \stdClass
109
     */
110
    public function getInstallInfos()
111
    {
112
        return $this->installInfos;
113
    }
114
115
    /**
116
     * Get the load informations
117
     * 
118
     * @return \stdClass
119
     */
120
    public function getLoadInfos()
121
    {
122
        return $this->loadInfos;
123
    }
124
125
    /**
126
     * Get the status object for this module
127
     * 
128
     * @return \stdClass
129
     */
130
    public function getStatus()
131
    {
132
        return $this->status;
133
    }
134
135
    /**
136
     * Return the load status
137
     * 
138
     * @return boolean
139
     */
140
    public function isLoaded()
141
    {
142
        return $this->status->load;
143
    }
144
145
    /**
146
     * Return the run status
147
     * 
148
     * @return boolean
149
     */
150
    public function isRun()
151
    {
152
        return $this->status->run;
153
    }
154
155
    /**
156
     * Instantiate the Config object to obtains module's configuration
157
     * 
158
     * @return void
159
     */
160
    public function loadConfig()
161
    {
162
        if (!file_exists(CONFIG_DIR.$this->pathName)) {
163
            return;
164
        }
165
166
        $this->config = new \BFW\Config($this->pathName);
167
        $this->config->loadFiles();
168
    }
169
170
    /**
171
     * Get load information from json file
172
     * 
173
     * @return void
174
     */
175
    public function loadModuleInfos()
176
    {
177
        $currentClass = get_called_class(); //Allow extends
178
        
179
        $this->loadInfos = $currentClass::loadJsonFile(
180
            MODULES_DIR.$this->pathName
181
            .'/module.json'
182
        );
183
    }
184
185
    /**
186
     * Read a json file and return datas in json
187
     * 
188
     * @param string $jsonFilePath : The path to the file to read
189
     * 
190
     * @return mixed Json parsed datas
191
     * 
192
     * @throws Exception If the file is not found or for a json parser error
193
     */
194
    protected static function loadJsonFile($jsonFilePath)
195
    {
196
        if (!file_exists($jsonFilePath)) {
197
            throw new Exception('File '.$jsonFilePath.' not found.');
198
        }
199
200
        $infos = json_decode(file_get_contents($jsonFilePath));
201
        if ($infos === null) {
202
            throw new Exception(json_last_error_msg());
203
        }
204
205
        return $infos;
206
    }
207
    
208
    /**
209
     * Add a dependency to module
210
     * Used by needMe property in module infos
211
     * 
212
     * @param string $dependencyName The dependency name to add
213
     * 
214
     * @return $this
215
     */
216
    public function addDependency($dependencyName)
217
    {
218
        if (!property_exists($this->loadInfos, 'require')) {
219
            $this->loadInfos->require = [];
220
        }
221
        
222
        if (!is_array($this->loadInfos->require)) {
223
            $this->loadInfos->require = [$this->loadInfos->require];
224
        }
225
        
226
        $this->loadInfos->require[] = $dependencyName;
227
        
228
        return $this;
229
    }
230
231
    /**
232
     * Get path to the runner file
233
     * 
234
     * @return string
235
     * 
236
     * @throws Exception If the file not exists
237
     */
238
    protected function getRunnerFile()
239
    {
240
        $moduleInfos = $this->loadInfos;
241
        $runnerFile  = '';
242
243
        if (property_exists($moduleInfos, 'runner')) {
244
            $runnerFile = (string) $moduleInfos->runner;
245
        }
246
247
        if ($runnerFile === '') {
248
            return;
249
        }
250
251
        $runnerFile = MODULES_DIR.$this->pathName
252
            .'/'.$runnerFile
253
        ;
254
255
        if (!file_exists($runnerFile)) {
256
            throw new Exception(
257
                'Runner file for module '.$this->pathName.' not found.'
258
            );
259
        }
260
261
        return $runnerFile;
262
    }
263
264
    /**
265
     * Run the module in a closure
266
     * 
267
     * @return void
268
     */
269
    public function runModule()
270
    {
271
        $runnerFile = $this->getRunnerFile();
272
        $module     = $this;
273
274
        $initFunction = function() use ($runnerFile, $module) {
275
            if ($runnerFile === null) {
276
                return;
277
            }
278
            
279
            require(realpath($runnerFile));
280
        };
281
282
        $this->status->run = true;
283
        $initFunction();
284
    }
285
}
286