Completed
Pull Request — master (#377)
by Victor
05:44
created

AppManager::getAllApps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 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\Console\Output\OutputInterface;
25
use Owncloud\Updater\Utils\OccRunner;
26
27
class AppManager {
28
29
	/**
30
	 * @var OccRunner $occRunner
31
	 */
32
	protected $occRunner;
33
34
	/**
35
	 * @var array $disabledApps
36
	 */
37
	protected $disabledApps = [];
38
39
	/**
40
	 *
41
	 * @param OccRunner $occRunner
42
	 */
43 5
	public function __construct(OccRunner $occRunner){
44 5
		$this->occRunner = $occRunner;
45 5
	}
46
47 1
	public function disableApp($appId){
48
		try{
49 1
			$this->occRunner->run('app:disable', ['app-id' => $appId]);
50
		} catch (\Exception $e){
51
			return false;
52
		}
53 1
		return true;
54
	}
55
56 1
	public function enableApp($appId){
57
		try{
58 1
			$this->occRunner->run('app:enable', ['app-id' => $appId]);
59 1
			array_unshift($this->disabledApps, $appId);
60
		} catch (\Exception $e){
61
			return false;
62
		}
63 1
		return true;
64
	}
65
66
	/**
67
	 * @param OutputInterface|null $output
68
	 */
69
	public function disableNotShippedApps(OutputInterface $output = null){
70
		$notShippedApps = $this->occRunner->runJson('app:list', ['--shipped' => 'false']);
71
		$appsToDisable = array_keys($notShippedApps['enabled']);
72
		foreach ($appsToDisable as $appId){
73
			$result = $this->disableApp($appId);
74
			$status = $result ? '<info>success</info>' : '<error>failed</error>';
75
			if ($result){
76
				$this->disabledApps[] = $appId;
77
			}
78
			if (!is_null($output)){
79
				$message = sprintf('Disable app %s: [%s]', $appId, $status);
80
				$output->writeln($message);
81
			}
82
		}
83
	}
84
85
	public function reenableNotShippedApps(OutputInterface $output = null){
86
		foreach ($this->disabledApps as $appId){
87
			$result = $this->enableApp($appId);
88
			$status = $result ? '<info>success</info>' : '<error>failed</error>';
89
			if (!is_null($output)){
90
				$message = sprintf('Enable app %s: [%s]', $appId, $status);
91
				$output->writeln($message);
92
			}
93
		}
94
	}
95
96
	public function getAllApps(){
97
		$shippedApps = $this->occRunner->runJson('app:list');
98
		$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
99
		return $allApps;
100
	}
101
102
	public function getNotShippedApps(){
103
		$shippedApps = $this->occRunner->runJson('app:list', ['--shipped' => 'false']);
104
		$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
105
		return $allApps;
106
	}
107
108 2
	public function getShippedApps(){
109 2
		$shippedApps = $this->occRunner->runJson('app:list', ['--shipped' => 'true']);
110 2
		$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
111 2
		return $allApps;
112
	}
113
114 1
	public function getAppPath($appId){
115
		try {
116 1
			$response = $this->occRunner->run('app:getpath', ['app-id' => $appId]);
117
		} catch (\Exception $e) {
118
			return '';
119
		}
120 1
		return trim($response);
121
	}
122
123
}
124