Passed
Push — master ( 74e577...526905 )
by Morris
11:32 queued 11s
created
lib/private/App/AppStore/Fetcher/Fetcher.php 2 patches
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -41,186 +41,186 @@
 block discarded – undo
41 41
 use OCP\ILogger;
42 42
 
43 43
 abstract class Fetcher {
44
-	public const INVALIDATE_AFTER_SECONDS = 3600;
45
-
46
-	/** @var IAppData */
47
-	protected $appData;
48
-	/** @var IClientService */
49
-	protected $clientService;
50
-	/** @var ITimeFactory */
51
-	protected $timeFactory;
52
-	/** @var IConfig */
53
-	protected $config;
54
-	/** @var Ilogger */
55
-	protected $logger;
56
-	/** @var string */
57
-	protected $fileName;
58
-	/** @var string */
59
-	protected $endpointName;
60
-	/** @var string */
61
-	protected $version;
62
-	/** @var string */
63
-	protected $channel;
64
-
65
-	/**
66
-	 * @param Factory $appDataFactory
67
-	 * @param IClientService $clientService
68
-	 * @param ITimeFactory $timeFactory
69
-	 * @param IConfig $config
70
-	 * @param ILogger $logger
71
-	 */
72
-	public function __construct(Factory $appDataFactory,
73
-								IClientService $clientService,
74
-								ITimeFactory $timeFactory,
75
-								IConfig $config,
76
-								ILogger $logger) {
77
-		$this->appData = $appDataFactory->get('appstore');
78
-		$this->clientService = $clientService;
79
-		$this->timeFactory = $timeFactory;
80
-		$this->config = $config;
81
-		$this->logger = $logger;
82
-	}
83
-
84
-	/**
85
-	 * Fetches the response from the server
86
-	 *
87
-	 * @param string $ETag
88
-	 * @param string $content
89
-	 *
90
-	 * @return array
91
-	 */
92
-	protected function fetch($ETag, $content) {
93
-		$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
94
-
95
-		if (!$appstoreenabled) {
96
-			return [];
97
-		}
98
-
99
-		$options = [
100
-			'timeout' => 10,
101
-			'headers' => ['Accept-Encoding' => 'gzip'],
102
-		];
103
-
104
-		if ($ETag !== '') {
105
-			$options['headers']['If-None-Match'] = $ETag;
106
-		}
107
-
108
-		$client = $this->clientService->newClient();
109
-		$response = $client->get($this->getEndpoint(), $options);
110
-
111
-		$responseJson = [];
112
-		if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
113
-			$responseJson['data'] = json_decode($content, true);
114
-		} else {
115
-			$responseJson['data'] = json_decode($response->getBody(), true);
116
-			$ETag = $response->getHeader('ETag');
117
-		}
118
-
119
-		$responseJson['timestamp'] = $this->timeFactory->getTime();
120
-		$responseJson['ncversion'] = $this->getVersion();
121
-		if ($ETag !== '') {
122
-			$responseJson['ETag'] = $ETag;
123
-		}
124
-
125
-		return $responseJson;
126
-	}
127
-
128
-	/**
129
-	 * Returns the array with the categories on the appstore server
130
-	 *
131
-	 * @return array
132
-	 */
133
-	public function get() {
134
-		$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
135
-		$internetavailable = $this->config->getSystemValue('has_internet_connection', true);
136
-
137
-		if (!$appstoreenabled || !$internetavailable) {
138
-			return [];
139
-		}
140
-
141
-		$rootFolder = $this->appData->getFolder('/');
142
-
143
-		$ETag = '';
144
-		$content = '';
145
-
146
-		try {
147
-			// File does already exists
148
-			$file = $rootFolder->getFile($this->fileName);
149
-			$jsonBlob = json_decode($file->getContent(), true);
150
-			if (is_array($jsonBlob)) {
151
-
152
-				// No caching when the version has been updated
153
-				if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
154
-
155
-					// If the timestamp is older than 3600 seconds request the files new
156
-					if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
157
-						return $jsonBlob['data'];
158
-					}
159
-
160
-					if (isset($jsonBlob['ETag'])) {
161
-						$ETag = $jsonBlob['ETag'];
162
-						$content = json_encode($jsonBlob['data']);
163
-					}
164
-				}
165
-			}
166
-		} catch (NotFoundException $e) {
167
-			// File does not already exists
168
-			$file = $rootFolder->newFile($this->fileName);
169
-		}
170
-
171
-		// Refresh the file content
172
-		try {
173
-			$responseJson = $this->fetch($ETag, $content);
174
-			$file->putContent(json_encode($responseJson));
175
-			return json_decode($file->getContent(), true)['data'];
176
-		} catch (ConnectException $e) {
177
-			$this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']);
178
-			return [];
179
-		} catch (\Exception $e) {
180
-			$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
181
-			return [];
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * Get the currently Nextcloud version
187
-	 * @return string
188
-	 */
189
-	protected function getVersion() {
190
-		if ($this->version === null) {
191
-			$this->version = $this->config->getSystemValue('version', '0.0.0');
192
-		}
193
-		return $this->version;
194
-	}
195
-
196
-	/**
197
-	 * Set the current Nextcloud version
198
-	 * @param string $version
199
-	 */
200
-	public function setVersion(string $version) {
201
-		$this->version = $version;
202
-	}
203
-
204
-	/**
205
-	 * Get the currently Nextcloud update channel
206
-	 * @return string
207
-	 */
208
-	protected function getChannel() {
209
-		if ($this->channel === null) {
210
-			$this->channel = \OC_Util::getChannel();
211
-		}
212
-		return $this->channel;
213
-	}
214
-
215
-	/**
216
-	 * Set the current Nextcloud update channel
217
-	 * @param string $channel
218
-	 */
219
-	public function setChannel(string $channel) {
220
-		$this->channel = $channel;
221
-	}
222
-
223
-	protected function getEndpoint(): string {
224
-		return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName;
225
-	}
44
+    public const INVALIDATE_AFTER_SECONDS = 3600;
45
+
46
+    /** @var IAppData */
47
+    protected $appData;
48
+    /** @var IClientService */
49
+    protected $clientService;
50
+    /** @var ITimeFactory */
51
+    protected $timeFactory;
52
+    /** @var IConfig */
53
+    protected $config;
54
+    /** @var Ilogger */
55
+    protected $logger;
56
+    /** @var string */
57
+    protected $fileName;
58
+    /** @var string */
59
+    protected $endpointName;
60
+    /** @var string */
61
+    protected $version;
62
+    /** @var string */
63
+    protected $channel;
64
+
65
+    /**
66
+     * @param Factory $appDataFactory
67
+     * @param IClientService $clientService
68
+     * @param ITimeFactory $timeFactory
69
+     * @param IConfig $config
70
+     * @param ILogger $logger
71
+     */
72
+    public function __construct(Factory $appDataFactory,
73
+                                IClientService $clientService,
74
+                                ITimeFactory $timeFactory,
75
+                                IConfig $config,
76
+                                ILogger $logger) {
77
+        $this->appData = $appDataFactory->get('appstore');
78
+        $this->clientService = $clientService;
79
+        $this->timeFactory = $timeFactory;
80
+        $this->config = $config;
81
+        $this->logger = $logger;
82
+    }
83
+
84
+    /**
85
+     * Fetches the response from the server
86
+     *
87
+     * @param string $ETag
88
+     * @param string $content
89
+     *
90
+     * @return array
91
+     */
92
+    protected function fetch($ETag, $content) {
93
+        $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
94
+
95
+        if (!$appstoreenabled) {
96
+            return [];
97
+        }
98
+
99
+        $options = [
100
+            'timeout' => 10,
101
+            'headers' => ['Accept-Encoding' => 'gzip'],
102
+        ];
103
+
104
+        if ($ETag !== '') {
105
+            $options['headers']['If-None-Match'] = $ETag;
106
+        }
107
+
108
+        $client = $this->clientService->newClient();
109
+        $response = $client->get($this->getEndpoint(), $options);
110
+
111
+        $responseJson = [];
112
+        if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
113
+            $responseJson['data'] = json_decode($content, true);
114
+        } else {
115
+            $responseJson['data'] = json_decode($response->getBody(), true);
116
+            $ETag = $response->getHeader('ETag');
117
+        }
118
+
119
+        $responseJson['timestamp'] = $this->timeFactory->getTime();
120
+        $responseJson['ncversion'] = $this->getVersion();
121
+        if ($ETag !== '') {
122
+            $responseJson['ETag'] = $ETag;
123
+        }
124
+
125
+        return $responseJson;
126
+    }
127
+
128
+    /**
129
+     * Returns the array with the categories on the appstore server
130
+     *
131
+     * @return array
132
+     */
133
+    public function get() {
134
+        $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
135
+        $internetavailable = $this->config->getSystemValue('has_internet_connection', true);
136
+
137
+        if (!$appstoreenabled || !$internetavailable) {
138
+            return [];
139
+        }
140
+
141
+        $rootFolder = $this->appData->getFolder('/');
142
+
143
+        $ETag = '';
144
+        $content = '';
145
+
146
+        try {
147
+            // File does already exists
148
+            $file = $rootFolder->getFile($this->fileName);
149
+            $jsonBlob = json_decode($file->getContent(), true);
150
+            if (is_array($jsonBlob)) {
151
+
152
+                // No caching when the version has been updated
153
+                if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
154
+
155
+                    // If the timestamp is older than 3600 seconds request the files new
156
+                    if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
157
+                        return $jsonBlob['data'];
158
+                    }
159
+
160
+                    if (isset($jsonBlob['ETag'])) {
161
+                        $ETag = $jsonBlob['ETag'];
162
+                        $content = json_encode($jsonBlob['data']);
163
+                    }
164
+                }
165
+            }
166
+        } catch (NotFoundException $e) {
167
+            // File does not already exists
168
+            $file = $rootFolder->newFile($this->fileName);
169
+        }
170
+
171
+        // Refresh the file content
172
+        try {
173
+            $responseJson = $this->fetch($ETag, $content);
174
+            $file->putContent(json_encode($responseJson));
175
+            return json_decode($file->getContent(), true)['data'];
176
+        } catch (ConnectException $e) {
177
+            $this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']);
178
+            return [];
179
+        } catch (\Exception $e) {
180
+            $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
181
+            return [];
182
+        }
183
+    }
184
+
185
+    /**
186
+     * Get the currently Nextcloud version
187
+     * @return string
188
+     */
189
+    protected function getVersion() {
190
+        if ($this->version === null) {
191
+            $this->version = $this->config->getSystemValue('version', '0.0.0');
192
+        }
193
+        return $this->version;
194
+    }
195
+
196
+    /**
197
+     * Set the current Nextcloud version
198
+     * @param string $version
199
+     */
200
+    public function setVersion(string $version) {
201
+        $this->version = $version;
202
+    }
203
+
204
+    /**
205
+     * Get the currently Nextcloud update channel
206
+     * @return string
207
+     */
208
+    protected function getChannel() {
209
+        if ($this->channel === null) {
210
+            $this->channel = \OC_Util::getChannel();
211
+        }
212
+        return $this->channel;
213
+    }
214
+
215
+    /**
216
+     * Set the current Nextcloud update channel
217
+     * @param string $channel
218
+     */
219
+    public function setChannel(string $channel) {
220
+        $this->channel = $channel;
221
+    }
222
+
223
+    protected function getEndpoint(): string {
224
+        return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName;
225
+    }
226 226
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 				if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
154 154
 
155 155
 					// If the timestamp is older than 3600 seconds request the files new
156
-					if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
156
+					if ((int) $jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
157 157
 						return $jsonBlob['data'];
158 158
 					}
159 159
 
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
 			$file->putContent(json_encode($responseJson));
175 175
 			return json_decode($file->getContent(), true)['data'];
176 176
 		} catch (ConnectException $e) {
177
-			$this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']);
177
+			$this->logger->warning('Could not connect to appstore: '.$e->getMessage(), ['app' => 'appstoreFetcher']);
178 178
 			return [];
179 179
 		} catch (\Exception $e) {
180 180
 			$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
@@ -221,6 +221,6 @@  discard block
 block discarded – undo
221 221
 	}
222 222
 
223 223
 	protected function getEndpoint(): string {
224
-		return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName;
224
+		return $this->config->getSystemValue('appstoreurl', 'https://apps.nextcloud.com/api/v1').'/'.$this->endpointName;
225 225
 	}
226 226
 }
Please login to merge, or discard this patch.