Completed
Push — master ( 62ce3e...4b4091 )
by Thomas
02:42
created

AppManager::disableNotShippedApps()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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