Completed
Push — master ( fcc3f1...b930c0 )
by Thomas
07:52
created

AppSettingsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 16
loc 118
rs 10
c 1
b 0
f 0
wmc 15
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
C listApps() 16 70 13
A getInstalledApps() 0 7 1

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 Christoph Wurst <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2016, ownCloud GmbH.
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Settings\Controller;
27
28
use OC\App\DependencyAnalyzer;
29
use OC\App\Platform;
30
use OCP\App\IAppManager;
31
use \OCP\AppFramework\Controller;
32
use OCP\IRequest;
33
use OCP\IL10N;
34
use OCP\IConfig;
35
36
/**
37
 * @package OC\Settings\Controller
38
 */
39
class AppSettingsController extends Controller {
40
	const CAT_ENABLED = 0;
41
	const CAT_DISABLED = 1;
42
43
	/** @var \OCP\IL10N */
44
	private $l10n;
45
	/** @var IConfig */
46
	private $config;
47
	/** @var IAppManager */
48
	private $appManager;
49
50
	/**
51
	 * @param string $appName
52
	 * @param IRequest $request
53
	 * @param IL10N $l10n
54
	 * @param IConfig $config
55
	 * @param IAppManager $appManager
56
	 */
57
	public function __construct($appName,
58
								IRequest $request,
59
								IL10N $l10n,
60
								IConfig $config,
61
								IAppManager $appManager) {
62
		parent::__construct($appName, $request);
63
		$this->l10n = $l10n;
64
		$this->config = $config;
65
		$this->appManager = $appManager;
66
	}
67
68
	/**
69
	 * Get all available apps in a category
70
	 *
71
	 * @param string $category
72
	 * @return array
73
	 */
74
	public function listApps($category = '') {
75
		if ($category === '') {
76
			$category = 'enabled';
77
		}
78
		switch ($category) {
79
			// installed apps
80
			case 'enabled':
81
				$apps = $this->getInstalledApps();
82 View Code Duplication
				usort($apps, function ($a, $b) {
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...
83
					$a = (string)$a['name'];
84
					$b = (string)$b['name'];
85
					if ($a === $b) {
86
						return 0;
87
					}
88
					return ($a < $b) ? -1 : 1;
89
				});
90
				break;
91
			// not-installed apps
92
			case 'disabled':
93
				$apps = \OC_App::listAllApps();
94
				$apps = array_filter($apps, function ($app) {
95
					return !$app['active'];
96
				});
97 View Code Duplication
				usort($apps, function ($a, $b) {
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...
98
					$a = (string)$a['name'];
99
					$b = (string)$b['name'];
100
					if ($a === $b) {
101
						return 0;
102
					}
103
					return ($a < $b) ? -1 : 1;
104
				});
105
				break;
106
			default:
107
				$apps = [];
108
109
				break;
110
		}
111
112
		// fix groups to be an array
113
		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
114
		$apps = array_map(function($app) use ($dependencyAnalyzer) {
115
116
			// fix groups
117
			$groups = [];
118
			if (is_string($app['groups'])) {
119
				$groups = json_decode($app['groups']);
120
			} elseif (is_array($app['groups'])) {
121
				$groups = $app['groups'];
122
			}
123
			$app['groups'] = $groups;
124
			$app['canUnInstall'] = !$app['active'] && $app['removable'];
125
126
			// fix licence vs license
127
			if (isset($app['license']) && !isset($app['licence'])) {
128
				$app['licence'] = $app['license'];
129
			}
130
131
			// analyse dependencies
132
			$missing = $dependencyAnalyzer->analyze($app);
133
			$app['canInstall'] = empty($missing);
134
			$app['missingDependencies'] = $missing;
135
136
			$app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['min-version']);
137
			$app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['max-version']);
138
139
			return $app;
140
		}, $apps);
141
142
		return ['apps' => $apps, 'status' => 'success'];
143
	}
144
145
	/**
146
	 * @return array
147
	 */
148
	private function getInstalledApps() {
149
		$apps = \OC_App::listAllApps();
150
		$apps = array_filter($apps, function ($app) {
151
			return $app['active'];
152
		});
153
		return $apps;
154
	}
155
156
}
157