Completed
Push — master ( 2dcc62...2dcc62 )
by thomas
151:35 queued 116:37
created

AlertSerializer::getSigBuf()   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
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
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\Bitcoin\Serializer\Types;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\Parser;
12
13
class AlertSerializer
14
{
15
    /**
16
     * @var AlertDetailSerializer
17
     */
18
    private $detail;
19
20
    /**
21
     * @param AlertDetailSerializer $detail
22
     */
23 96
    public function __construct(AlertDetailSerializer $detail)
24
    {
25 96
        $this->detail = $detail;
26 96
        $this->varstring = Types::varstring();
0 ignored issues
show
Bug introduced by
The property varstring does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    /**
30
     * @param Parser $parser
31 3
     * @return Alert
32
     */
33 3
    public function fromParser(Parser $parser)
34 3
    {
35 3
        $detailBuffer = $this->varstring->read($parser);
36
        $detail = $this->detail->fromParser(new Parser($detailBuffer));
37
38
        $sigBuffer = $this->varstring->read($parser);
39
        $adapter = Bitcoin::getEcAdapter();
40
        $serializer = EcSerializer::getSerializer('BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface', true, $adapter);
41
        $sig = $serializer->parse($sigBuffer);
42 3
43
        return new Alert(
44 3
            $detail,
45
            $sig
46 3
        );
47 3
    }
48 3
49 3
    /**
50
     * @param $data
51 3
     * @return Alert
52 2
     */
53
    public function parse($data)
54 2
    {
55
        return $this->fromParser(new Parser($data));
56
    }
57
58
    /**
59
     * @param Alert $alert
60
     * @return \BitWasp\Buffertools\Buffer
61 3
     */
62
    public function serialize(Alert $alert)
63 3
    {
64
        return new Buffer("{$this->varstring->write($alert->getDetail()->getBuffer())}{$this->varstring->write($alert->getSignature()->getBuffer())}");
65
    }
66
}
67