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

AlertSerializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 9
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromParser() 0 15 1
A parse() 0 4 1
A serialize() 0 4 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