Completed
Pull Request — master (#217)
by Victor
02:11
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 GuzzleHttp\Event\ProgressEvent;
26
use Owncloud\Updater\Utils\Feed;
27
28
class Fetcher {
29
30
	const DEFAULT_BASE_URL = 'https://updates.owncloud.com/server/';
31
32
	/**
33
	 * @var Locator $locator
34
	 */
35
	protected $locator;
36
37
	/**
38
	 * @var ConfigReader $configReader
39
	 */
40
	protected $configReader;
41
42
	/**
43
	 * @var Client $httpClient
44
	 */
45
	protected $httpClient;
46
	protected $requiredFeedEntries = [
47
		'version',
48
		'versionstring',
49
		'url'
50
	];
51
52
	/**
53
	 * Constructor
54
	 *
55
	 * @param Client $httpClient
56
	 * @param Locator $locator
57
	 * @param ConfigReader $configReader
58
	 */
59 3
	public function __construct(Client $httpClient, Locator $locator, ConfigReader $configReader){
60 3
		$this->httpClient = $httpClient;
61 3
		$this->locator = $locator;
62 3
		$this->configReader = $configReader;
63 3
	}
64
65
	/**
66
	 * Download new owncloud package
67
	 * @param Feed $feed
68
	 * @param Callable $onProgress
69
	 * @throws \UnexpectedValueException
70
	 */
71
	public function getOwncloud(Feed $feed, callable $onProgress){
72
		if ($feed->isValid()){
73
			$downloadPath = $this->getBaseDownloadPath($feed);
74
			if (!is_writable(dirname($downloadPath))){
75
				throw new \Exception(dirname($downloadPath) . ' is not writable.');
76
			}
77
			$url = $feed->getUrl();
78
			$request = $this->httpClient->createRequest(
79
					'GET',
80
					$url,
81
					[
82
						'save_to' => $downloadPath,
83
						'timeout' => 600
84
					]
85
			);
86
			$request->getEmitter()->on('progress', $onProgress);
87
			$response = $this->httpClient->send($request);
88 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...
89
				throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.');
90
			}
91
		}
92
	}
93
94
	/**
95
	 * Produce a local path to save the package to
96
	 * @param Feed $feed
97
	 * @return string
98
	 */
99
	public function getBaseDownloadPath(Feed $feed){
100
		$basePath = $this->locator->getDownloadBaseDir();
101
		return $basePath . '/' . $feed->getDownloadedFileName();
102
	}
103
104
	/**
105
	 * Get md5 sum for the package
106
	 * @param Feed $feed
107
	 * @return string
108
	 */
109
	public function getMd5(Feed $feed){
110
		$fullChecksum = $this->download($feed->getChecksumUrl());
111
		// we got smth like "5776cbd0a95637ade4b2c0d8694d8fca  -"
112
		//strip trailing space & dash
113
		return substr($fullChecksum, 0, 32);
114
	}
115
116
	/**
117
	 * Read update feed for new releases
118
	 * @return Feed
119
	 */
120 3
	public function getFeed(){
121 3
		$url = $this->getFeedUrl();
122 3
		$xml = $this->download($url);
123 3
		$tmp = [];
124 3
		if ($xml){
125 2
			$loadEntities = libxml_disable_entity_loader(true);
126 2
			$data = @simplexml_load_string($xml);
127 2
			libxml_disable_entity_loader($loadEntities);
128 2
			if ($data !== false){
129 1
				$tmp['version'] = (string) $data->version;
130 1
				$tmp['versionstring'] = (string) $data->versionstring;
131 1
				$tmp['url'] = (string) $data->url;
132 1
				$tmp['web'] = (string) $data->web;
133 1
			}
134 2
		}
135
136 3
		return new Feed($tmp);
137
	}
138
139 3
	public function getUpdateChannel(){
140 3
		return $this->configReader->getByPath('apps.core.OC_Channel');
141
	}
142
143
	/**
144
	 * Produce complete feed URL
145
	 * @return string
146
	 */
147 3
	protected function getFeedUrl(){
148 3
		$currentVersion = $this->configReader->getByPath('system.version');
149 3
		$version = explode('.', $currentVersion);
150 3
		$version['installed'] = $this->configReader->getByPath('apps.core.installedat');
151 3
		$version['updated'] = $this->configReader->getByPath('apps.core.lastupdatedat');
152 3
		$version['updatechannel'] = $this->getUpdateChannel();
153 3
		$version['edition'] = $this->configReader->getEdition();
154 3
		$version['build'] = $this->locator->getBuild();
155
156 3
		$url = self::DEFAULT_BASE_URL . '?version=' . implode('x', $version);
157 3
		return $url;
158
	}
159
160
	/**
161
	 * Get URL content
162
	 * @param string $url
163
	 * @return string
164
	 * @throws \UnexpectedValueException
165
	 */
166 3
	protected function download($url){
167 3
		$response = $this->httpClient->get($url, ['timeout' => 600]);
168 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...
169
			throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.');
170
		}
171 3
		return $response->getBody()->getContents();
172
	}
173
174
}
175