1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\TranslationBundle\Http; |
4
|
|
|
|
5
|
|
|
use Happyr\TranslationBundle\Exception\HttpException; |
6
|
|
|
use Happyr\TranslationBundle\Translation\FilesystemUpdater; |
7
|
|
|
use Http\Client\Exception\TransferException; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Client\Plugin\ErrorPlugin; |
10
|
|
|
use Http\Client\Plugin\LoggerPlugin; |
11
|
|
|
use Http\Client\Plugin\PluginClient; |
12
|
|
|
use Http\Discovery\HttpClientDiscovery; |
13
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
14
|
|
|
use Http\Message\MessageFactory; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
class RequestManager |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var HttpClient |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MessageFactory |
26
|
|
|
*/ |
27
|
|
|
private $messageFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @param HttpClient $client |
32
|
|
|
* @param MessageFactory $messageFactory |
33
|
|
|
*/ |
34
|
1 |
|
public function __construct(HttpClient $client, MessageFactory $messageFactory) |
35
|
|
|
{ |
36
|
1 |
|
$this->client = new PluginClient($client, [new ErrorPlugin()]); |
|
|
|
|
37
|
1 |
|
$this->messageFactory = $messageFactory; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $data |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function downloadFiles(FilesystemUpdater $filesystem, array $data) |
46
|
|
|
{ |
47
|
|
|
$factory = $this->getMessageFactory(); |
48
|
|
|
$client = $this->getClient(); |
49
|
|
|
|
50
|
|
|
foreach ($data as $url => $fileName) { |
51
|
|
|
$response = $client->sendRequest($factory->createRequest('GET', $url)); |
52
|
|
|
$filesystem->writeToFile($fileName, $response->getBody()->__toString()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $method |
58
|
|
|
* @param string $url |
59
|
|
|
* @param array $data |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
* |
63
|
|
|
* @throws HttpException |
64
|
|
|
*/ |
65
|
|
|
public function send($method, $url, $body = null, $headers = array()) |
66
|
|
|
{ |
67
|
|
|
$request = $this->getMessageFactory()->createRequest($method, $url, $headers, $body); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$response = $this->getClient()->sendRequest($request); |
71
|
|
|
} catch (TransferException $e) { |
72
|
|
|
$message = 'Error sending request. '; |
73
|
|
|
if ($e instanceof \Http\Client\Exception\HttpException) { |
74
|
|
|
$message .= (string) $e->getResponse()->getBody(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new HttpException($message, $e->getCode(), $e); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// TODO add more error checks |
81
|
|
|
return json_decode($response->getBody()->__toString(), true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return HttpClient |
86
|
|
|
*/ |
87
|
|
|
public function getClient() |
88
|
|
|
{ |
89
|
|
|
return $this->client; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @return \Http\Message\MessageFactory |
95
|
|
|
*/ |
96
|
|
|
private function getMessageFactory() |
97
|
|
|
{ |
98
|
|
|
return $this->messageFactory; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.