HttpClient::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Trezor\Bridge\Http;
6
7
use BitWasp\Trezor\Bridge\Codec\CallMessage;
8
use BitWasp\Trezor\Bridge\Message\Device;
9
use BitWasp\Trezor\Device\Message;
10
use BitWasp\TrezorProto\MessageType;
11
use GuzzleHttp\Client as GuzzleClient;
12
use Psr\Http\Message\ResponseInterface;
13
14
class HttpClient
15
{
16
    /**
17
     * @var GuzzleClient
18
     */
19
    private $client;
20
21
    /**
22
     * Base headers to use when the request is
23
     * simple JSON
24
     *
25
     * @var array
26
     */
27
    private $jsonHeaders = [
28
        'Accept' => 'application/json',
29
    ];
30
31
    /**
32
     * Encoder for serializing the call payload & response
33
     *
34
     * @var CallMessage\HexCodec
35
     */
36
    private $callCodec;
37
38 53
    public function __construct(GuzzleClient $client, CallMessage\HexCodec $codec = null)
39
    {
40 53
        $this->client = $client;
41 53
        $this->callCodec = $codec ?: new CallMessage\HexCodec();
42 53
    }
43
44 53
    public static function forUri(string $uri, array $options = []): self
45
    {
46 53
        return new self(new GuzzleClient(
47 53
            array_merge(
48 53
                $options,
49
                [
50 53
                'base_uri' => $uri,
51
                'headers' => [
52
                    'Origin' => 'http://localhost:5000',
53
                ]
54
                ]
55
            )
56
        ));
57
    }
58
59 8
    public function bridgeVersion(): ResponseInterface
60
    {
61 8
        return $this->client->post('/', [
62 8
            'headers' => $this->jsonHeaders,
63
        ]);
64
    }
65
66 3
    public function listDevices(): ResponseInterface
67
    {
68 3
        return $this->client->post('/enumerate', [
69 3
            'headers' => $this->jsonHeaders,
70
        ]);
71
    }
72
73 2
    public function listen(Device ...$devices): ResponseInterface
74
    {
75 2
        return $this->client->post('/listen', [
76 2
            'headers' => $this->jsonHeaders,
77 2
            'json' => array_map(function (Device $device): \stdClass {
78 2
                return $device->getObject();
79 2
            }, $devices),
80
        ]);
81
    }
82
83 5
    public function acquire(Device $device): ResponseInterface
84
    {
85 5
        if ($device->getSession()) {
86 1
            $prevSession = $device->getSession();
87
        } else {
88 4
            $prevSession = "null";
89
        }
90
91 5
        return $this->client->post("/acquire/{$device->getPath()}/{$prevSession}", [
92 5
            'headers' => $this->jsonHeaders,
93
        ]);
94
    }
95
96 4
    public function release(string $sessionId): ResponseInterface
97
    {
98 4
        return $this->client->post("/release/{$sessionId}", [
99 4
            'headers' => $this->jsonHeaders,
100
        ]);
101
    }
102
103 30
    public function call(string $sessionId, Message $message): Message
104
    {
105 30
        static $prefixLen;
106 30
        if (null === $prefixLen) {
107 1
            $prefixLen = strlen("MessageType_");
108
        }
109
110 30
        $response = $this->client->post("/call/{$sessionId}", [
111 30
            'body' => $this->callCodec->encode($message->getType(), $message->getProto()),
112
        ]);
113
114 30
        list ($type, $result) = $this->callCodec->parsePayload($response->getBody());
115
116 30
        $messageType = MessageType::valueOf($type);
117 30
        $protoType = substr($messageType->name(), $prefixLen);
118 30
        $reader = ["\\BitWasp\\TrezorProto\\{$protoType}", 'fromStream'];
119 30
        assert(class_exists($reader[0]));
120
121 30
        $protobuf = call_user_func($reader, $result);
122 30
        return new Message($messageType, $protobuf);
123
    }
124
}
125