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

Fetcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fetch() 0 8 1
B get() 0 27 5
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\Utility\ITimeFactory;
25
use OCP\Files\IAppData;
26
use OCP\Files\NotFoundException;
27
use OCP\Http\Client\IClientService;
28
29
abstract class Fetcher {
30
	const INVALIDATE_AFTER_SECONDS = 300;
31
32
	/** @var IAppData */
33
	protected $appData;
34
	/** @var IClientService */
35
	protected $clientService;
36
	/** @var ITimeFactory */
37
	protected $timeFactory;
38
	/** @var string */
39
	protected $fileName;
40
	/** @var string */
41
	protected $endpointUrl;
42
43
	/**
44
	 * @param IAppData $appData
45
	 * @param IClientService $clientService
46
	 * @param ITimeFactory $timeFactory
47
	 */
48
	public function __construct(IAppData $appData,
49
								IClientService $clientService,
50
								ITimeFactory $timeFactory) {
51
		$this->appData = $appData;
52
		$this->clientService = $clientService;
53
		$this->timeFactory = $timeFactory;
54
	}
55
56
	/**
57
	 * Fetches the response from the server
58
	 *
59
	 * @return array
60
	 */
61
	protected function fetch() {
62
		$client = $this->clientService->newClient();
63
		$response = $client->get($this->endpointUrl);
64
		$responseJson = [];
65
		$responseJson['data'] = json_decode($response->getBody(), true);
66
		$responseJson['timestamp'] = $this->timeFactory->getTime();
67
		return $responseJson;
68
	}
69
70
	/**
71
	 * Returns the array with the categories on the appstore server
72
	 *
73
	 * @return array
74
	 */
75
	 public function get() {
76
		$rootFolder = $this->appData->getFolder('/');
77
78
		try {
79
			// File does already exists
80
			$file = $rootFolder->getFile($this->fileName);
81
			$jsonBlob = json_decode($file->getContent(), true);
82
			if(is_array($jsonBlob)) {
83
				// If the timestamp is older than 300 seconds request the files new
84
				if((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
85
					return $jsonBlob['data'];
86
				}
87
			}
88
		} catch (NotFoundException $e) {
89
			// File does not already exists
90
			$file = $rootFolder->newFile($this->fileName);
91
		}
92
93
		// Refresh the file content
94
		try {
95
			$responseJson = $this->fetch();
96
			$file->putContent(json_encode($responseJson));
97
			return json_decode($file->getContent(), true)['data'];
98
		} catch (\Exception $e) {
99
			return [];
100
		}
101
	}
102
}
103