Failed Conditions
Push — master ( ba8c0d...a9ded0 )
by thomas
04:50
created

Client::parseResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Trezor\Bridge;
6
7
use BitWasp\Trezor\Bridge\Exception\InvalidMessageException;
8
use BitWasp\Trezor\Bridge\Exception\SchemaValidationException;
9
use BitWasp\Trezor\Bridge\Message\Device;
10
use BitWasp\Trezor\Bridge\Message\ListDevicesResponse;
11
use BitWasp\Trezor\Bridge\Message\ListenResponse;
12
use BitWasp\Trezor\Bridge\Message\VersionResponse;
13
use BitWasp\Trezor\Bridge\Schema\ValidatorFactory;
14
use BitWasp\Trezor\Bridge\Http\HttpClient;
15
use BitWasp\Trezor\Device\Message;
16
use Psr\Http\Message\ResponseInterface;
17
18
class Client
19
{
20
    /**
21
     * @var HttpClient
22
     */
23
    private $client;
24
25
    /**
26
     * @var ValidatorFactory
27
     */
28
    private $validation;
29
30
    /**
31
     * TrezorClient constructor.
32
     * @param HttpClient $client
33
     */
34 4
    public function __construct(HttpClient $client)
35
    {
36 4
        $this->client = $client;
37 4
        $this->validation = new ValidatorFactory();
38 4
    }
39
40
    /**
41
     * @param \Psr\Http\Message\StreamInterface $body
42
     * @return mixed
43
     */
44 4
    protected function parseResponse(\Psr\Http\Message\StreamInterface $body)
45
    {
46 4
        $data = json_decode($body->getContents());
47 4
        if (json_last_error() !== JSON_ERROR_NONE) {
48
            throw new InvalidMessageException("Invalid JSON received in response");
49
        }
50 4
        return $data;
51
    }
52
53
    /**
54
     * @param \stdClass $data
55
     * @param \stdClass $schema
56
     */
57 4
    protected function validateSchema($data, \stdClass $schema)
58
    {
59 4
        $validator = new \JsonSchema\Validator;
60 4
        $validator->coerce($data, $schema);
61 4
        if ($validator->isValid()) {
62 4
            return;
63
        }
64
65
        throw new SchemaValidationException($validator->getErrors());
66
    }
67
68
    /**
69
     * @param ResponseInterface $response - Response message
70
     * @param \stdClass $schema - JSON schema to validate against
71
     * @return mixed
72
     */
73 4
    protected function processResponse(ResponseInterface $response, \stdClass $schema)
74
    {
75 4
        $result = $this->parseResponse($response->getBody());
76 4
        $this->validateSchema($result, $schema);
77 4
        return $result;
78
    }
79
80
    /**
81
     * @return VersionResponse
82
     */
83 1
    public function bridgeVersion(): VersionResponse
84
    {
85 1
        $result = $this->processResponse(
86 1
            $this->client->bridgeVersion(),
87 1
            $this->validation->versionResponse()
88
        );
89
90 1
        return new VersionResponse($result);
91
    }
92
93
    /**
94
     * @return ListDevicesResponse
95
     */
96 3
    public function listDevices(): ListDevicesResponse
97
    {
98 3
        $result = $this->processResponse(
99 3
            $this->client->listDevices(),
100 3
            $this->validation->listDevicesResponse()
101
        );
102
103 3
        $devices = [];
104 3
        foreach ($result as $device) {
105 3
            $devices[] = new Device($device);
106
        }
107
108 3
        return new ListDevicesResponse($devices);
109
    }
110
111
    public function listen(Device ...$devices): ListenResponse
112
    {
113
        $result = $this->processResponse(
114
            $this->client->listen(...$devices),
115
            $this->validation->listDevicesResponse()
116
        );
117
118
        $devices = [];
119
        foreach ($result as $device) {
120
            $devices[] = new Device($device);
121
        }
122
123
        return new ListenResponse($devices);
124
    }
125
126
    /**
127
     * @param Device $device
128
     * @return Session
129
     */
130 3
    public function acquire(Device $device): Session
131
    {
132 3
        $result = $this->processResponse(
133 3
            $this->client->acquire($device),
134 3
            $this->validation->acquireResponse()
135
        );
136
137 3
        return new Session($this, $device, $result->session);
138
    }
139
140 2
    public function release(string $sessionId): bool
141
    {
142 2
        $this->processResponse(
143 2
            $this->client->release($sessionId),
144 2
            $this->validation->releaseResponse()
145
        );
146
147 2
        return true;
148
    }
149
150 2
    public function call(string $sessionId, Message $message): Message
151
    {
152 2
        return $this->client->call($sessionId, $message);
153
    }
154
}
155