Failed Conditions
Push — experimental/3.1 ( 7f7c1e...7af380 )
by Ryo
68:08 queued 67:47
created

ComposerApiService::composerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 9.6666
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
namespace Eccube\Service\Composer;
24
25
use Composer\Console\Application;
26
use Eccube\Annotation\Service;
27
use Symfony\Component\Console\Input\ArrayInput;
28
use Symfony\Component\Console\Output\BufferedOutput;
29
30
/**
31
 * Class ComposerApiService
32
 * @package Eccube\Service\Composer
33
 * @Service
34
 */
35
class ComposerApiService implements ComposerServiceInterface
36
{
37
38
    /**
39
     * @var array
40
     */
41
    protected $appConfig;
42
43
    /**
44
     * @var Application $consoleApplication
45
     */
46
    private $consoleApplication;
47
48
    private $workingDir;
49
50
    public function __construct($appConfig)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
51
    {
52
        $this->appConfig = $appConfig;
53
    }
54
55
    /**
56
     * Run get info command
57
     *
58
     * @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0"
59
     * @return array
60
     */
61
    public function execInfo($pluginName)
62
    {
63
        $output = $this->runCommand(array(
64
            'command' => 'info',
65
            'package' => $pluginName,
66
        ));
67
68
        return OutputParser::parseInfo($output);
69
    }
70
71
    /**
72
     * Run execute command
73
     *
74
     * @param string $packageName format "foo/bar foo/bar:1.0.0"
75
     * @return array
76
     */
77 View Code Duplication
    public function execRequire($packageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $packageName = explode(" ", trim($packageName));
80
        $output = $this->runCommand(array(
81
            'command' => 'require',
82
            'packages' => $packageName,
83
            '--no-interaction' => true,
84
            '--profile' => true,
85
            '--prefer-dist' => true,
86
            '--ignore-platform-reqs' => true,
87
        ));
88
89
        return OutputParser::parseRequire($output);
90
    }
91
92
    /**
93
     * Run remove command
94
     *
95
     * @param string $packageName format "foo/bar foo/bar:1.0.0"
96
     * @return bool
97
     */
98 View Code Duplication
    public function execRemove($packageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $packageName = explode(' ', trim($packageName));
101
        $this->runCommand(array(
102
            'command' => 'remove',
103
            'packages' => $packageName,
104
            '--ignore-platform-reqs' => true,
105
            '--no-interaction' => true,
106
            '--profile' => true,
107
        ));
108
109
        return true;
110
    }
111
112
    /**
113
     * Get require
114
     *
115
     * @param string $packageName
116
     * @param string $callback
117
     * @param null   $typeFilter
118
     */
119
    public function foreachRequires($packageName, $callback, $typeFilter = null)
120
    {
121
        $info = $this->execInfo($packageName);
122
        if (isset($info['requires'])) {
123
            foreach ($info['requires'] as $name => $version) {
124
                $package = $this->execInfo($name);
125
                if (is_null($typeFilter) || @$package['type'] === $typeFilter) {
126
                    $callback($package);
127
                }
128
            }
129
        }
130
    }
131
132
    /**
133
     * Run get config information
134
     *
135
     * @param string $key
136
     * @param null   $value
137
     * @return array|mixed
138
     */
139
    public function execConfig($key, $value = null)
140
    {
141
        $commands = array(
142
            'command' => 'config',
143
            'setting-key' => $key,
144
            'setting-value' => $value,
145
        );
146
        if ($value) {
147
            $commands['setting-value'] = $value;
148
        }
149
        $output = $this->runCommand($commands);
150
151
        return OutputParser::parseConfig($output);
152
    }
153
154
    /**
155
     * Get config list
156
     *
157
     * @return array
158
     */
159
    public function getConfig()
160
    {
161
        $output = $this->runCommand(array(
162
            'command' => 'config',
163
            '--list' => true,
164
        ));
165
166
        return OutputParser::parseList($output);
167
    }
168
169
    /**
170
     * Set work dir
171
     *
172
     * @param string $workingDir
173
     */
174
    public function setWorkingDir($workingDir)
175
    {
176
        $this->workingDir = $workingDir;
177
    }
178
179
    /**
180
     * Run composer command
181
     *
182
     * @param array $commands
183
     * @return string
184
     */
185
    public function runCommand($commands)
186
    {
187
        $this->init();
188
        $commands['--working-dir'] = $this->workingDir;
189
        $commands['--no-ansi'] = 1;
190
        $input = new ArrayInput($commands);
191
        $output = new BufferedOutput();
192
193
        $exitCode = $this->consoleApplication->run($input, $output);
194
195
        $log = $output->fetch();
196
        if ($exitCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $exitCode of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
197
            log_error($log);
198
            throw new \RuntimeException($log);
199
        }
200
        log_info($log, $commands);
201
202
        return $log;
203
    }
204
205
    /**
206
     * Init composer console application
207
     */
208
    private function init()
209
    {
210
        set_time_limit(0);
211
        @ini_set('memory_limit', '1536M');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
212
        // Config for some environment
213
        putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer');
214
        $consoleApplication = new Application();
215
        $consoleApplication->resetComposer();
216
        $consoleApplication->setAutoExit(false);
217
        $this->consoleApplication = $consoleApplication;
218
        $this->workingDir = $this->workingDir ? $this->workingDir : $this->appConfig['root_dir'];
219
    }
220
221
    /**
222
     * Get version of composer
223
     * @return null|string
224
     */
225
    public function composerVersion()
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
226
    {
227
        $this->init();
228
        $output = $this->runCommand(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
229
            '--version' => true
230
        ));
231
232
        return OutputParser::parseComposerVersion($output);
233
    }
234
235
    /**
236
     * Get mode
237
     * @return mixed|string
238
     */
239
    public function getMode()
240
    {
241
        return 'API';
242
    }
243
}
244