Completed
Pull Request — master (#240)
by thomas
73:04
created

BlockSerializer::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Block;
4
5
use BitWasp\Bitcoin\Block\Block;
6
use BitWasp\Bitcoin\Collection\Transaction\TransactionCollection;
7
use BitWasp\Buffertools\Buffertools;
8
use BitWasp\Buffertools\Exceptions\ParserOutOfRange;
9
use BitWasp\Bitcoin\Math\Math;
10
use BitWasp\Buffertools\Parser;
11
use BitWasp\Bitcoin\Block\BlockInterface;
12
use BitWasp\Bitcoin\Serializer\Transaction\MTransactionSerializer;
13
use BitWasp\Buffertools\TemplateFactory;
14
15
class BlockSerializer
16
{
17
    /**
18
     * @var Math
19
     */
20
    private $math;
21
22
    /**
23
     * @var BlockHeaderSerializer
24
     */
25
    private $headerSerializer;
26
27
    /**
28
     * @var TransactionSerializer
29
     */
30
    private $txSerializer;
31
32
    /**
33
     * @param Math $math
34
     * @param BlockHeaderSerializer $headerSerializer
35
     * @param TransactionSerializer $txSerializer
36
     */
37 90
    public function __construct(Math $math, BlockHeaderSerializer $headerSerializer, MTransactionSerializer $txSerializer)
38
    {
39 90
        $this->math = $math;
40 90
        $this->headerSerializer = $headerSerializer;
41 90
        $this->txSerializer = $txSerializer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $txSerializer of type object<BitWasp\Bitcoin\S...MTransactionSerializer> is incompatible with the declared type object<BitWasp\Bitcoin\S...\TransactionSerializer> of property $txSerializer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42 90
    }
43
44
    /**
45
     * @return \BitWasp\Buffertools\Template
46
     */
47 78
    private function getTxsTemplate()
48
    {
49 78
        return (new TemplateFactory())
50 78
            ->vector(function (Parser &$parser) {
51 72
                return $this->txSerializer->fromParser($parser);
52 78
            })
53 78
            ->getTemplate();
54
    }
55
56
    /**
57
     * @param Parser $parser
58
     * @return BlockInterface
59
     * @throws ParserOutOfRange
60
     */
61 84
    public function fromParser(Parser $parser)
62
    {
63
        try {
64 84
            return new Block(
65 84
                $this->math,
66 84
                $this->headerSerializer->fromParser($parser),
67 78
                new TransactionCollection($this->getTxsTemplate()->parse($parser)[0])
68 78
            );
69 6
        } catch (ParserOutOfRange $e) {
70 6
            throw new ParserOutOfRange('Failed to extract full block header from parser');
71
        }
72
    }
73
74
    /**
75
     * @param \BitWasp\Buffertools\Buffer|string $string
76
     * @return BlockInterface
77
     * @throws ParserOutOfRange
78
     */
79 72
    public function parse($string)
80
    {
81 72
        return $this->fromParser(new Parser($string));
82
    }
83
84
    /**
85
     * @param BlockInterface $block
86
     * @return \BitWasp\Buffertools\Buffer
87
     */
88 24
    public function serialize(BlockInterface $block)
89
    {
90 24
        return Buffertools::concat(
91 24
            $this->headerSerializer->serialize($block->getHeader()),
92 24
            $this->getTxsTemplate()->write([$block->getTransactions()->all()])
93 24
        );
94
    }
95
}
96