Passed
Push — master ( 0559b6...302c10 )
by Morris
11:14 queued 10s
created
lib/private/App/AppStore/Fetcher/AppFetcher.php 2 patches
Indentation   +118 added lines, -118 removed lines patch added patch discarded remove patch
@@ -37,122 +37,122 @@
 block discarded – undo
37 37
 
38 38
 class AppFetcher extends Fetcher {
39 39
 
40
-	/** @var CompareVersion */
41
-	private $compareVersion;
42
-
43
-	/** @var bool */
44
-	private $ignoreMaxVersion;
45
-
46
-	/**
47
-	 * @param Factory $appDataFactory
48
-	 * @param IClientService $clientService
49
-	 * @param ITimeFactory $timeFactory
50
-	 * @param IConfig $config
51
-	 * @param CompareVersion $compareVersion
52
-	 * @param ILogger $logger
53
-	 */
54
-	public function __construct(Factory $appDataFactory,
55
-								IClientService $clientService,
56
-								ITimeFactory $timeFactory,
57
-								IConfig $config,
58
-								CompareVersion $compareVersion,
59
-								ILogger $logger) {
60
-		parent::__construct(
61
-			$appDataFactory,
62
-			$clientService,
63
-			$timeFactory,
64
-			$config,
65
-			$logger
66
-		);
67
-
68
-		$this->fileName = 'apps.json';
69
-		$this->setEndpoint();
70
-		$this->compareVersion = $compareVersion;
71
-		$this->ignoreMaxVersion = true;
72
-	}
73
-
74
-	/**
75
-	 * Only returns the latest compatible app release in the releases array
76
-	 *
77
-	 * @param string $ETag
78
-	 * @param string $content
79
-	 *
80
-	 * @return array
81
-	 */
82
-	protected function fetch($ETag, $content) {
83
-		/** @var mixed[] $response */
84
-		$response = parent::fetch($ETag, $content);
85
-
86
-		$allowPreReleases = $this->getChannel() === 'beta' || $this->getChannel() === 'daily';
87
-		$allowNightly = $this->getChannel() === 'daily';
88
-
89
-		foreach($response['data'] as $dataKey => $app) {
90
-			$releases = [];
91
-
92
-			// Filter all compatible releases
93
-			foreach($app['releases'] as $release) {
94
-				// Exclude all nightly and pre-releases if required
95
-				if (($allowNightly || $release['isNightly'] === false)
96
-					&& ($allowPreReleases || strpos($release['version'], '-') === false)) {
97
-					// Exclude all versions not compatible with the current version
98
-					try {
99
-						$versionParser = new VersionParser();
100
-						$version = $versionParser->getVersion($release['rawPlatformVersionSpec']);
101
-						$ncVersion = $this->getVersion();
102
-						$min = $version->getMinimumVersion();
103
-						$max = $version->getMaximumVersion();
104
-						$minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>=');
105
-						$maxFulfilled = $max !== '' &&
106
-							$this->compareVersion->isCompatible($ncVersion, $max, '<=');
107
-						if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled)) {
108
-							$releases[] = $release;
109
-						}
110
-					} catch (\InvalidArgumentException $e) {
111
-						$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
112
-					}
113
-				}
114
-			}
115
-
116
-			if (empty($releases)) {
117
-				// Remove apps that don't have a matching release
118
-				continue;
119
-			}
120
-
121
-			// Get the highest version
122
-			$versions = [];
123
-			foreach($releases as $release) {
124
-				$versions[] = $release['version'];
125
-			}
126
-			usort($versions, 'version_compare');
127
-			$versions = array_reverse($versions);
128
-			if(isset($versions[0])) {
129
-				$highestVersion = $versions[0];
130
-				foreach ($releases as $release) {
131
-					if ((string)$release['version'] === (string)$highestVersion) {
132
-						$response['data'][$dataKey]['releases'] = [$release];
133
-						break;
134
-					}
135
-				}
136
-			}
137
-		}
138
-
139
-		$response['data'] = array_values($response['data']);
140
-		return $response;
141
-	}
142
-
143
-	private function setEndpoint() {
144
-		$this->endpointUrl = 'https://apps.nextcloud.com/api/v1/apps.json';
145
-	}
146
-
147
-	/**
148
-	 * @param string $version
149
-	 * @param string $fileName
150
-	 * @param bool $ignoreMaxVersion
151
-	 */
152
-	public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) {
153
-		parent::setVersion($version);
154
-		$this->fileName = $fileName;
155
-		$this->ignoreMaxVersion = $ignoreMaxVersion;
156
-		$this->setEndpoint();
157
-	}
40
+    /** @var CompareVersion */
41
+    private $compareVersion;
42
+
43
+    /** @var bool */
44
+    private $ignoreMaxVersion;
45
+
46
+    /**
47
+     * @param Factory $appDataFactory
48
+     * @param IClientService $clientService
49
+     * @param ITimeFactory $timeFactory
50
+     * @param IConfig $config
51
+     * @param CompareVersion $compareVersion
52
+     * @param ILogger $logger
53
+     */
54
+    public function __construct(Factory $appDataFactory,
55
+                                IClientService $clientService,
56
+                                ITimeFactory $timeFactory,
57
+                                IConfig $config,
58
+                                CompareVersion $compareVersion,
59
+                                ILogger $logger) {
60
+        parent::__construct(
61
+            $appDataFactory,
62
+            $clientService,
63
+            $timeFactory,
64
+            $config,
65
+            $logger
66
+        );
67
+
68
+        $this->fileName = 'apps.json';
69
+        $this->setEndpoint();
70
+        $this->compareVersion = $compareVersion;
71
+        $this->ignoreMaxVersion = true;
72
+    }
73
+
74
+    /**
75
+     * Only returns the latest compatible app release in the releases array
76
+     *
77
+     * @param string $ETag
78
+     * @param string $content
79
+     *
80
+     * @return array
81
+     */
82
+    protected function fetch($ETag, $content) {
83
+        /** @var mixed[] $response */
84
+        $response = parent::fetch($ETag, $content);
85
+
86
+        $allowPreReleases = $this->getChannel() === 'beta' || $this->getChannel() === 'daily';
87
+        $allowNightly = $this->getChannel() === 'daily';
88
+
89
+        foreach($response['data'] as $dataKey => $app) {
90
+            $releases = [];
91
+
92
+            // Filter all compatible releases
93
+            foreach($app['releases'] as $release) {
94
+                // Exclude all nightly and pre-releases if required
95
+                if (($allowNightly || $release['isNightly'] === false)
96
+                    && ($allowPreReleases || strpos($release['version'], '-') === false)) {
97
+                    // Exclude all versions not compatible with the current version
98
+                    try {
99
+                        $versionParser = new VersionParser();
100
+                        $version = $versionParser->getVersion($release['rawPlatformVersionSpec']);
101
+                        $ncVersion = $this->getVersion();
102
+                        $min = $version->getMinimumVersion();
103
+                        $max = $version->getMaximumVersion();
104
+                        $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>=');
105
+                        $maxFulfilled = $max !== '' &&
106
+                            $this->compareVersion->isCompatible($ncVersion, $max, '<=');
107
+                        if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled)) {
108
+                            $releases[] = $release;
109
+                        }
110
+                    } catch (\InvalidArgumentException $e) {
111
+                        $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
112
+                    }
113
+                }
114
+            }
115
+
116
+            if (empty($releases)) {
117
+                // Remove apps that don't have a matching release
118
+                continue;
119
+            }
120
+
121
+            // Get the highest version
122
+            $versions = [];
123
+            foreach($releases as $release) {
124
+                $versions[] = $release['version'];
125
+            }
126
+            usort($versions, 'version_compare');
127
+            $versions = array_reverse($versions);
128
+            if(isset($versions[0])) {
129
+                $highestVersion = $versions[0];
130
+                foreach ($releases as $release) {
131
+                    if ((string)$release['version'] === (string)$highestVersion) {
132
+                        $response['data'][$dataKey]['releases'] = [$release];
133
+                        break;
134
+                    }
135
+                }
136
+            }
137
+        }
138
+
139
+        $response['data'] = array_values($response['data']);
140
+        return $response;
141
+    }
142
+
143
+    private function setEndpoint() {
144
+        $this->endpointUrl = 'https://apps.nextcloud.com/api/v1/apps.json';
145
+    }
146
+
147
+    /**
148
+     * @param string $version
149
+     * @param string $fileName
150
+     * @param bool $ignoreMaxVersion
151
+     */
152
+    public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) {
153
+        parent::setVersion($version);
154
+        $this->fileName = $fileName;
155
+        $this->ignoreMaxVersion = $ignoreMaxVersion;
156
+        $this->setEndpoint();
157
+    }
158 158
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -86,11 +86,11 @@  discard block
 block discarded – undo
