Completed
Pull Request — master (#503)
by thomas
49:19 queued 47:08
created

WitnessProgram::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.6666
c 0
b 0
f 0
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 190
    public function __construct($version, BufferInterface $program)
32
    {
33 190
        if ($this->version < 0 || $this->version > 16) {
34
            throw new \RuntimeException("Invalid witness program version");
35
        }
36
37 190
        $this->version = $version;
38 190
        $this->program = $program;
39 190
    }
40
41
    /**
42
     * @param BufferInterface $program
43
     * @return WitnessProgram
44
     */
45 20
    public static function v0(BufferInterface $program)
46
    {
47 20
        if ($program->getSize() === 20) {
48 6
            return new self(self::V0, $program);
49 14
        } else if ($program->getSize() === 32) {
50 12
            return new self(self::V0, $program);
51
        } else {
52 2
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
53
        }
54
    }
55
56
    /**
57
     * @return int
58
     */
59 170
    public function getVersion()
60
    {
61 170
        return $this->version;
62
    }
63
64
    /**
65
     * @return BufferInterface
66
     */
67 168
    public function getProgram()
68
    {
69 168
        return $this->program;
70
    }
71
72
    /**
73
     * @return ScriptInterface
74
     */
75 26
    public function getScript()
76
    {
77 26
        if (null === $this->outputScript) {
78 24
            $this->outputScript = ScriptFactory::sequence([encodeOpN($this->version), $this->program]);
79
        }
80
81 26
        return $this->outputScript;
82
    }
83
}
84