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

GetDataSerializer::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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\GetData;
8
use BitWasp\Bitcoin\Networking\Serializer\Structure\InventorySerializer;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\BufferInterface;
12
use BitWasp\Buffertools\Parser;
13
14
class GetDataSerializer
15
{
16
    /**
17
     * @var \BitWasp\Buffertools\Types\Vector
18
     */
19
    private $vectorInt;
20 96
21
    /**
22 96
     * @param InventorySerializer $inv
23 96
     */
24
    public function __construct(InventorySerializer $inv)
25
    {
26
        $this->vectorInt = Types::vector([$inv, 'fromParser']);
27
    }
28 3
29
    /**
30 3
     * @param Parser $parser
31 3
     * @return GetData
32 3
     */
33 3
    public function fromParser(Parser $parser): GetData
34 3
    {
35
        $addrs = $this->vectorInt->read($parser);
36
        return new GetData($addrs);
37
    }
38
39
    /**
40
     * @param BufferInterface $data
41 3
     * @return GetData
42
     */
43 3
    public function parse(BufferInterface $data): GetData
44 3
    {
45
        return $this->fromParser(new Parser($data));
46
    }
47
48
    /**
49
     * @param GetData $getData
50
     * @return BufferInterface
51 3
     */
52
    public function serialize(GetData $getData): BufferInterface
53 3
    {
54
        return new Buffer($this->vectorInt->write($getData->getItems()));
55
    }
56
}
57