Completed
Push — master ( 846b0d...c88112 )
by Morris
22:58
created

APIController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

3 Methods

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