Completed
Pull Request — master (#446)
by thomas
105:00 queued 101:56
created

BitcoindBlockSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 4 1
A __construct() 0 7 1
A serialize() 0 10 1
A fromParser() 0 11 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Block;
4
5
use BitWasp\Bitcoin\Block\BlockInterface;
6
use BitWasp\Bitcoin\Network\NetworkInterface;
7
use BitWasp\Bitcoin\Serializer\Types;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\Buffertools;
10
use BitWasp\Buffertools\Parser;
11
12
class BitcoindBlockSerializer
13
{
14
    /**
15
     * @var NetworkInterface
16
     */
17
    private $network;
18
19
    /**
20
     * @var BlockSerializer
21
     */
22
    private $blockSerializer;
23
24
    /**
25
     * @param NetworkInterface $network
26
     * @param BlockSerializer $blockSerializer
27
     */
28 6
    public function __construct(NetworkInterface $network, BlockSerializer $blockSerializer)
29
    {
30 6
        $this->blockSerializer = $blockSerializer;
31 6
        $this->magic = Types::bytestringle(4);
0 ignored issues
show
Bug introduced by
The property magic 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...
32 6
        $this->size = Types::uint32le();
0 ignored issues
show
Bug introduced by
The property size 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...
33 6
        $this->network = $network;
34 6
    }
35
36
    /**
37
     * @param BlockInterface $block
38
     * @return \BitWasp\Buffertools\BufferInterface
39
     */
40 2
    public function serialize(BlockInterface $block)
41
    {
42 2
        $buffer = $this->blockSerializer->serialize($block);
43 2
        $size = $buffer->getSize();
44 2
        return new Buffer(
45 2
            Buffertools::flipBytes(pack("H*", $this->network->getNetMagicBytes())) .
46 2
            $this->size->write($size) .
47 2
            $buffer->getBinary()
48
        );
49
    }
50
51
    /**
52
     * @param Parser $parser
53
     * @return BlockInterface
54
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
55
     */
56 6
    public function fromParser(Parser $parser)
57
    {
58
        /** @var Buffer $bytes */
59
        /** @var int|string $blockSize */
60 6
        list ($bytes, $blockSize) = [$this->magic->read($parser), $this->size->read($parser)];
61 6
        if ($bytes->getHex() !== $this->network->getNetMagicBytes()) {
62 2
            throw new \RuntimeException('Block version bytes did not match network');
63
        }
64
65 4
        return $this->blockSerializer->fromParser(new Parser($parser->readBytes($blockSize)));
66
    }
67
68
    /**
69
     * @param \BitWasp\Buffertools\BufferInterface|string $data
70
     * @return BlockInterface
71
     */
72 2
    public function parse($data)
73
    {
74 2
        return $this->fromParser(new Parser($data));
75
    }
76
}
77