Completed
Push — master ( 4f9ff9...fee18d )
by Joas
08:54
created

Fetcher   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B fetch() 0 28 4
C get() 0 39 8
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 OCP\AppFramework\Http;
25
use OCP\AppFramework\Utility\ITimeFactory;
26
use OCP\Files\IAppData;
27
use OCP\Files\NotFoundException;
28
use OCP\Http\Client\IClientService;
29
use OCP\IConfig;
30
31
abstract class Fetcher {
32
	const INVALIDATE_AFTER_SECONDS = 300;
33
34
	/** @var IAppData */
35
	protected $appData;
36
	/** @var IClientService */
37
	protected $clientService;
38
	/** @var ITimeFactory */
39
	protected $timeFactory;
40
	/** @var IConfig */
41
	protected $config;
42
	/** @var string */
43
	protected $fileName;
44
	/** @var string */
45
	protected $endpointUrl;
46
47
	/**
48
	 * @param IAppData $appData
49
	 * @param IClientService $clientService
50
	 * @param ITimeFactory $timeFactory
51
	 * @param IConfig $config
52
	 */
53
	public function __construct(IAppData $appData,
54
								IClientService $clientService,
55
								ITimeFactory $timeFactory,
56
								IConfig $config) {
57
		$this->appData = $appData;
58
		$this->clientService = $clientService;
59
		$this->timeFactory = $timeFactory;
60
		$this->config = $config;
61
	}
62
63
	/**
64
	 * Fetches the response from the server
65
	 *
66
	 * @param string $ETag
67
	 * @param string $content
68
	 *
69
	 * @return array
70
	 */
71
	protected function fetch($ETag, $content) {
72
		$options = [];
73
74
		if ($ETag !== '') {
75
			$options['headers'] = [
76
				'If-None-Match' => $ETag,
77
			];
78
		}
79
80
		$client = $this->clientService->newClient();
81
		$response = $client->get($this->endpointUrl, $options);
82
83
		$responseJson = [];
84
		if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
85
			$responseJson['data'] = json_decode($content, true);
86
		} else {
87
			$responseJson['data'] = json_decode($response->getBody(), true);
88
			$ETag = $response->getHeader('ETag');
89
		}
90
91
		$responseJson['timestamp'] = $this->timeFactory->getTime();
92
		$responseJson['ncversion'] = $this->config->getSystemValue('version');
93
		if ($ETag !== '') {
94
			$responseJson['ETag'] = $ETag;
95
		}
96
97
		return $responseJson;
98
	}
99
100
	/**
101
	 * Returns the array with the categories on the appstore server
102
	 *
103
	 * @return array
104
	 */
105
	 public function get() {
106
		$rootFolder = $this->appData->getFolder('/');
107
108
		$ETag = '';
109
		$content = '';
110
111
		try {
112
			// File does already exists
113
			$file = $rootFolder->getFile($this->fileName);
114
			$jsonBlob = json_decode($file->getContent(), true);
115
			if(is_array($jsonBlob)) {
116
				/*
117
				 * If the timestamp is older than 300 seconds request the files new
118
				 * If the version changed (update!) also refresh
119
				 */
120
				if((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS) &&
121
					isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->config->getSystemValue('version', '0.0.0')) {
122
					return $jsonBlob['data'];
123
				}
124
125
				if (isset($jsonBlob['ETag'])) {
126
					$ETag = $jsonBlob['ETag'];
127
					$content = json_encode($jsonBlob['data']);
128
				}
129
			}
130
		} catch (NotFoundException $e) {
131
			// File does not already exists
132
			$file = $rootFolder->newFile($this->fileName);
133
		}
134
135
		// Refresh the file content
136
		try {
137
			$responseJson = $this->fetch($ETag, $content);
138
			$file->putContent(json_encode($responseJson));
139
			return json_decode($file->getContent(), true)['data'];
140
		} catch (\Exception $e) {
141
			return [];
142
		}
143
	}
144
}
145