Completed
Pull Request — master (#446)
by thomas
228:15 queued 224:06
created

BitcoindBlockSerializer::getHeaderTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 18
     */
28
    public function __construct(NetworkInterface $network, BlockSerializer $blockSerializer)
29 18
    {
30 18
        $this->blockSerializer = $blockSerializer;
31 18
        $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
        $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
        $this->network = $network;
34
    }
35
36 18
    /**
37
     * @param BlockInterface $block
38 18
     * @return \BitWasp\Buffertools\BufferInterface
39 18
     */
40 18
    public function serialize(BlockInterface $block)
41 18
    {
42
        $buffer = $this->blockSerializer->serialize($block);
43
        $size = $buffer->getSize();
44
        return new Buffer(
45
            Buffertools::flipBytes(pack("H*", $this->network->getNetMagicBytes())) .
46
            $this->size->write($size) .
47
            $buffer->getBinary()
48 6
        );
49
    }
50 6
51 6
    /**
52 6
     * @param Parser $parser
53 6
     * @return BlockInterface
54 3
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
55 3
     */
56
    public function fromParser(Parser $parser)
57 6
    {
58
        /** @var Buffer $bytes */
59 6
        /** @var int|string $blockSize */
60
        list ($bytes, $blockSize) = [$this->magic->read($parser), $this->size->read($parser)];
61
        if ($bytes->getHex() !== $this->network->getNetMagicBytes()) {
62
            throw new \RuntimeException('Block version bytes did not match network');
63
        }
64
65
        return $this->blockSerializer->fromParser(new Parser($parser->readBytes($blockSize)));
66
    }
67 18
68
    /**
69
     * @param \BitWasp\Buffertools\BufferInterface|string $data
70
     * @return BlockInterface
71 18
     */
72 18
    public function parse($data)
73 6
    {
74
        return $this->fromParser(new Parser($data));
75
    }
76
}
77