Passed
Push — master ( 3dc4f8...535c32 )
by thomas
15:05
created

GetBlocksSerializer::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 Method

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