1 | <?php |
||
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) |
|
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | 4 | public function __toString() |
|
65 | |||
66 | /** |
||
67 | * @throws \LogicException When url is not defined |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 20 | public function getUrl() |
|
79 | |||
80 | /** |
||
81 | * @param string $url |
||
82 | */ |
||
83 | 2 | public function setUrl($url) |
|
87 | |||
88 | /** |
||
89 | * @return string|null |
||
90 | */ |
||
91 | 14 | public function getUser() |
|
95 | |||
96 | /** |
||
97 | * @return string|null |
||
98 | */ |
||
99 | 4 | public function getPass() |
|
103 | |||
104 | /** |
||
105 | * @param ClientInterface $client |
||
106 | */ |
||
107 | 20 | public function setClient(ClientInterface $client) |
|
111 | |||
112 | /** |
||
113 | * @return ClientInterface |
||
114 | */ |
||
115 | public function getClient() |
||
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function getLastModifiedDate() |
||
127 | |||
128 | /** |
||
129 | * @return int|null |
||
130 | */ |
||
131 | public function getSize() |
||
135 | |||
136 | /** |
||
137 | * @param ResponseInterface $response |
||
138 | */ |
||
139 | 6 | public function onHeaders(ResponseInterface $response) |
|
146 | |||
147 | /** |
||
148 | * @param int $downloadSize |
||
149 | * @param int $downloaded |
||
150 | */ |
||
151 | 6 | public function onProgress($downloadSize, $downloaded) |
|
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) |
|
196 | } |
||
197 |