Completed
Push — master ( fceab7...09c4c4 )
by Fabian
13s
created

HttpClient::send()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.8666
c 0
b 0
f 0
cc 3
nc 5
nop 3
crap 3.0261
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\CallbackProxy;
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\RequestException;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Client to forward requests to configured Target
13
 */
14
class HttpClient
15
{
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21 48
    public function __construct(Client $client)
22
    {
23 48
        $this->client = $client;
24 48
    }
25
26 48
    public function send(RequestInterface $request, Target $target, string $action): ResponseInterface
27
    {
28
        try {
29 48
            $response = $this->client->send($target->applyToRequest($request, $action));
30 42
            return $target->applyToResponse($response, $action);
31 15
        } catch (RequestException $e) {
32 15
            if ($e->getResponse() instanceof ResponseInterface) {
33 15
                return $target->applyToResponse($e->getResponse(), $action);
34
            }
35
            throw $e;
36
        }
37
    }
38
}
39