AlertDetailSerializer::fromParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 1
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Serializer\Structure;
6
7
use BitWasp\Bitcoin\Networking\Structure\AlertDetail;
8
use BitWasp\Bitcoin\Serializer\Types;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
use BitWasp\Buffertools\Parser;
12
13
class AlertDetailSerializer
14
{
15 6
    /**
16
     * @var \BitWasp\Buffertools\Types\Uint32
17 6
     */
18 6
    private $uint32le;
19 6
    /**
20 6
     * @var \BitWasp\Buffertools\Types\Uint64
21 6
     */
22 6
    private $uint64le;
23
    /**
24 6
     * @var \BitWasp\Buffertools\Types\Vector
25 6
     */
26 6
    private $vectorUint32le;
27 6
    /**
28 6
     * @var \BitWasp\Buffertools\Types\VarString
29 6
     */
30 6
    private $varstring;
31 6
32 6
    public function __construct()
33 6
    {
34 6
        $this->uint32le = Types::uint32le();
35
        $this->uint64le = Types::uint64le();
36
        $this->vectorUint32le = Types::vector(function (Parser $parser): int {
37 6
            return (int) $this->uint32le->read($parser);
38
        });
39 6
        $this->varstring = Types::varstring();
40
    }
41
42 6
    /**
43
     * @param Parser $parser
44 6
     * @return AlertDetail
45
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
46 6
     */
47
    public function fromParser(Parser $parser): AlertDetail
48 6
    {
49
        return new AlertDetail(
50 6
            (int) $this->uint32le->read($parser),
51
            (int) $this->uint64le->read($parser),
52 6
            (int) $this->uint64le->read($parser),
53
            (int) $this->uint32le->read($parser),
54 6
            (int) $this->uint32le->read($parser),
55
            $this->vectorUint32le->read($parser),
56 6
            (int) $this->uint32le->read($parser),
57
            (int) $this->uint32le->read($parser),
58 6
            $this->vectorUint32le->read($parser),
59
            (int) $this->uint32le->read($parser),
60 6
            $this->varstring->read($parser),
61
            $this->varstring->read($parser)
62 6
        );
63
    }
64 6
65
    /**
66 6
     * @param BufferInterface $data
67 4
     * @return AlertDetail
68 4
     */
69 4
    public function parse(BufferInterface $data): AlertDetail
70 4
    {
71 4
        return $this->fromParser(new Parser($data));
72 4
    }
73 4
74 4
    /**
75 4
     * @param AlertDetail $detail
76 4
     * @return BufferInterface
77 4
     */
78
    public function serialize(AlertDetail $detail): BufferInterface
79 4
    {
80
        $setCancels = [];
81
        foreach ($detail->getSetCancel() as $toCancel) {
82
            $setCancels[] = new Buffer(pack('V', $toCancel));
83
        }
84
85
        $setSubVers = [];
86 3
        foreach ($detail->getSetSubVer() as $subVer) {
87
            $setSubVers[] = new Buffer(pack('V', $subVer));
88 3
        }
89
90
        return new Buffer(
91
            sprintf(
92
                "%s%s%s%s%s%s%s%s%s%s%s%s",
93
                $this->uint32le->write($detail->getVersion()),
94
                $this->uint64le->write($detail->getRelayUntil()),
95 6
                $this->uint64le->write($detail->getExpiration()),
96
                $this->uint32le->write($detail->getId()),
97 6
                $this->uint32le->write($detail->getCancel()),
98 6
                $this->vectorUint32le->write($setCancels),
99 6
                $this->uint32le->write($detail->getMinVer()),
100 6
                $this->uint32le->write($detail->getMaxVer()),
101 4
                $this->vectorUint32le->write($setSubVers),
102
                $this->uint32le->write($detail->getPriority()),
103 6
                $this->varstring->write($detail->getComment()),
104 6
                $this->varstring->write($detail->getStatusBar())
105 6
            )
106 6
        );
107 4
    }
108
}
109