Completed
Pull Request — master (#216)
by Thomas
03:52
created

OccRunner   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 29.4%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 36
ccs 5
cts 17
cp 0.294
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A run() 0 11 2
A runJson() 0 8 2
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 Symfony\Component\Process\Process;
25
use Symfony\Component\Process\Exception\ProcessFailedException;
26
use Owncloud\Updater\Utils\Locator;
27
28
class OccRunner {
29
	/**
30
	 * @var Locator $locator
31
	 */
32
	protected $locator;
33
34
	/**
35
	 *
36
	 * @param Locator $locator
37
	 */
38
	public function __construct(Locator $locator){
39
		$this->locator = $locator;
40
	}
41
42
	public function run($args){
43
		$occPath = $this->locator->getPathToOccFile();
44
		$cmd = "php $occPath $args";
45
		$process = new Process($cmd);
46
		$process->run();
47
48
		if (!$process->isSuccessful()){
49
			throw new ProcessFailedException($process);
50
		}
51
		return $process->getOutput();
52
	}
53
54 3
	public function runJson($args){
55 3
		$plain = $this->run($args . '  --output "json"');
56 3
		$decoded = json_decode($plain, true);
57 3
		if (!is_array($decoded)){
58
			throw new \UnexpectedValueException('Could not parse a response for ' . $args . '. Please check if the current shell user can run occ command. Raw output: ' . PHP_EOL . $plain);
59
		}
60 3
		return $decoded;
61
	}
62
63
}
64