Completed
Pull Request — master (#476)
by thomas
24:45
created

WitnessProgram::v0()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 9.4285
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Buffertools\BufferInterface;
6
7
class WitnessProgram
8
{
9
    const V0 = 0;
10
11
    /**
12
     * @var int
13
     */
14
    private $version;
15
16
    /**
17
     * @var BufferInterface
18
     */
19
    private $program;
20
21
    /**
22
     * @var
23
     */
24
    private $outputScript;
25
26
    /**
27
     * WitnessProgram constructor.
28
     * @param int $version
29
     * @param BufferInterface $program
30
     */
31 101
    public function __construct($version, BufferInterface $program)
32
    {
33 101
        $this->version = $version;
34 101
        $this->program = $program;
35 101
    }
36
37
    /**
38
     * @param BufferInterface $program
39
     * @return WitnessProgram
40
     */
41 4
    public static function v0(BufferInterface $program)
42
    {
43 4
        if ($program->getSize() === 20) {
44
            return new self(self::V0, $program);
45 4
        } else if ($program->getSize() === 32) {
46 4
            return new self(self::V0, $program);
47
        } else {
48
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
49
        }
50
    }
51
52
    /**
53
     * @return int
54
     */
55 91
    public function getVersion()
56
    {
57 91
        return $this->version;
58
    }
59
60
    /**
61
     * @return BufferInterface
62
     */
63 90
    public function getProgram()
64
    {
65 90
        return $this->program;
66
    }
67
68
    /**
69
     * @return ScriptInterface
70
     */
71 6
    public function getScript()
72
    {
73 6
        if (null === $this->outputScript) {
74 4
            $this->outputScript = ScriptFactory::sequence([$this->version, $this->program]);
75
        }
76
77 6
        return $this->outputScript;
78
    }
79
}
80