Passed
Pull Request — master (#89)
by thomas
02:12
created

NotFoundSerializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromParser() 0 4 1
A parse() 0 3 1
A __construct() 0 5 1
A serialize() 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\NotFound;
8
use BitWasp\Bitcoin\Networking\Serializer\Structure\InventorySerializer;
9
use BitWasp\Bitcoin\Networking\Structure\Inventory;
10
use BitWasp\Bitcoin\Serializer\Types;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
use BitWasp\Buffertools\Parser;
14
use BitWasp\Buffertools\Types\Vector;
15
16
class NotFoundSerializer
17
{
18
    /**
19
     * @var InventorySerializer
20 96
     */
21
    private $invSerializer;
22 96
23 96
    /**
24
     * @var Vector
25
     */
26
    private $vectorInvSer;
27
28 3
    /**
29
     * @param InventorySerializer $inv
30 3
     */
31 3
    public function __construct(InventorySerializer $inv)
32 3
    {
33 3
        $this->invSerializer = $inv;
34 3
        $this->vectorInvSer = Types::vector(function (Parser $parser): Inventory {
35
            return $this->invSerializer->fromParser($parser);
36
        });
37
    }
38
39
    /**
40
     * @param Parser $parser
41 3
     * @return NotFound
42
     */
43 3
    public function fromParser(Parser $parser): NotFound
44 3
    {
45
        $items = $this->vectorInvSer->read($parser);
46
        return new NotFound($items);
47
    }
48
49
    /**
50
     * @param BufferInterface $data
51 3
     * @return NotFound
52
     */
53 3
    public function parse(BufferInterface $data): NotFound
54
    {
55
        return $this->fromParser(new Parser($data));
56
    }
57
58
    /**
59
     * @param NotFound $notFound
60 3
     * @return BufferInterface
61
     */
62 3
    public function serialize(NotFound $notFound): BufferInterface
63 3
    {
64 2
        return new Buffer($this->vectorInvSer->write($notFound->getItems()));
65
    }
66
}
67