1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Updater Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
namespace SWP\UpdaterBundle\Client; |
15
|
|
|
|
16
|
|
|
use GuzzleHttp\Client as BaseClient; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Remote Client class. |
22
|
|
|
* Allows to fetch data from the remote update server. |
23
|
|
|
*/ |
24
|
|
|
class GuzzleClient extends BaseClient implements ClientInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Default request options. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $options = array( |
32
|
|
|
'Accept' => 'application/json', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Remote's server URI. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $baseUri; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $config = array(), array $options = array()) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($config); |
48
|
|
|
$this->options = $options; |
49
|
|
|
$this->baseUri = $config['base_uri']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function call( |
56
|
|
|
$endpoint = '/', |
57
|
|
|
array $arguments = array(), |
58
|
|
|
array $options = array(), |
59
|
|
|
$fullResponse = false |
60
|
|
|
) { |
61
|
|
|
try { |
62
|
|
|
$response = $this->get( |
63
|
|
|
$endpoint, |
64
|
|
|
$this->process($arguments, $options) |
65
|
|
|
); |
66
|
|
|
} catch (\Exception $e) { |
67
|
|
|
throw new ServiceUnavailableHttpException( |
68
|
|
|
null, |
69
|
|
|
'Could not resolve host: '.$this->baseUri, |
70
|
|
|
$e, |
71
|
|
|
$e->getCode() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($fullResponse) { |
76
|
|
|
return $this->decode($response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return (string) $response->getBody(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function saveFile($fromUrl, $filePath) |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
$this->get($fromUrl, array( |
89
|
|
|
'save_to' => $filePath, |
90
|
|
|
)); |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} catch (\Exception $e) { |
94
|
|
|
unlink($filePath); |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function process($arguments, $options) |
101
|
|
|
{ |
102
|
|
|
if (!empty($options)) { |
103
|
|
|
$this->options = array_merge($this->options, $options); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// add query parameters |
107
|
|
|
$this->options['query'] = $arguments; |
108
|
|
|
|
109
|
|
|
return $this->options; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function decode(ResponseInterface $response) |
113
|
|
|
{ |
114
|
|
|
return array( |
115
|
|
|
'headers' => $response->getHeaders(), |
116
|
|
|
'status' => $response->getStatusCode(), |
117
|
|
|
'reason' => $response->getReasonPhrase(), |
118
|
|
|
'version' => $response->getProtocolVersion(), |
119
|
|
|
'body' => (string) $response->getBody(), |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|