1 | <?php |
||
21 | class Client implements HttpClient |
||
22 | { |
||
23 | /** |
||
24 | * @var ClientInterface |
||
25 | */ |
||
26 | private $client; |
||
27 | |||
28 | /** |
||
29 | * @var ResponseFactory |
||
30 | */ |
||
31 | private $responseFactory; |
||
32 | |||
33 | /** |
||
34 | * @param ClientInterface|null $client |
||
35 | * @param ResponseFactory|null $responseFactory |
||
36 | */ |
||
37 | 168 | public function __construct(ClientInterface $client = null, ResponseFactory $responseFactory = null) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 168 | public function sendRequest(RequestInterface $request): ResponseInterface |
|
58 | |||
59 | /** |
||
60 | * Converts a PSR request into a Guzzle request. |
||
61 | * |
||
62 | * @param RequestInterface $request |
||
63 | * |
||
64 | * @return GuzzleRequest |
||
65 | */ |
||
66 | 168 | private function createRequest(RequestInterface $request) |
|
67 | { |
||
68 | $options = [ |
||
69 | 168 | 'exceptions' => false, |
|
70 | 'allow_redirects' => false, |
||
71 | ]; |
||
72 | |||
73 | 168 | $options['version'] = $request->getProtocolVersion(); |
|
74 | 168 | $options['headers'] = $request->getHeaders(); |
|
75 | 168 | $body = (string) $request->getBody(); |
|
76 | 168 | $options['body'] = '' === $body ? null : $body; |
|
77 | |||
78 | 168 | return $this->client->createRequest( |
|
79 | 168 | $request->getMethod(), |
|
80 | 168 | (string) $request->getUri(), |
|
81 | 168 | $options |
|
82 | ); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Converts a Guzzle response into a PSR response. |
||
87 | * |
||
88 | * @param GuzzleResponse $response |
||
89 | * |
||
90 | * @return ResponseInterface |
||
91 | */ |
||
92 | 157 | private function createResponse(GuzzleResponse $response) |
|
104 | |||
105 | /** |
||
106 | * Converts a Guzzle exception into an Httplug exception. |
||
107 | * |
||
108 | * @param GuzzleExceptions\TransferException $exception |
||
109 | * @param RequestInterface $request |
||
110 | * |
||
111 | * @return HttplugException |
||
112 | */ |
||
113 | 15 | private function handleException(GuzzleExceptions\TransferException $exception, RequestInterface $request) |
|
137 | } |
||
138 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: