Total Complexity | 5 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Coverage | 91.3% |
Changes | 0 |
1 | <?php |
||
10 | class HexCodec |
||
11 | { |
||
12 | /** |
||
13 | * @var StreamUtil |
||
14 | */ |
||
15 | private $stream; |
||
16 | |||
17 | 9 | public function __construct() |
|
20 | 9 | } |
|
21 | |||
22 | 1 | public function parsePayload(StreamInterface $stream): array |
|
23 | { |
||
24 | 1 | if ($stream->getSize() < 12) { |
|
25 | throw new \Exception("Malformed data (size too small)"); |
||
26 | } |
||
27 | |||
28 | 1 | $stream = $this->stream->hex2bin($stream); |
|
29 | |||
30 | 1 | list ($type) = array_values(unpack('n', $stream->read(2))); |
|
31 | 1 | $stream->seek(2); |
|
32 | |||
33 | 1 | list ($size) = array_values(unpack('N', $stream->read(4))); |
|
34 | 1 | $stream->seek(6); |
|
35 | |||
36 | 1 | if ($size > ($stream->getSize() - 6)) { |
|
37 | throw new \Exception("Malformed data (sent more than size)"); |
||
38 | } |
||
39 | |||
40 | 1 | return [$type, $this->stream->createStream($stream->read($size))]; |
|
41 | } |
||
42 | |||
43 | 1 | public function encode(int $messageType, \Protobuf\Message $protobuf): string |
|
54 | } |
||
55 | } |
||
56 |