1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\Feeder\Transport; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
8
|
|
|
use GuzzleHttp\RequestOptions; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use TreeHouse\Feeder\Event\FetchProgressEvent; |
11
|
|
|
use TreeHouse\Feeder\Exception\TransportException; |
12
|
|
|
use TreeHouse\Feeder\FeedEvents; |
13
|
|
|
|
14
|
|
|
class HttpTransport extends AbstractTransport implements ProgressAwareInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ClientInterface |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $size; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \DateTime |
28
|
|
|
*/ |
29
|
|
|
protected $lastModified; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $url |
33
|
|
|
* @param string|null $user |
34
|
|
|
* @param string|null $pass |
35
|
|
|
* |
36
|
|
|
* @return HttpTransport |
37
|
|
|
*/ |
38
|
20 |
|
public static function create($url, $user = null, $pass = null) |
39
|
|
|
{ |
40
|
20 |
|
$client = new Client([ |
41
|
|
|
'headers' => [ |
42
|
20 |
|
'User-Agent' => static::getDefaultUserAgent(), |
43
|
20 |
|
], |
44
|
20 |
|
]); |
45
|
|
|
|
46
|
20 |
|
$conn = new Connection([ |
47
|
20 |
|
'url' => $url, |
48
|
20 |
|
'user' => $user, |
49
|
20 |
|
'pass' => $pass, |
50
|
20 |
|
]); |
51
|
|
|
|
52
|
20 |
|
$transport = new static($conn); |
53
|
20 |
|
$transport->setClient($client); |
54
|
|
|
|
55
|
20 |
|
return $transport; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
4 |
|
public function __toString() |
62
|
|
|
{ |
63
|
4 |
|
return $this->getUrl(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws \LogicException When url is not defined |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
20 |
|
public function getUrl() |
72
|
|
|
{ |
73
|
20 |
|
if (!isset($this->connection['url'])) { |
74
|
2 |
|
throw new \LogicException('No url defined'); |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
return $this->connection['url']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $url |
82
|
|
|
*/ |
83
|
2 |
|
public function setUrl($url) |
84
|
|
|
{ |
85
|
2 |
|
$this->connection['url'] = $url; |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
14 |
|
public function getUser() |
92
|
|
|
{ |
93
|
14 |
|
return isset($this->connection['user']) ? $this->connection['user'] : null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string|null |
98
|
|
|
*/ |
99
|
4 |
|
public function getPass() |
100
|
|
|
{ |
101
|
4 |
|
return isset($this->connection['pass']) ? $this->connection['pass'] : null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param ClientInterface $client |
106
|
|
|
*/ |
107
|
20 |
|
public function setClient(ClientInterface $client) |
108
|
|
|
{ |
109
|
20 |
|
$this->client = $client; |
110
|
20 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return ClientInterface |
114
|
|
|
*/ |
115
|
|
|
public function getClient() |
116
|
|
|
{ |
117
|
|
|
return $this->client; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
public function getLastModifiedDate() |
124
|
|
|
{ |
125
|
|
|
return $this->lastModified; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return int|null |
130
|
|
|
*/ |
131
|
|
|
public function getSize() |
132
|
|
|
{ |
133
|
|
|
return $this->size; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param ResponseInterface $response |
138
|
|
|
*/ |
139
|
6 |
|
public function onHeaders(ResponseInterface $response) |
140
|
|
|
{ |
141
|
|
|
// set the modified date if we got it in the response |
142
|
6 |
|
if (!empty($lastModified = $response->getHeader('Last-Modified'))) { |
143
|
6 |
|
$this->lastModified = new \DateTime(reset($lastModified)); |
144
|
6 |
|
} |
145
|
6 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int $downloadSize |
149
|
|
|
* @param int $downloaded |
150
|
|
|
*/ |
151
|
6 |
|
public function onProgress($downloadSize, $downloaded) |
152
|
|
|
{ |
153
|
6 |
|
$this->size = $downloadSize; |
154
|
|
|
|
155
|
6 |
|
$progressEvent = new FetchProgressEvent($downloaded, $downloadSize); |
156
|
6 |
|
$this->eventDispatcher->dispatch(FeedEvents::FETCH_PROGRESS, $progressEvent); |
157
|
6 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
*/ |
162
|
12 |
|
protected function doFetch($destination) |
163
|
|
|
{ |
164
|
12 |
|
if (!$this->client) { |
165
|
|
|
throw new \LogicException('No client set to use for downloading'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$options = [ |
169
|
12 |
|
RequestOptions::SINK => $destination, |
170
|
12 |
|
RequestOptions::ON_HEADERS => [$this, 'onHeaders'], |
171
|
12 |
|
RequestOptions::PROGRESS => [$this, 'onProgress'], |
172
|
12 |
|
]; |
173
|
|
|
|
174
|
12 |
|
if ($user = $this->getUser()) { |
175
|
2 |
|
$options['auth'] = [$user, $this->getPass()]; |
176
|
2 |
|
} |
177
|
|
|
|
178
|
|
|
try { |
179
|
12 |
|
$response = $this->client->request('GET', $this->getUrl(), $options); |
180
|
|
|
|
181
|
10 |
|
if ($response->getBody()->getSize() === 0) { |
182
|
2 |
|
throw new TransportException('Server did not return any content, status code was ' . $response->getStatusCode()); |
183
|
|
|
} |
184
|
12 |
|
} catch (RequestException $e) { |
185
|
2 |
|
throw new TransportException(sprintf('Could not download feed: %s', $e->getMessage()), null, $e); |
186
|
|
|
} |
187
|
8 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @inheritdoc |
191
|
|
|
*/ |
192
|
12 |
|
protected function isFresh($destination, \DateTime $maxAge = null) |
193
|
|
|
{ |
194
|
12 |
|
return false; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|