Completed
Pull Request — master (#90)
by thomas
03:18
created

InventorySerializer::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Serializer\Structure;
6
7
use BitWasp\Bitcoin\Networking\Structure\Inventory;
8
use BitWasp\Bitcoin\Serializer\Types;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
use BitWasp\Buffertools\Parser;
12
13
class InventorySerializer
14 15
{
15
    /**
16 15
     * @var \BitWasp\Buffertools\Types\Uint32
17 15
     */
18 15
    private $uint32le;
19 15
20
    /**
21
     * @var \BitWasp\Buffertools\Types\ByteString
22
     */
23
    private $bytestring32le;
24
25
    public function __construct()
26 15
    {
27
        $this->uint32le = Types::uint32le();
28 15
        $this->bytestring32le = Types::bytestringle(32);
29 15
    }
30 15
31 10
    /**
32
     * @param Inventory $inv
33
     * @return BufferInterface
34
     */
35
    public function serialize(Inventory $inv): BufferInterface
36
    {
37
        $flags = $this->uint32le->write($inv->getType());
38 12
        $hash = $this->bytestring32le->write($inv->getHash());
39
        return new Buffer("{$flags}{$hash}");
40 12
    }
41 12
42 8
    /**
43
     * @param Parser $parser
44 8
     * @return Inventory
45
     */
46
    public function fromParser(Parser $parser): Inventory
47
    {
48
        $type = (int) $this->uint32le->read($parser);
49
        $hash = $this->bytestring32le->read($parser);
50
        return new Inventory($type, $hash);
51 3
    }
52
53 3
    /**
54
     * @param BufferInterface $data
55
     * @return Inventory
56
     */
57
    public function parse(BufferInterface $data): Inventory
58
    {
59
        return $this->fromParser(new Parser($data));
60
    }
61
}
62