|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of CacheTool. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Samuel Gordalina <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CacheTool\Adapter\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
|
|
17
|
|
|
class SymfonyHttpClient extends AbstractHttp |
|
18
|
|
|
{ |
|
19
|
|
|
private $client; |
|
20
|
|
|
|
|
21
|
1 |
|
public function __construct($baseUrl, $httpClientConfig) |
|
22
|
|
|
{ |
|
23
|
1 |
|
$this->client = HttpClient::create($httpClientConfig); |
|
24
|
1 |
|
parent::__construct($baseUrl); |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function fetch($filename) |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
|
|
$url = "{$this->baseUrl}/{$filename}"; |
|
31
|
|
|
|
|
32
|
|
|
if (!filter_var($url, FILTER_VALIDATE_URL)) { |
|
33
|
|
|
throw new \RuntimeException( |
|
34
|
|
|
sprintf( |
|
35
|
|
|
"The given url is not valid: %s, did you forget to specify the --web-url option?", |
|
36
|
|
|
$url |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$response = $this->client->request('GET', $url); |
|
42
|
|
|
|
|
43
|
|
|
if ($response->getStatusCode() !== Response::HTTP_OK) { |
|
44
|
|
|
throw new \RuntimeException( |
|
45
|
|
|
sprintf( |
|
46
|
|
|
"HTTP Response Code for URL %s is not 200, it is: %s", |
|
47
|
|
|
$url, |
|
48
|
|
|
$response->getStatusCode() |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $response->getContent(); |
|
54
|
|
|
|
|
55
|
|
|
} catch (\Throwable $throwable) { |
|
56
|
|
|
|
|
57
|
|
|
return serialize([ |
|
58
|
|
|
'result' => false, |
|
59
|
|
|
'errors' => [ |
|
60
|
|
|
[ |
|
61
|
|
|
'no' => $throwable->getCode(), |
|
62
|
|
|
'str' => sprintf( |
|
63
|
|
|
"%s: %s,\n%s", |
|
64
|
|
|
get_class($throwable), |
|
65
|
|
|
$throwable->getMessage(), |
|
66
|
|
|
$throwable->getTraceAsString() |
|
67
|
|
|
) |
|
68
|
|
|
], |
|
69
|
|
|
], |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|