Completed
Pull Request — master (#217)
by Victor
02:18
created

Fetcher::getOwncloud()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 3
Ratio 13.64 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 3
loc 22
ccs 0
cts 18
cp 0
rs 8.9197
cc 4
eloc 15
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
use GuzzleHttp\Client;
25
use Owncloud\Updater\Utils\Feed;
26
27
class Fetcher {
28
29
	const DEFAULT_BASE_URL = 'https://updates.owncloud.com/server/';
30
31
	/**
32
	 * @var Locator $locator
33
	 */
34
	protected $locator;
35
36
	/**
37
	 * @var ConfigReader $configReader
38
	 */
39
	protected $configReader;
40
41
	/**
42
	 * @var Client $httpClient
43
	 */
44
	protected $httpClient;
45
	protected $requiredFeedEntries = [
46
		'version',
47
		'versionstring',
48
		'url'
49
	];
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @param Client $httpClient
55
	 * @param Locator $locator
56
	 * @param ConfigReader $configReader
57
	 */
58 3
	public function __construct(Client $httpClient, Locator $locator, ConfigReader $configReader){
59 3
		$this->httpClient = $httpClient;
60 3
		$this->locator = $locator;
61 3
		$this->configReader = $configReader;
62 3
	}
63
64
	/**
65
	 * Download new owncloud package
66
	 * @param Feed $feed
67
	 * @param Callable $onProgress
68
	 * @throws \UnexpectedValueException
69
	 */
70
	public function getOwncloud(Feed $feed, callable $onProgress){
71
		if ($feed->isValid()){
72
			$downloadPath = $this->getBaseDownloadPath($feed);
73
			if (!is_writable(dirname($downloadPath))){
74
				throw new \Exception(dirname($downloadPath) . ' is not writable.');
75
			}
76
			$url = $feed->getUrl();
77
			$request = $this->httpClient->createRequest(
78
					'GET',
79
					$url,
80
					[
81
						'save_to' => $downloadPath,
82
						'timeout' => 600
83
					]
84
			);
85
			$request->getEmitter()->on('progress', $onProgress);
86
			$response = $this->httpClient->send($request);
87 View Code Duplication
			if ($response->getStatusCode() !== 200){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
				throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.');
89
			}
90
		}
91
	}
92
93
	/**
94
	 * Produce a local path to save the package to
95
	 * @param Feed $feed
96
	 * @return string
97
	 */
98
	public function getBaseDownloadPath(Feed $feed){
99
		$basePath = $this->locator->getDownloadBaseDir();
100
		return $basePath . '/' . $feed->getDownloadedFileName();
101
	}
102
103
	/**
104
	 * Get md5 sum for the package
105
	 * @param Feed $feed
106
	 * @return string
107
	 */
108
	public function getMd5(Feed $feed){
109
		$fullChecksum = $this->download($feed->getChecksumUrl());
110
		// we got smth like "5776cbd0a95637ade4b2c0d8694d8fca  -"
111
		//strip trailing space & dash
112
		return substr($fullChecksum, 0, 32);
113
	}
114
115
	/**
116
	 * Read update feed for new releases
117
	 * @return Feed
118
	 */
119 3
	public function getFeed(){
120 3
		$url = $this->getFeedUrl();
121 3
		$xml = $this->download($url);
122 3
		$tmp = [];
123 3
		if ($xml){
124 2
			$loadEntities = libxml_disable_entity_loader(true);
125 2
			$data = @simplexml_load_string($xml);
126 2
			libxml_disable_entity_loader($loadEntities);
127 2
			if ($data !== false){
128 1
				$tmp['version'] = (string) $data->version;
129 1
				$tmp['versionstring'] = (string) $data->versionstring;
130 1
				$tmp['url'] = (string) $data->url;
131 1
				$tmp['web'] = (string) $data->web;
132 1
			}
133 2
		}
134
135 3
		return new Feed($tmp);
136
	}
137
138 3
	public function getUpdateChannel(){
139 3
		return $this->configReader->getByPath('apps.core.OC_Channel');
140
	}
141
142
	/**
143
	 * Produce complete feed URL
144
	 * @return string
145
	 */
146 3
	protected function getFeedUrl(){
147 3
		$currentVersion = $this->configReader->getByPath('system.version');
148 3
		$version = explode('.', $currentVersion);
149 3
		$version['installed'] = $this->configReader->getByPath('apps.core.installedat');
150 3
		$version['updated'] = $this->configReader->getByPath('apps.core.lastupdatedat');
151 3
		$version['updatechannel'] = $this->getUpdateChannel();
152 3
		$version['edition'] = $this->configReader->getEdition();
153 3
		$version['build'] = $this->locator->getBuild();
154
155 3
		$url = self::DEFAULT_BASE_URL . '?version=' . implode('x', $version);
156 3
		return $url;
157
	}
158
159
	/**
160
	 * Get URL content
161
	 * @param string $url
162
	 * @return string
163
	 * @throws \UnexpectedValueException
164
	 */
165 3
	protected function download($url){
166 3
		$response = $this->httpClient->get($url, ['timeout' => 600]);
167 3 View Code Duplication
		if ($response->getStatusCode() !== 200){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
			throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.');
169
		}
170 3
		return $response->getBody()->getContents();
171
	}
172
173
}
174