Completed
Push — master ( 94c2f1...705483 )
by Morris
31s
created

AppFetcher   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
C fetch() 0 54 12
A setEndpoint() 0 9 1
A setVersion() 0 4 1
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
	/**
32
	 * @param IAppData $appData
33
	 * @param IClientService $clientService
34
	 * @param ITimeFactory $timeFactory
35
	 * @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...
36
	 */
37
	public function __construct(IAppData $appData,
38
								IClientService $clientService,
39
								ITimeFactory $timeFactory,
40
								IConfig $config) {
41
		parent::__construct(
42
			$appData,
43
			$clientService,
44
			$timeFactory,
45
			$config
46
		);
47
48
		$this->fileName = 'apps.json';
49
		$this->setEndpoint();
50
	}
51
52
	/**
53
	 * Only returns the latest compatible app release in the releases array
54
	 *
55
	 * @param string $ETag
56
	 * @param string $content
57
	 *
58
	 * @return array
59
	 */
60
	protected function fetch($ETag, $content) {
61
		/** @var mixed[] $response */
62
		$response = parent::fetch($ETag, $content);
63
64
		$ncVersion = $this->getVersion();
65
		$ncMajorVersion = explode('.', $ncVersion)[0];
66
		foreach($response['data'] as $dataKey => $app) {
67
			$releases = [];
68
69
			// Filter all compatible releases
70
			foreach($app['releases'] as $release) {
71
				// Exclude all nightly and pre-releases
72
				if($release['isNightly'] === false
73
					&& strpos($release['version'], '-') === false) {
74
					// Exclude all versions not compatible with the current version
75
					$versionParser = new VersionParser();
76
					$version = $versionParser->getVersion($release['rawPlatformVersionSpec']);
77
					if (
78
						// Major version is bigger or equals to the minimum version of the app
79
						version_compare($ncMajorVersion, $version->getMinimumVersion(), '>=')
80
						// Major version is smaller or equals to the maximum version of the app
81
						&& version_compare($ncMajorVersion, $version->getMaximumVersion(), '<=')
82
					) {
83
						$releases[] = $release;
84
					}
85
				}
86
			}
87
88
			// Get the highest version
89
			$versions = [];
90
			foreach($releases as $release) {
91
				$versions[] = $release['version'];
92
			}
93
			usort($versions, 'version_compare');
94
			$versions = array_reverse($versions);
95
			$compatible = false;
96
			if(isset($versions[0])) {
97
				$highestVersion = $versions[0];
98
				foreach ($releases as $release) {
99
					if ((string)$release['version'] === (string)$highestVersion) {
100
						$compatible = true;
101
						$response['data'][$dataKey]['releases'] = [$release];
102
						break;
103
					}
104
				}
105
			}
106
			if(!$compatible) {
107
				unset($response['data'][$dataKey]);
108
			}
109
		}
110
111
		$response['data'] = array_values($response['data']);
112
		return $response;
113
	}
114
115
	private function setEndpoint() {
116
		$versionArray = explode('.', $this->getVersion());
117
		$this->endpointUrl = sprintf(
118
			'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json',
119
			$versionArray[0],
120
			$versionArray[1],
121
			$versionArray[2]
122
		);
123
	}
124
125
	/**
126
	 * @param string $version
127
	 */
128
	public function setVersion($version) {
129
		parent::setVersion($version);
130
		$this->setEndpoint();
131
	}
132
}
133