Completed
Push — master ( 092ec7...c255e1 )
by Victor
9s
created

AppManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 85
Duplicated Lines 18.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 37.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 3
dl 16
loc 85
ccs 17
cts 45
cp 0.3778
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A disableNotShippedApps() 8 12 4
A reenableNotShippedApps() 8 10 4
A getAllApps() 0 5 1
A getShippedApps() 0 5 1
A disableApp() 0 8 2
A enableApp() 0 9 2
A getAppPath() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
		} 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
		} catch (\Exception $e){
62
			return false;
63
		}
64 1
		return true;
65
	}
66
67
	public function disableNotShippedApps(OutputInterface $output = null){
68
		$notShippedApps = $this->occRunner->runJson('app:list --shipped false');
69
		$appsToDisable = array_keys($notShippedApps['enabled']);
70 View Code Duplication
		foreach ($appsToDisable as $appId){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71
			$result = $this->disableApp($appId);
72
			$status = $result ? '<info>success</info>' : '<error>failed</error>';
73
			if (!is_null($output)){
74
				$message = sprintf('Disable app %s: [%s]', $appId, $status);
75
				$output->writeln($message);
76
			}
77
		}
78
	}
79
80
	public function reenableNotShippedApps(OutputInterface $output = null){
81 View Code Duplication
		foreach ($this->disabledApps as $appId){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
			$result = $this->enableApp($appId);
83
			$status = $result ? '<info>success</info>' : '<error>failed</error>';
84
			if (!is_null($output)){
85
				$message = sprintf('Enable app %s: [%s]', $appId, $status);
86
				$output->writeln($message);
87
			}
88
		}
89
	}
90
91
	public function getAllApps(){
92
		$shippedApps = $this->occRunner->runJson('app:list');
93
		$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
94
		return $allApps;
95
	}
96
97 2
	public function getShippedApps(){
98 2
		$shippedApps = $this->occRunner->runJson('app:list --shipped true');
99 2
		$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
100 2
		return $allApps;
101
	}
102
103 1
	public function getAppPath($appId){
104
		try {
105 1
			$response = $this->occRunner->run('app:getpath ' . ProcessUtils::escapeArgument($appId));
106
		} catch (\Exception $e) {
107
			return '';
108
		}
109 1
		return trim($response);
110
	}
111
112
}
113