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.
Passed
Branch 3.0 (213cde)
by Vermeulen
01:24
created

Module::isRun()

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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