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

BlockFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromHex() 0 4 1
A fromBuffer() 0 10 2
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