Passed
Pull Request — master (#7)
by Igor
04:31
created

SymfonyTransport::send()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.8666
cc 2
nc 4
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Transport\Http;
12
13
use Strider2038\JsonRpcClient\Exception\RemoteProcedureCallFailedException;
14
use Strider2038\JsonRpcClient\Transport\TransportInterface;
15
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
16
use Symfony\Contracts\HttpClient\HttpClientInterface;
17
use Symfony\Contracts\HttpClient\ResponseInterface;
18
19
/**
20
 * @internal
21
 *
22
 * @author Igor Lazarev <[email protected]>
23
 */
24
class SymfonyTransport implements TransportInterface
25
{
26
    /** @var HttpClientInterface */
27
    private $client;
28
29 14
    public function __construct(HttpClientInterface $client)
30
    {
31 14
        $this->client = $client;
32 14
    }
33
34
    /**
35
     * @throws RemoteProcedureCallFailedException
36
     */
37 8
    public function send(string $request): string
38
    {
39
        try {
40 8
            $response = $this->client->request('POST', '', [
41 8
                'body'    => $request,
42
                'headers' => [
43
                    'Content-Type' => 'application/json',
44
                ],
45
            ]);
46
47 7
            $this->validateResponse($response);
48 5
            $content = $response->getContent();
49 3
        } catch (ExceptionInterface $exception) {
50 2
            throw new RemoteProcedureCallFailedException(
51 2
                sprintf('JSON RPC request failed with error: %s.', $exception->getMessage()),
52 2
                0,
53 2
                $exception
54
            );
55
        }
56
57 5
        return $content;
58
    }
59
60
    /**
61
     * @throws RemoteProcedureCallFailedException
62
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
63
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
64
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
65
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
66
     */
67 7
    private function validateResponse(ResponseInterface $response): void
68
    {
69 7
        $statusCode = $response->getStatusCode();
70
71 6
        if (200 !== $statusCode) {
72 1
            throw new RemoteProcedureCallFailedException(
73 1
                sprintf('JSON RPC request failed with error %d: %s.', $statusCode, $response->getContent(false))
74
            );
75
        }
76 5
    }
77
}
78