Test Failed
Push — master ( 310ecf...5d6054 )
by Alexey
05:08
created

ComposerCmd::installComposer()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 3
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
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 ComposerCmd {
12
13
    public static function check() {
14
        if (!file_exists(getenv('COMPOSER_HOME') . '/composer/vendor/autoload.php')) {
15
            self::installComposer(getenv('COMPOSER_HOME'));
16
        }
17
        if (!file_exists(getenv('COMPOSER_HOME') . '/vendor/autoload.php')) {
18
            self::initComposer(getenv('COMPOSER_HOME'));
19
        }
20
        if (!file_exists(App::$primary->path . '/vendor/autoload.php')) {
21
            self::initComposer();
22
        }
23
    }
24
25
    public static function installComposer($path) {
26
        if (file_exists($path . '/composer/bin/composer')) {
27
            return true;
28
        }
29
        Tools::createDir($path . '/composer');
30
        if (!file_exists($path . '/composer/composer.phar')) {
31
            file_put_contents($path . '/composer/composerInstall.php', str_replace('process(is_array($argv) ? $argv : array());', '', file_get_contents('https://getcomposer.org/installer')));
32
            include_once $path . '/composer/composerInstall.php';
33
34
            $check = false;
0 ignored issues
show
Unused Code introduced by
$check 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...
35
            $help = false;
0 ignored issues
show
Unused Code introduced by
$help 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...
36
            $force = false;
0 ignored issues
show
Unused Code introduced by
$force 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...
37
            $quiet = false;
38
            $channel = 'stable';
39
            $disableTls = false;
40
            $installDir = $path . '/composer/';
41
            $version = false;
42
            $filename = 'composer.phar';
43
            $cafile = false;
44
            setUseAnsi([]);
45
            installComposer($version, $installDir, $filename, $quiet, $disableTls, $cafile, $channel);
46
        }
47
        $composer = new Phar($path . '/composer/composer.phar');
48
        $composer->extractTo($path . '/composer/');
49
    }
50
51
    public static function initComposer($path = '') {
52
        if (!$path) {
53
            $path = App::$primary->path . '/';
54
        }
55
        if (!file_exists($path . '/composer.json')) {
56
            $json = [
57
                "name" => get_current_user() . "/" . App::$primary->name,
58
                "config" => [
59
                    "cache-dir" => "./composerCache/"
60
                ],
61
                "authors" => [
62
                    [
63
                        "name" => get_current_user(),
64
                        "email" => get_current_user() . "@" . INJI_DOMAIN_NAME
65
                    ]
66
                ],
67
                "require" => [
68
                    "php" => ">=5.5.0"
69
                ]
70
            ];
71
            Tools::createDir($path);
72
            file_put_contents($path . '/composer.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
73
        }
74
        self::command('install', false, $path);
75
    }
76
77
    public static function command($command, $needOutput = true, $path = null) {
78
        ini_set('memory_limit', '2000M');
79
        include_once getenv('COMPOSER_HOME') . '/composer/vendor/autoload.php';
80
        if ($needOutput) {
81
            $output = new Symfony\Component\Console\Output\StreamOutput(fopen('php://output', 'w'));
82
        } else {
83
            $output = null;
84
        }
85
        $path = str_replace('\\', '/', $path === null ? App::$primary->path . '/' : $path);
86
        $input = new Symfony\Component\Console\Input\StringInput($command . ' -d ' . $path);
87
        $app = new Composer\Console\Application();
88
        $app->setAutoExit(false);
89
        $dir = getcwd();
90
        $app->run($input, $output);
91
        chdir($dir);
92
    }
93
94
    public static function requirePackage($packageName, $version = '', $path = '') {
95
        if (!$path) {
96
            $path = App::$primary->path;
97
        }
98
        if (file_exists($path . '/composer.lock')) {
99
            $lockFile = json_decode(file_get_contents($path . '/composer.lock'), true);
100
        }
101
        if (!empty($lockFile['packages'])) {
102
            foreach ($lockFile['packages'] as $package) {
103
                if ($package['name'] == $packageName) {
104
                    return true;
105
                }
106
            }
107
        }
108
109
        ComposerCmd::command('require ' . $packageName . ($version ? ':' . $version : ''), false, $path);
110
        return true;
111
    }
112
}