Completed
Pull Request — master (#392)
by thomas
190:09 queued 117:15
created

WitnessProgram::v0()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 3
rs 9.4285
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
     * WitnessProgram constructor.
23
     * @param int $version
24 111
     * @param BufferInterface $program
25
     */
26 111
    public function __construct($version, BufferInterface $program)
27 111
    {
28 111
        $this->version = $version;
29
        $this->program = $program;
30
    }
31
32
    /**
33 91
     * @param BufferInterface $program
34
     * @return WitnessProgram
35 91
     */
36
    public static function v0(BufferInterface $program)
37
    {
38
        if ($program->getSize() === 20) {
39
            return new self(self::V0, $program);
40
        } else if ($program->getSize() === 20) {
41 87
            return new self(self::V0, $program);
42
        } else {
43 87
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
44
        }
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function getVersion()
51
    {
52
        return $this->version;
53
    }
54
55
    /**
56
     * @return BufferInterface
57
     */
58
    public function getProgram()
59
    {
60
        return $this->program;
61
    }
62
63
    /**
64
     * @return ScriptInterface
65
     */
66
    public function getScript()
67
    {
68
        return ScriptFactory::create()
69
            ->int($this->version)
70
            ->push($this->program)
71
            ->getScript();
72
    }
73
}
74