86 86
 		$allowPreReleases = $this->getChannel() === 'beta' || $this->getChannel() === 'daily';
87 87
 		$allowNightly = $this->getChannel() === 'daily';
88 88
 
89
-		foreach($response['data'] as $dataKey => $app) {
89
+		foreach ($response['data'] as $dataKey => $app) {
90 90
 			$releases = [];
91 91
 
92 92
 			// Filter all compatible releases
93
-			foreach($app['releases'] as $release) {
93
+			foreach ($app['releases'] as $release) {
94 94
 				// Exclude all nightly and pre-releases if required
95 95
 				if (($allowNightly || $release['isNightly'] === false)
96 96
 					&& ($allowPreReleases || strpos($release['version'], '-') === false)) {
@@ -120,15 +120,15 @@  discard block
 block discarded – undo
120 120
 
121 121
 			// Get the highest version
122 122
 			$versions = [];
123
-			foreach($releases as $release) {
123
+			foreach ($releases as $release) {
124 124
 				$versions[] = $release['version'];
125 125
 			}
126 126
 			usort($versions, 'version_compare');
127 127
 			$versions = array_reverse($versions);
128
-			if(isset($versions[0])) {
128
+			if (isset($versions[0])) {
129 129
 				$highestVersion = $versions[0];
130 130
 				foreach ($releases as $release) {
131
-					if ((string)$release['version'] === (string)$highestVersion) {
131
+					if ((string) $release['version'] === (string) $highestVersion) {
132 132
 						$response['data'][$dataKey]['releases'] = [$release];
133 133
 						break;
134 134
 					}
Please login to merge, or discard this patch.
lib/private/App/AppStore/Fetcher/Fetcher.php 1 patch
Indentation   +179 added lines, -179 removed lines patch added patch discarded remove patch
@@ -39,183 +39,183 @@
 block discarded – undo
39 39
 use OCP\Util;
40 40
 
41 41
 abstract class Fetcher {
42
-	const INVALIDATE_AFTER_SECONDS = 300;
43
-
44
-	/** @var IAppData */
45
-	protected $appData;
46
-	/** @var IClientService */
47
-	protected $clientService;
48
-	/** @var ITimeFactory */
49
-	protected $timeFactory;
50
-	/** @var IConfig */
51
-	protected $config;
52
-	/** @var Ilogger */
53
-	protected $logger;
54
-	/** @var string */
55
-	protected $fileName;
56
-	/** @var string */
57
-	protected $endpointUrl;
58
-	/** @var string */
59
-	protected $version;
60
-	/** @var string */
61
-	protected $channel;
62
-
63
-	/**
64
-	 * @param Factory $appDataFactory
65
-	 * @param IClientService $clientService
66
-	 * @param ITimeFactory $timeFactory
67
-	 * @param IConfig $config
68
-	 * @param ILogger $logger
69
-	 */
70
-	public function __construct(Factory $appDataFactory,
71
-								IClientService $clientService,
72
-								ITimeFactory $timeFactory,
73
-								IConfig $config,
74
-								ILogger $logger) {
75
-		$this->appData = $appDataFactory->get('appstore');
76
-		$this->clientService = $clientService;
77
-		$this->timeFactory = $timeFactory;
78
-		$this->config = $config;
79
-		$this->logger = $logger;
80
-	}
81
-
82
-	/**
83
-	 * Fetches the response from the server
84
-	 *
85
-	 * @param string $ETag
86
-	 * @param string $content
87
-	 *
88
-	 * @return array
89
-	 */
90
-	protected function fetch($ETag, $content) {
91
-		$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
92
-
93
-		if (!$appstoreenabled) {
94
-			return [];
95
-		}
96
-
97
-		$options = [
98
-			'timeout' => 10,
99
-		];
100
-
101
-		if ($ETag !== '') {
102
-			$options['headers'] = [
103
-				'If-None-Match' => $ETag,
104
-			];
105
-		}
106
-
107
-		$client = $this->clientService->newClient();
108
-		$response = $client->get($this->endpointUrl, $options);
109
-
110
-		$responseJson = [];
111
-		if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
112
-			$responseJson['data'] = json_decode($content, true);
113
-		} else {
114
-			$responseJson['data'] = json_decode($response->getBody(), true);
115
-			$ETag = $response->getHeader('ETag');
116
-		}
117
-
118
-		$responseJson['timestamp'] = $this->timeFactory->getTime();
119
-		$responseJson['ncversion'] = $this->getVersion();
120
-		if ($ETag !== '') {
121
-			$responseJson['ETag'] = $ETag;
122
-		}
123
-
124
-		return $responseJson;
125
-	}
126
-
127
-	/**
128
-	 * Returns the array with the categories on the appstore server
129
-	 *
130
-	 * @return array
131
-	 */
132
-	public function get() {
133
-		$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
134
-		$internetavailable = $this->config->getSystemValue('has_internet_connection', true);
135
-
136
-		if (!$appstoreenabled || !$internetavailable) {
137
-			return [];
138
-		}
139
-
140
-		$rootFolder = $this->appData->getFolder('/');
141
-
142
-		$ETag = '';
143
-		$content = '';
144
-
145
-		try {
146
-			// File does already exists
147
-			$file = $rootFolder->getFile($this->fileName);
148
-			$jsonBlob = json_decode($file->getContent(), true);
149
-			if (is_array($jsonBlob)) {
150
-
151
-				// No caching when the version has been updated
152
-				if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
153
-
154
-					// If the timestamp is older than 300 seconds request the files new
155
-					if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
156
-						return $jsonBlob['data'];
157
-					}
158
-
159
-					if (isset($jsonBlob['ETag'])) {
160
-						$ETag = $jsonBlob['ETag'];
161
-						$content = json_encode($jsonBlob['data']);
162
-					}
163
-				}
164
-			}
165
-		} catch (NotFoundException $e) {
166
-			// File does not already exists
167
-			$file = $rootFolder->newFile($this->fileName);
168
-		}
169
-
170
-		// Refresh the file content
171
-		try {
172
-			$responseJson = $this->fetch($ETag, $content);
173
-			$file->putContent(json_encode($responseJson));
174
-			return json_decode($file->getContent(), true)['data'];
175
-		} catch (ConnectException $e) {
176
-			$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']);
177
-			return [];
178
-		} catch (\Exception $e) {
179
-			$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]);
180
-			return [];
181
-		}
182
-	}
183
-
184
-	/**
185
-	 * Get the currently Nextcloud version
186
-	 * @return string
187
-	 */
188
-	protected function getVersion() {
189
-		if ($this->version === null) {
190
-			$this->version = $this->config->getSystemValue('version', '0.0.0');
191
-		}
192
-		return $this->version;
193
-	}
194
-
195
-	/**
196
-	 * Set the current Nextcloud version
197
-	 * @param string $version
198
-	 */
199
-	public function setVersion(string $version) {
200
-		$this->version = $version;
201
-	}
202
-
203
-	/**
204
-	 * Get the currently Nextcloud update channel
205
-	 * @return string
206
-	 */
207
-	protected function getChannel() {
208
-		if ($this->channel === null) {
209
-			$this->channel = \OC_Util::getChannel();
210
-		}
211
-		return $this->channel;
212
-	}
213
-
214
-	/**
215
-	 * Set the current Nextcloud update channel
216
-	 * @param string $channel
217
-	 */
218
-	public function setChannel(string $channel) {
219
-		$this->channel = $channel;
220
-	}
42
+    const INVALIDATE_AFTER_SECONDS = 300;
43
+
44
+    /** @var IAppData */
45
+    protected $appData;
46
+    /** @var IClientService */
47
+    protected $clientService;
48
+    /** @var ITimeFactory */
49
+    protected $timeFactory;
50
+    /** @var IConfig */
51
+    protected $config;
52
+    /** @var Ilogger */
53
+    protected $logger;
54
+    /** @var string */
55
+    protected $fileName;
56
+    /** @var string */
57
+    protected $endpointUrl;
58
+    /** @var string */
59
+    protected $version;
60
+    /** @var string */
61
+    protected $channel;
62
+
63
+    /**
64
+     * @param Factory $appDataFactory
65
+     * @param IClientService $clientService
66
+     * @param ITimeFactory $timeFactory
67
+     * @param IConfig $config
68
+     * @param ILogger $logger
69
+     */
70
+    public function __construct(Factory $appDataFactory,
71
+                                IClientService $clientService,
72
+                                ITimeFactory $timeFactory,
73
+                                IConfig $config,
74
+                                ILogger $logger) {
75
+        $this->appData = $appDataFactory->get('appstore');
76
+        $this->clientService = $clientService;
77
+        $this->timeFactory = $timeFactory;
78
+        $this->config = $config;
79
+        $this->logger = $logger;
80
+    }
81
+
82
+    /**
83
+     * Fetches the response from the server
84
+     *
85
+     * @param string $ETag
86
+     * @param string $content
87
+     *
88
+     * @return array
89
+     */
90
+    protected function fetch($ETag, $content) {
91
+        $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
92
+
93
+        if (!$appstoreenabled) {
94
+            return [];
95
+        }
96
+
97
+        $options = [
98
+            'timeout' => 10,
99
+        ];
100
+
101
+        if ($ETag !== '') {
102
+            $options['headers'] = [
103
+                'If-None-Match' => $ETag,
104
+            ];
105
+        }
106
+
107
+        $client = $this->clientService->newClient();
108
+        $response = $client->get($this->endpointUrl, $options);
109
+
110
+        $responseJson = [];
111
+        if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
112
+            $responseJson['data'] = json_decode($content, true);
113
+        } else {
114
+            $responseJson['data'] = json_decode($response->getBody(), true);
115
+            $ETag = $response->getHeader('ETag');
116
+        }
117
+
118
+        $responseJson['timestamp'] = $this->timeFactory->getTime();
119
+        $responseJson['ncversion'] = $this->getVersion();
120
+        if ($ETag !== '') {
121
+            $responseJson['ETag'] = $ETag;
122
+        }
123
+
124
+        return $responseJson;
125
+    }
126
+
127
+    /**
128
+     * Returns the array with the categories on the appstore server
129
+     *
130
+     * @return array
131
+     */
132
+    public function get() {
133
+        $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
134
+        $internetavailable = $this->config->getSystemValue('has_internet_connection', true);
135
+
136
+        if (!$appstoreenabled || !$internetavailable) {
137
+            return [];
138
+        }
139
+
140
+        $rootFolder = $this->appData->getFolder('/');
141
+
142
+        $ETag = '';
143
+        $content = '';
144
+
145
+        try {
146
+            // File does already exists
147
+            $file = $rootFolder->getFile($this->fileName);
148
+            $jsonBlob = json_decode($file->getContent(), true);
149
+            if (is_array($jsonBlob)) {
150
+
151
+                // No caching when the version has been updated
152
+                if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
153
+
154
+                    // If the timestamp is older than 300 seconds request the files new
155
+                    if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
156
+                        return $jsonBlob['data'];
157
+                    }
158
+
159
+                    if (isset($jsonBlob['ETag'])) {
160
+                        $ETag = $jsonBlob['ETag'];
161
+                        $content = json_encode($jsonBlob['data']);
162
+                    }
163
+                }
164
+            }
165
+        } catch (NotFoundException $e) {
166
+            // File does not already exists
167
+            $file = $rootFolder->newFile($this->fileName);
168
+        }
169
+
170
+        // Refresh the file content
171
+        try {
172
+            $responseJson = $this->fetch($ETag, $content);
173
+            $file->putContent(json_encode($responseJson));
174
+            return json_decode($file->getContent(), true)['data'];
175
+        } catch (ConnectException $e) {
176
+            $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']);
177
+            return [];
178
+        } catch (\Exception $e) {
179
+            $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]);
180
+            return [];
181
+        }
182
+    }
183
+
184
+    /**
185
+     * Get the currently Nextcloud version
186
+     * @return string
187
+     */
188
+    protected function getVersion() {
189
+        if ($this->version === null) {
190
+            $this->version = $this->config->getSystemValue('version', '0.0.0');
191
+        }
192
+        return $this->version;
193
+    }
194
+
195
+    /**
196
+     * Set the current Nextcloud version
197
+     * @param string $version
198
+     */
199
+    public function setVersion(string $version) {
200
+        $this->version = $version;
201
+    }
202
+
203
+    /**
204
+     * Get the currently Nextcloud update channel
205
+     * @return string
206
+     */
207
+    protected function getChannel() {
208
+        if ($this->channel === null) {
209
+            $this->channel = \OC_Util::getChannel();
210
+        }
211
+        return $this->channel;
212
+    }
213
+
214
+    /**
215
+     * Set the current Nextcloud update channel
216
+     * @param string $channel
217
+     */
218
+    public function setChannel(string $channel) {
219
+        $this->channel = $channel;
220
+    }
221 221
 }
Please login to merge, or discard this patch.