Completed
Pull Request — master (#89)
by thomas
03:13
created

GetHeadersSerializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 5 1
A __construct() 0 4 1
A fromParser() 0 5 1
A parse() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Serializer\Message;
6
7
use BitWasp\Bitcoin\Networking\Messages\GetHeaders;
8
use BitWasp\Bitcoin\Serializer\Chain\BlockLocatorSerializer;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\BufferInterface;
12
use BitWasp\Buffertools\Parser;
13
14
class GetHeadersSerializer
15
{
16
    /**
17
     * @var BlockLocatorSerializer
18
     */
19
    private $locator;
20
21 96
    /**
22
     * @var \BitWasp\Buffertools\Types\Uint32
23 96
     */
24 96
    private $uint32le;
25
26
    /**
27
     * @param BlockLocatorSerializer $locatorSerializer
28
     */
29 3
    public function __construct(BlockLocatorSerializer $locatorSerializer)
30
    {
31 3
        $this->uint32le = Types::uint32le();
32 3
        $this->locator = $locatorSerializer;
33 3
    }
34
35
    /**
36
     * @param Parser $parser
37
     * @return GetHeaders
38
     */
39
    public function fromParser(Parser $parser): GetHeaders
40 3
    {
41
        return new GetHeaders(
42 3
            (int) $this->uint32le->read($parser),
43 3
            $this->locator->fromParser($parser)
44
        );
45 3
    }
46 2
47
    /**
48 2
     * @param BufferInterface $data
49
     * @return GetHeaders
50
     */
51
    public function parse(BufferInterface $data): GetHeaders
52
    {
53
        return $this->fromParser(new Parser($data));
54
    }
55 3
56
    /**
57 3
     * @param GetHeaders $msg
58
     * @return BufferInterface
59
     */
60
    public function serialize(GetHeaders $msg): BufferInterface
61
    {
62
        return new Buffer(
63
            $this->uint32le->write($msg->getVersion()) .
64 3
            $this->locator->serialize($msg->getLocator())->getBinary()
65
        );
66 3
    }
67
}
68