Completed
Pull Request — master (#392)
by thomas
24:31
created

WitnessProgram   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 42.11%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 8
cts 19
cp 0.4211
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A V0() 0 10 3
A getVersion() 0 4 1
A getProgram() 0 4 1
A getScript() 0 7 1
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
     * @param BufferInterface $program
25
     */
26 69
    public function __construct($version, BufferInterface $program)
27
    {
28 69
        $this->version = $version;
29 69
        $this->program = $program;
30 69
    }
31
32
    /**
33
     * @param BufferInterface $program
34
     * @return WitnessProgram
35
     */
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
            return new self(self::V0, $program);
42
        } else {
43
            throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes');
44
        }
45
    }
46
47
    /**
48
     * @return int
49
     */
50 53
    public function getVersion()
51
    {
52 53
        return $this->version;
53
    }
54
55
    /**
56
     * @return BufferInterface
57
     */
58 51
    public function getProgram()
59
    {
60 51
        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