Passed
Pull Request — master (#17)
by Pavel
04:41
created

JsonRpcClient::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 4
crap 3
1
<?php
2
3
namespace ScayTrase\Api\JsonRpc;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\GuzzleException;
7
use GuzzleHttp\Psr7\Request;
8
use Psr\Http\Message\UriInterface;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use ScayTrase\Api\IdGenerator\IdGeneratorInterface;
12
use ScayTrase\Api\IdGenerator\UuidGenerator;
13
use ScayTrase\Api\Rpc\Exception\RemoteCallFailedException;
14
use ScayTrase\Api\Rpc\RpcClientInterface;
15
use ScayTrase\Api\Rpc\RpcRequestInterface;
16
17
final class JsonRpcClient implements RpcClientInterface
18
{
19
    const VERSION = '2.0';
20
21
    /**
22
     * @var ClientInterface
23
     */
24
    private $client;
25
    /** @var UriInterface */
26
    private $uri;
27
    /** @var IdGeneratorInterface */
28
    private $idGenerator;
29
    /** @var LoggerInterface */
30
    private $logger;
31
32
    /**
33
     * JsonRpcClient constructor.
34
     * @param ClientInterface $client
35
     * @param UriInterface $endpoint
36
     * @param IdGeneratorInterface|null $idGenerator
37
     * @param LoggerInterface $logger
38
     */
39 18
    public function __construct(
40
        ClientInterface $client,
41
        UriInterface $endpoint,
42
        IdGeneratorInterface $idGenerator = null,
43
        LoggerInterface $logger = null
44
    ) {
45 18
        $this->client = $client;
46 18
        $this->uri = $endpoint;
47 18
        $this->idGenerator = $idGenerator ?: new UuidGenerator();
48 18
        $this->logger = $logger ?: new NullLogger();
49 18
    }
50
51
    /** {@inheritdoc} */
52 18
    public function invoke($calls)
53
    {
54
        try {
55 18
            if (!is_array($calls)) {
56 8
                if (!($calls instanceof RpcRequestInterface)) {
57 1
                    throw new \InvalidArgumentException('Request should be either array or single RpcRequestInterface instance');
58
                }
59 7
                $transformedCall = $this->transformCall($calls);
60
61 7
                return new JsonRpcResponseCollection(
62 7
                    $this->client->sendAsync(
63 7
                        $this->createHttpRequest($transformedCall)
64 7
                    ),
65 7
                    [new RequestTransformation($calls, $transformedCall)]
66 7
                );
67
            }
68
69 10
            $requests = [];
70 10
            $batchRequest = [];
71
72 10
            foreach ($calls as $key => $call) {
73 10
                $transformedCall = $this->transformCall($call);
74 10
                $requests[spl_object_hash($call)] = new RequestTransformation($call, $transformedCall);
75 10
                $batchRequest[] = $transformedCall;
76 10
            }
77
78 10
            return new JsonRpcResponseCollection(
79 10
                $this->client->sendAsync($this->createHttpRequest($batchRequest)),
80
                $requests
81 10
            );
82 1
        } catch (GuzzleException $exception) {
83
            throw new RemoteCallFailedException($exception->getMessage(), 0, $exception);
84
        }
85
    }
86
87
    /**
88
     * @param $requestBody
89
     *
90
     * @return Request
91
     */
92 17
    private function createHttpRequest($requestBody)
93
    {
94 17
        return new Request(
95 17
            'POST',
96 17
            $this->uri,
97
            [
98 17
                'Content-Type' => 'application/json',
99 17
                'Accept' => 'application/json',
100 17
            ],
101 17
            json_encode($requestBody, JSON_PRETTY_PRINT)
102 17
        );
103
    }
104
105
    /**
106
     * @param RpcRequestInterface $call
107
     *
108
     * @return JsonRpcRequestInterface
109
     */
110 17
    private function transformCall(RpcRequestInterface $call)
111
    {
112 17
        if ($call instanceof JsonRpcRequestInterface) {
113 16
            return $call;
114
        }
115
116 1
        return JsonRpcRequest::fromRpcRequest($call, $this->idGenerator->getRequestIdentifier($call));
117
    }
118
}
119