Passed
Push — master ( e275dc...aad8b3 )
by Joas
13:25 queued 13s
created

APIController::getAppList()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 34
nc 4
nop 1
dl 0
loc 52
rs 8.7537
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
namespace OCA\UpdateNotification\Controller;
28
29
use OC\App\AppStore\Fetcher\AppFetcher;
30
use OCP\App\AppPathNotFoundException;
31
use OCP\App\IAppManager;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCP\AppFramework\OCSController;
35
use OCP\IConfig;
36
use OCP\IRequest;
37
use OCP\IUserSession;
38
use OCP\L10N\IFactory;
39
40
class APIController extends OCSController {
41
42
	/** @var IConfig */
43
	protected $config;
44
45
	/** @var IAppManager */
46
	protected $appManager;
47
48
	/** @var AppFetcher */
49
	protected $appFetcher;
50
51
	/** @var IFactory */
52
	protected $l10nFactory;
53
54
	/** @var IUserSession */
55
	protected $userSession;
56
57
	/** @var string */
58
	protected $language;
59
60
	/**
61
	 * List of apps that were in the appstore but are now shipped and don't have
62
	 * a compatible update available.
63
	 *
64
	 * @var array<string, int>
65
	 */
66
	protected array $appsShippedInFutureVersion = [
67
		'bruteforcesettings' => 25,
68
		'suspicious_login' => 25,
69
		'twofactor_totp' => 25,
70
	];
71
72
	public function __construct(string $appName,
73
								IRequest $request,
74
								IConfig $config,
75
								IAppManager $appManager,
76
								AppFetcher $appFetcher,
77
								IFactory $l10nFactory,
78
								IUserSession $userSession) {
79
		parent::__construct($appName, $request);
80
81
		$this->config = $config;
82
		$this->appManager = $appManager;
83
		$this->appFetcher = $appFetcher;
84
		$this->l10nFactory = $l10nFactory;
85
		$this->userSession = $userSession;
86
	}
87
88
	/**
89
	 * @param string $newVersion
90
	 * @return DataResponse
91
	 */
92
	public function getAppList(string $newVersion): DataResponse {
93
		if (!$this->config->getSystemValue('appstoreenabled', true)) {
94
			return new DataResponse([
95
				'appstore_disabled' => true,
96
			], Http::STATUS_NOT_FOUND);
97
		}
98
99
		// Get list of installed custom apps
100
		$installedApps = $this->appManager->getInstalledApps();
101
		$installedApps = array_filter($installedApps, function ($app) {
102
			try {
103
				$this->appManager->getAppPath($app);
104
			} catch (AppPathNotFoundException $e) {
105
				return false;
106
			}
107
			return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]);
108
		});
109
110
		if (empty($installedApps)) {
111
			return new DataResponse([
112
				'missing' => [],
113
				'available' => [],
114
			]);
115
		}
116
117
		$this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
118
119
		// Apps available on the app store for that version
120
		$availableApps = array_map(static function (array $app) {
121
			return $app['id'];
122
		}, $this->appFetcher->get());
123
124
		if (empty($availableApps)) {
125
			return new DataResponse([
126
				'appstore_disabled' => false,
127
				'already_on_latest' => false,
128
			], Http::STATUS_NOT_FOUND);
129
		}
130
131
		$this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
132
133
		$missing = array_diff($installedApps, $availableApps);
134
		$missing = array_map([$this, 'getAppDetails'], $missing);
135
		sort($missing);
136
137
		$available = array_intersect($installedApps, $availableApps);
138
		$available = array_map([$this, 'getAppDetails'], $available);
139
		sort($available);
140
141
		return new DataResponse([
142
			'missing' => $missing,
143
			'available' => $available,
144
		]);
145
	}
146
147
	/**
148
	 * Get translated app name
149
	 *
150
	 * @param string $appId
151
	 * @return string[]
152
	 */
153
	protected function getAppDetails(string $appId): array {
154
		$app = $this->appManager->getAppInfo($appId, false, $this->language);
155
		return [
156
			'appId' => $appId,
157
			'appName' => $app['name'] ?? $appId,
158
		];
159
	}
160
}
161