Completed
Pull Request — master (#591)
by thomas
22:54 queued 09:12
created

BlockFactory::fromHex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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