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 ( a38ab0...ba3e89 )
by Vermeulen
02:44
created

ModuleInstall::createConfigDirectory()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 7
nop 0
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace BFW\Install;
4
5
use \Exception;
6
7
class ModuleInstall
8
{
9
    const INSTALL_SCRIPT_VERSION = '3.0.0';
10
11
    /**
12
     * @var string $bfwPath : Path to root bfw project
13
     */
14
    protected $bfwPath = '';
15
16
    /**
17
     * @var string $bfwConfigPath : Path to bfw config directory
18
     */
19
    protected $bfwConfigPath = '';
20
21
    /**
22
     * @var string $bfwModulePath : Path to bfw modules directory
23
     */
24
    protected $bfwModulePath = '';
25
26
    /**
27
     * @var boolean $forceReinstall : Force complete reinstall of module
28
     */
29
    protected $forceReinstall = false;
30
31
    /**
32
     * @var string $name : Module name
33
     */
34
    protected $name = '';
35
36
    /**
37
     * @var string $pathToModule : Path to module which be installed
38
     */
39
    protected $pathToModule = '';
40
41
    /**
42
     * @var string $srcPath : Path to module source files
43
     */
44
    protected $srcPath = '';
45
46
    /**
47
     * @var string $configPath : Path to module config files
48
     */
49
    protected $configPath = '';
50
51
    /**
52
     * @var array $configFiles : List of config file to copy to config directory
53
     */
54
    protected $configFiles = [];
55
56
    /**
57
     * @var string|bool $installScript : Script to run for specific install from module
58
     *                                  Boolean on 2.x
59
     */
60
    protected $installScript = '';
61
62
    /**
63
     * Constructor
64
     * 
65
     * @param type $bfwPath
66
     * @param type $pathToModule
67
     */
68
    public function __construct($bfwPath, $pathToModule)
69
    {
70
        $this->bfwPath      = $bfwPath;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bfwPath of type object<BFW\Install\type> is incompatible with the declared type string of property $bfwPath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        $this->pathToModule = $pathToModule;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pathToModule of type object<BFW\Install\type> is incompatible with the declared type string of property $pathToModule.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
73
        $this->bfwConfigPath = $this->bfwPath.'/app/config/';
74
        $this->bfwModulePath = $this->bfwPath.'/app/modules/';
75
    }
76
77
    /**
78
     * Get accessor to module name
79
     * 
80
     * @return string : Module name
81
     */
82
    public function getName()
83
    {
84
        return $this->name;
85
    }
86
87
    /**
88
     * Load module informations from files
89
     * Define module name
90
     * 
91
     * @return void
92
     */
93
    public function loadInfos()
94
    {
95
        $pathExplode = explode('/', $this->pathToModule);
96
        $this->name  = $pathExplode[(count($pathExplode) - 1)];
97
98
        $this->bfwConfigPath .= $this->name;
99
        $this->bfwModulePath .= $this->name;
100
101
        $infos = $this->getInfosFromModule();
102
        
103
        if (!property_exists($infos, 'srcPath')) {
104
            throw new Exception(
105
                'srcPath must be present in install json file for module '.$this->name
106
            );
107
        }
108
109
        $this->srcPath    = $infos->srcPath;
110
        $this->configPath = $infos->srcPath;
111
112
        if (property_exists($infos, 'configFiles')) {
113
            $this->configFiles = (array) $infos->configFiles;
114
        }
115
116
        if (property_exists($infos, 'configPath')) {
117
            $this->configPath = $infos->configPath;
118
        }
119
120
        if (property_exists($infos, 'installScript')) {
121
            $this->installScript = $infos->installScript;
122
        }
123
124
        $this->srcPath = realpath($this->pathToModule.'/'.$this->srcPath);
125
    }
126
    
127
    /**
128
     * Get infos for this module from BFW Module class
129
     * It's a separate method for easy override.
130
     * 
131
     * @return \stdClass
132
     */
133
    protected function getInfosFromModule()
134
    {
135
        return \BFW\Module::installInfos($this->pathToModule);
136
    }
137
138
    /**
139
     * Run module installation
140
     * 
141
     * @param boolean $reinstall : If we force reinstall module
142
     * 
143
     * @return void
144
     */
145
    public function install($reinstall)
146
    {
147
        $this->forceReinstall = $reinstall;
148
        
149
        echo $this->name." : Run install.\n";
150
        
151
        try {
152
            $this->createSymbolicLink();
153
            $this->copyConfigFiles();
154
            $this->runInstallScript();
155
        } catch (Exception $e) {
156
            trigger_error('Module '.$this->name.' install error : '.$e->getMessage(), E_USER_WARNING);
157
        }
158
    }
159
    
160
    /**
161
     * Create symlink in bfw project module directory
162
     * 
163
     * @return void
164
     * 
165
     * @throws Exception : If remove symlink fail for reinstall option
166
     */
167
    protected function createSymbolicLink()
168
    {
169
        echo ' > Create symbolic link ... '."\n";
170
171
        $targetPath     = $this->bfwModulePath;
172
        $alreadyCreated = file_exists($targetPath);
173
174
        if ($alreadyCreated && $this->forceReinstall === true) {
175
            echo '[Force Reinstall: Remove symlink] ';
176
            $alreadyCreated = false;
177
178
            if (!unlink($targetPath)) {
179
                echo "\033[1;31m Symbolic link remove fail.\033[0m\n";
180
                throw new Exception('Reinstall fail. Symlink remove error');
181
            }
182
        }
183
184
        //If module already exist in "modules" directory
185
        if ($alreadyCreated) {
186
            echo "\033[1;33m Not created. Module already exist in 'modules' directory.\033[0m\n";
187
            return;
188
        }
189
190
        //If symbolic link create fail.
191
        if (!symlink($this->srcPath, $targetPath)) {
192
            echo "\033[1;31m Symbolic link creation fail.\033[0m\n";
193
            return;
194
        }
195
196
        echo "\033[1;32m Done\033[0m\n";
197
    }
198
199
    /**
200
     * Create a directory in bfw project config directory for this module and
201
     * copy all config files declared in this directory
202
     * 
203
     * @return void
204
     */
205
    protected function copyConfigFiles()
206
    {
207
        echo ' > Copy config files : '."\n";
208
209
        if ($this->configFiles === []) {
210
            echo ' >> '."\033[1;33m".'No config file declared. Pass'."\033[0m\n";
211
            return;
212
        }
213
214
        $configDirStatus = $this->createConfigDirectory();
215
        if ($configDirStatus === false) {
216
            return;
217
        }
218
219
        foreach ($this->configFiles as $configFile) {
220
            try {
221
                $this->copyConfigFile($configFile);
222
            } catch (Exception $e) {
223
                trigger_error(
224
                    'Module '.$this->name.' Config file '.$configFile.' copy error: '.$e->getMessage(),
225
                    E_USER_WARNING
226
                );
227
            }
228
        }
229
    }
230
231
    /**
232
     * Create a directory in bfw project config directory for this module
233
     * 
234
     * @return boolean : If directory exist.
235
     * 
236
     * @throws Exception : If remove directory fail for reinstall option
237
     */
238
    protected function createConfigDirectory()
239
    {
240
        echo ' >> Create config directory for this module ... ';
241
        
242
        $targetDirectory = $this->bfwConfigPath;
243
        $alreadyExist    = file_exists($targetDirectory);
244
        
245
        if ($alreadyExist && $this->forceReinstall === true) {
246
            echo '[Force Reinstall: Remove symlink] ';
247
            $alreadyExist = false;
248
            
249
            if (!rmdir($targetDirectory)) {
250
                echo "\033[1;31m Remove module config directory fail.\033[0m\n";
251
                throw new Exception('Reinstall fail. Remove module config directory error.');
252
            }
253
        }
254
        
255
        if ($alreadyExist) {
256
            echo "\033[1;33m Already exist.\033[0m\n";
257
            return true;
258
        }
259
            
260
        if (mkdir($targetDirectory, 0755)) {
261
            echo "\033[1;32m Created. \033[0m\n";
262
            return true;
263
        }
264
265
        trigger_error('Module '.$this->name.' Error to create config directory', E_USER_WARNING);
266
        echo "\033[1;31m Fail. \033[0m\n";
267
        
268
        return false;
269
    }
270
    
271
    /**
272
     * Copy a config file into config directory for this module
273
     * 
274
     * @param string $configFileName : The config filename
275
     * 
276
     * @return void
277
     * 
278
     * @throws Exception If copy fail or if source file not exist
279
     */
280
    protected function copyConfigFile($configFileName)
281
    {
282
        echo ' >> Copy '.$configFileName.' ... ';
283
284
        $sourceFile = realpath($this->configPath.'/'.$configFileName);
285
        $targetFile = realpath($this->bfwConfigPath.'/'.$configFileName);
286
287
        //Check if config file already exist
288
        if (file_exists($targetFile)) {
289
            echo "\033[1;33m Existe déjà.\033[0m\n";
290
            return;
291
        }
292
293
        //If source file not exist
294
        if (!file_exists($sourceFile)) {
295
            echo "\033[1;31m Config file not exist in module source.\033[0m\n";
296
            throw new Exception('Source file not exist');
297
        }
298
299
        //Alors on copie le fichier vers le dossier /configs/[monModule]/
300
        if (!copy($sourceFile, $targetFile)) {
301
            echo "\033[1;31m Copy fail.\033[0m\n";
302
            throw new Exception('Copy fail');
303
        }
304
305
        echo "\033[1;32m Done\033[0m\n";
306
    }
307
308
    /**
309
     * Run specific module install script if declared
310
     * 
311
     * @return void
312
     */
313
    protected function runInstallScript()
314
    {
315
        echo ' >> Run install specific script : ';
316
317
        if ($this->installScript === '' || $this->installScript === false) {
318
            echo ' >> '."\033[1;33m".'No specific script declared. Pass'."\033[0m\n";
319
            return;
320
        }
321
322
        if ($this->installScript === true) {
323
            $this->installScript = 'runInstallModule.php';
324
        }
325
326
        require_once($this->pathToModule.'/'.$this->installScript);
327
    }
328
}
329