Completed
Pull Request — master (#517)
by thomas
67:46 queued 65:32
created

WitnessProgram   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 6
A v0() 0 10 3
A getVersion() 0 4 1
A getProgram() 0 4 1
A getScript() 0 8 2
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 206
    public function __construct($version, BufferInterface $program)
32
    {
33 206
        if ($this->version < 0 || $this->version > 16) {
34
            throw new \RuntimeException("Invalid witness program version");
35
        }
36
37 206
        if ($this->version === 0 && ($program->getSize() !== 20 && $program->getSize() !== 32)) {
38
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
39
        }
40
41 206
        $this->version = $version;
42 206
        $this->program = $program;
43 206
    }
44
45
    /**
46
     * @param BufferInterface $program
47
     * @return WitnessProgram
48
     */
49 28
    public static function v0(BufferInterface $program)
50
    {
51 28
        if ($program->getSize() === 20) {
52 10
            return new self(self::V0, $program);
53 18
        } else if ($program->getSize() === 32) {
54 16
            return new self(self::V0, $program);
55
        } else {
56 2
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
57
        }
58
    }
59
60
    /**
61
     * @return int
62
     */
63 184
    public function getVersion()
64
    {
65 184
        return $this->version;
66
    }
67
68
    /**
69
     * @return BufferInterface
70
     */
71 182
    public function getProgram()
72
    {
73 182
        return $this->program;
74
    }
75
76
    /**
77
     * @return ScriptInterface
78
     */
79 38
    public function getScript()
80
    {
81 38
        if (null === $this->outputScript) {
82 36
            $this->outputScript = ScriptFactory::sequence([encodeOpN($this->version), $this->program]);
83
        }
84
85 38
        return $this->outputScript;
86
    }
87
}
88