Completed
Pull Request — master (#45)
by thomas
37:30
created

AlertSerializer::getSigBuf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Serializer\Message;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
7
use BitWasp\Bitcoin\Networking\Messages\Alert;
8
use BitWasp\Bitcoin\Networking\Serializer\Structure\AlertDetailSerializer;
9
use BitWasp\Buffertools\Buffertools;
10
use BitWasp\Buffertools\Parser;
11
use BitWasp\Buffertools\TemplateFactory;
12
13
class AlertSerializer
14
{
15
    /**
16
     * @var AlertDetailSerializer
17
     */
18
    private $detail;
19
20
    /**
21
     * @param AlertDetailSerializer $detail
22
     */
23
    public function __construct(AlertDetailSerializer $detail)
24
    {
25 99
        $this->detail = $detail;
26
    }
27 99
28 99
    /**
29
     * @return \BitWasp\Buffertools\Template
30
     */
31
    public function getSigBuf()
32
    {
33 3
        return (new TemplateFactory())
34
            ->varstring()
35 3
            ->getTemplate();
36 3
    }
37 3
38 3
    /**
39
     * @param Parser $parser
40
     * @return Alert
41
     */
42
    public function fromParser(Parser & $parser)
43
    {
44
        $detail = $this->detail->fromParser($parser);
45 3
46
        list ($sigBuffer) = $this->getSigBuf()->parse($parser);
47 3
        $adapter = Bitcoin::getEcAdapter();
48
        $serializer = EcSerializer::getSerializer($adapter, 'BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface');
49 3
        $sig = $serializer->parse($sigBuffer);
50 3
51
        return new Alert(
52 3
            $detail,
53
            $sig
54
        );
55
    }
56
57
    /**
58
     * @param $data
59
     * @return Alert
60
     */
61
    public function parse($data)
62
    {
63
        return $this->fromParser(new Parser($data));
64 3
    }
65
66
    /**
67 3
     * @param Alert $alert
68 3
     * @return \BitWasp\Buffertools\Buffer
69
     */
70 3
    public function serialize(Alert $alert)
71
    {
72
        $detail = $alert->getDetail()->getBuffer();
73
        $sig = $this->getSigBuf()->write([$alert->getSignature()->getBuffer()]);
74
        return Buffertools::concat(
75
            $detail,
76
            $sig
77 3
        );
78
    }
79
}
80