Completed
Push — master ( 988917...cb69ac )
by Blizzz
13:42 queued 05:46
created

AppFetcher   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
C fetch() 0 56 11
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\App\AppStore\Fetcher;
23
24
use OC\App\AppStore\Version\VersionParser;
25
use OCP\AppFramework\Utility\ITimeFactory;
26
use OCP\Files\IAppData;
27
use OCP\Http\Client\IClientService;
28
use OCP\IConfig;
29
30
class AppFetcher extends Fetcher {
31
	/** @var IConfig */
32
	private $config;
33
34
	/**
35
	 * @param IAppData $appData
36
	 * @param IClientService $clientService
37
	 * @param ITimeFactory $timeFactory
38
	 * @param IConfig $config;
0 ignored issues
show
Documentation introduced by
There is no parameter named $config;. Did you maybe mean $config?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
39
	 */
40
	public function __construct(IAppData $appData,
41
								IClientService $clientService,
42
								ITimeFactory $timeFactory,
43
								IConfig $config) {
44
		parent::__construct(
45
			$appData,
46
			$clientService,
47
			$timeFactory
48
		);
49
50
		$this->fileName = 'apps.json';
51
		$this->config = $config;
52
53
		$versionArray = \OC_Util::getVersion();
54
		$this->endpointUrl = sprintf(
55
			'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json',
56
			$versionArray[0],
57
			$versionArray[1],
58
			$versionArray[2]
59
		);
60
	}
61
62
	/**
63
	 * Only returns the latest compatible app release in the releases array
64
	 *
65
	 * @return array
66
	 */
67
	protected function fetch() {
68
		$client = $this->clientService->newClient();
69
		$response = $client->get($this->endpointUrl);
70
		$responseJson = [];
71
		$responseJson['data'] = json_decode($response->getBody(), true);
72
		$responseJson['timestamp'] = $this->timeFactory->getTime();
73
		$response = $responseJson;
74
75
		$ncVersion = $this->config->getSystemValue('version');
76
		$ncMajorVersion = explode('.', $ncVersion)[0];
77
		foreach($response['data'] as $dataKey => $app) {
78
			$releases = [];
79
80
			// Filter all compatible releases
81
			foreach($app['releases'] as $release) {
82
				// Exclude all nightly releases
83
				if($release['isNightly'] === false) {
84
					// Exclude all versions not compatible with the current version
85
					$versionParser = new VersionParser();
86
					$version = $versionParser->getVersion($release['rawPlatformVersionSpec']);
87
88
					if(
89
						// Major version is bigger or equals to the minimum version of the app
90
						version_compare($ncMajorVersion, $version->getMinimumVersion(), '>=')
91
						// Major version is smaller or equals to the maximum version of the app
92
						&& version_compare($ncMajorVersion, $version->getMaximumVersion(), '<=')) {
93
						$releases[] = $release;
94
					}
95
				}
96
			}
97
98
			// Get the highest version
99
			$versions = [];
100
			foreach($releases as $release) {
101
				$versions[] = $release['version'];
102
			}
103
			usort($versions, 'version_compare');
104
			$versions = array_reverse($versions);
105
			$compatible = false;
106
			if(isset($versions[0])) {
107
				$highestVersion = $versions[0];
108
				foreach ($releases as $release) {
109
					if ((string)$release['version'] === (string)$highestVersion) {
110
						$compatible = true;
111
						$response['data'][$dataKey]['releases'] = [$release];
112
						break;
113
					}
114
				}
115
			}
116
			if(!$compatible) {
117
				unset($response['data'][$dataKey]);
118
			}
119
		}
120
121
		return $response;
122
	}
123
}
124