Completed
Push — master ( 73a6c4...cc9f4e )
by thomas
20:03
created

BlockFactory::fromBuffer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Block;
6
7
use BitWasp\Bitcoin\Bitcoin;
8
use BitWasp\Bitcoin\Math\Math;
9
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer;
10
use BitWasp\Bitcoin\Serializer\Block\BlockSerializer;
11
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer;
12
use BitWasp\Buffertools\Buffer;
13
use BitWasp\Buffertools\BufferInterface;
14
15
class BlockFactory
16
{
17
    /**
18
     * @param string $string
19
     * @param Math|null $math
20
     * @return BlockInterface
21
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
22
     * @throws \Exception
23
     */
24 10
    public static function fromHex(string $string, Math $math = null): BlockInterface
25
    {
26 10
        return self::fromBuffer(Buffer::hex($string), $math);
27
    }
28
29
    /**
30
     * @param BufferInterface $buffer
31
     * @param Math|null $math
32
     * @return BlockInterface
33
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
34
     */
35 10
    public static function fromBuffer(BufferInterface $buffer, Math $math = null): BlockInterface
36
    {
37 10
        $serializer = new BlockSerializer(
38 10
            $math ?: Bitcoin::getMath(),
39 10
            new BlockHeaderSerializer(),
40 10
            new TransactionSerializer()
41
        );
42
43 10
        return $serializer->parse($buffer);
44
    }
45
}
46