Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

BowerCmd   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A check() 0 5 2
B initBower() 0 35 4
B command() 0 23 4
B requirePackage() 0 18 5
1
<?php
2
namespace Inji;
3
/**
4
 * Composer command tool
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class BowerCmd {
12
13
    public static $appInstance = null;
14
15
    public static function getInstance() {
16
        if (!self::$appInstance) {
17
            self::$appInstance = new Bowerphp\Console\Application();
18
        }
19
        return self::$appInstance;
20
    }
21
22
    public static function check() {
23
        if (!file_exists(INJI_BASE_DIR . Cache::folder() . "static/bowerLibs/bower.json")) {
24
            BowerCmd::initBower();
25
        }
26
    }
27
28
    public static function initBower($path = '') {
29
        while (!Inji::$inst->blockParallel()) {
30
            sleep(2);
31
        }
32
        if (!$path) {
33
            $path = INJI_BASE_DIR . Cache::folder() . "static/bowerLibs/";
34
        }
35
        $json = [
36
            "name" => get_current_user() . "/" . $_SERVER['SERVER_NAME'],
37
            "config" => [
38
                "cache-dir" => INJI_BASE_DIR . Cache::folder() . "bower/"
39
            ],
40
            "authors" => [
41
                [
42
                    get_current_user() . ' <' . get_current_user() . "@" . $_SERVER['SERVER_NAME'] . '>'
43
                ]
44
            ],
45
            'private' => true,
46
            "dependencies" => [],
47
        ];
48
        Tools::createDir($path);
49
        file_put_contents($path . 'bower.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
50
51
        if (!file_exists($path . '.bowerrc')) {
52
            $json = [
53
                "directory" => './',
54
                "interactive" => false
55
            ];
56
            Tools::createDir($path);
57
            file_put_contents($path . '.bowerrc', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
58
        }
59
        self::command('install', false, $path);
60
        gc_collect_cycles();
61
        Inji::$inst->unBlockParallel();
62
    }
63
64
    public static function command($command, $needOutput = true, $path = null) {
65
        while (!Inji::$inst->blockParallel()) {
66
            sleep(2);
67
        }
68
        ComposerCmd::requirePackage("injitools/bowerphp", "dev-master", '.');
69
        include_once 'vendor/injitools/bowerphp/src/bootstrap.php';
70
        if ($needOutput) {
71
            $output = new Symfony\Component\Console\Output\StreamOutput(fopen('php://output', 'w'));
72
        } else {
73
            $output = new Symfony\Component\Console\Output\NullOutput();
74
        }
75
        $path = str_replace('\\', '/', $path === null ? INJI_BASE_DIR . Cache::folder() . "static/bowerLibs/" : $path);
76
        $input = new Symfony\Component\Console\Input\StringInput($command);
77
        $app = self::getInstance();
78
        chdir($path);
79
        putenv('HOME=' . getcwd());
80
        $app->doRun($input, $output);
81
        $output = null;
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
        $input = null;
0 ignored issues
show
Unused Code introduced by
$input is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
        chdir(INJI_BASE_DIR);
84
        gc_collect_cycles();
85
        Inji::$inst->unBlockParallel();
86
    }
87
88
    public static function requirePackage($packageName, $version = '', $path = '') {
89
        if (!$path) {
90
            $path = INJI_BASE_DIR . Cache::folder() . "static/bowerLibs/";
91
        }
92
        $bowerJson = json_decode(file_get_contents($path . 'bower.json'), true);
0 ignored issues
show
Unused Code introduced by
$bowerJson is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
        if (strpos($packageName, 'github') !== false) {
94
            $needPackageName = basename($packageName);
95
        } else {
96
            $needPackageName = $packageName;
97
        }
98
        if (file_exists(Cache::folder() . 'static/bowerLibs/' . $needPackageName)) {
99
            return true;
100
        }
101
        set_time_limit(0);
102
        ini_set('memory_limit', '4000M');
103
        self::command('install ' . $packageName . ($version ? '#' . $version : '') . ' --save', false, $path);
104
        return true;
105
    }
106
}