Completed
Pull Request — master (#355)
by Victor
02:52
created

OccRunner::run()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 23
ccs 0
cts 19
cp 0
rs 8.5906
cc 6
eloc 18
nc 8
nop 3
crap 42
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
use GuzzleHttp\Client;
25
use Owncloud\Updater\Console\Application;
26
use Symfony\Component\Process\Process;
27
use Symfony\Component\Process\ProcessUtils;
28
use Symfony\Component\Process\Exception\ProcessFailedException;
29
use Owncloud\Updater\Utils\Locator;
30
31
class OccRunner {
32
	/**
33
	 * @var Locator $locator
34
	 */
35
	protected $locator;
36
37
	/**
38
	 * @var bool
39
	 */
40
	protected $canUseProcess;
41
42
	/**
43
	 *
44
	 * @param Locator $locator
45
	 */
46
	public function __construct(Locator $locator){
47
		$this->locator = $locator;
48
49
		$disabled = explode(',', ini_get('disable_functions'));
50
		$this->canUseProcess = function_exists('proc_open') && !in_array('proc_open', $disabled);
51
		// TODO: remove
52
		$this->canUseProcess = false;
53
	}
54
55
	public function run($command, $args = [], $asJson = false){
56
		if ($this->canUseProcess){
57
			$extra = $asJson ? '--output=json' : '';
58
			$cmdLine = trim($command . ' ' . $extra);
59
			foreach ($args as $optionTitle => $optionValue){
60
				if (strpos($optionTitle, '--') === 0){
61
					$line = trim("$optionTitle $optionValue");
62
				} else {
63
					$line = $optionValue;
64
				}
65
				$escapedLine = ProcessUtils::escapeArgument($line);
66
				$cmdLine .= " $escapedLine";
67
			}
68
			return $this->runAsProcess($cmdLine);
69
		} else {
70
			if ($asJson){
71
				$args['--output'] = 'json';
72
			}
73
			$response = $this->runAsRequest($command, $args);
74
			$decodedResponse = json_decode($response, true);
75
			return $decodedResponse['response'];
76
		}
77
	}
78
79 3
	public function runJson($command, $args = []){
80 3
		$plain = $this->run($command, $args, true);
81 3
		$decoded = json_decode($plain, true);
82 3
		if (!is_array($decoded)){
83
			throw new \UnexpectedValueException('Could not parse a response for ' . $command . '. Please check if the current shell user can run occ command. Raw output: ' . PHP_EOL . $plain);
84
		}
85 3
		return $decoded;
86
	}
87
88
	private function runAsRequest($command, $args){
89
		$container = Application::$container;
90
		$application = $container['application'];
91
		$client = new Client();
92
		$request = $client->createRequest(
93
			'POST',
94
			$application->getEndpoint() . $command,
95
			[
96
				'timeout' => 0,
97
				'json' => [
98
					'token' => $application->getAuthToken(),
99
					'params'=> $args
100
				]
101
			]
102
		);
103
104
		$response = $client->send($request);
105
		$responseBody = $response->getBody()->getContents();
106
		return $responseBody;
107
	}
108
109
	private function runAsProcess($cmdLine){
110
		$occPath = $this->locator->getPathToOccFile();
111
		$cmd = "php $occPath --no-warnings $cmdLine";
112
		$process = new Process($cmd);
113
		$process->setTimeout(null);
114
		$process->run();
115
116
		if (!$process->isSuccessful()){
117
			throw new ProcessFailedException($process);
118
		}
119
		return $process->getOutput();
120
	}
121
}
122