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

Fetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
ccs 5
cts 5
cp 1
crap 1
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
	/**
140
	 * Produce complete feed URL
141
	 * @return string
142
	 */
143 3
	protected function getFeedUrl(){
144 3
		$currentVersion = $this->configReader->getByPath('system.version');
145 3
		$version = explode('.', $currentVersion);
146 3
		$version['installed'] = $this->configReader->getByPath('apps.core.installedat');
147 3
		$version['updated'] = $this->configReader->getByPath('apps.core.lastupdatedat');
148 3
		$version['updatechannel'] = $this->configReader->getByPath('apps.core.OC_Channel');
149 3
		$version['edition'] = $this->configReader->getEdition();
150 3
		$version['build'] = $this->locator->getBuild();
151
152 3
		$url = self::DEFAULT_BASE_URL . '?version=' . implode('x', $version);
153 3
		return $url;
154
	}
155
156
	/**
157
	 * Get URL content
158
	 * @param string $url
159
	 * @return string
160
	 * @throws \UnexpectedValueException
161
	 */
162 3
	protected function download($url){
163 3
		$response = $this->httpClient->get($url, ['timeout' => 600]);
164 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...
165
			throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.');
166
		}
167 3
		return $response->getBody()->getContents();
168
	}
169
170
}
171