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

GetHeadersSerializer::getVersionTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 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