Completed
Push — master ( 3be9f6...d78503 )
by thomas
28:30
created

SignData::logicalPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
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 3
nop 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Transaction\Factory;
6
7
use BitWasp\Bitcoin\Script\ScriptInterface;
8
use BitWasp\Bitcoin\Script\WitnessScript;
9
10
class SignData
11
{
12
    /**
13
     * @var ScriptInterface
14
     */
15
    protected $redeemScript = null;
16
17
    /**
18
     * @var ScriptInterface
19
     */
20
    protected $witnessScript = null;
21
22
    /**
23
     * @var int
24
     */
25
    protected $signaturePolicy = null;
26
27
    /**
28
     * @var bool[]
29
     */
30
    protected $logicalPath = null;
31
32
    /**
33
     * @param ScriptInterface $redeemScript
34
     * @return $this
35
     */
36 40
    public function p2sh(ScriptInterface $redeemScript)
37
    {
38 40
        if ($redeemScript instanceof WitnessScript) {
39
            throw new \InvalidArgumentException("Cannot pass WitnessScript as a redeemScript");
40
        }
41 40
        $this->redeemScript = $redeemScript;
42 40
        return $this;
43
    }
44
45
    /**
46
     * @return bool
47
     */
48 104
    public function hasRedeemScript(): bool
49
    {
50 104
        return $this->redeemScript instanceof ScriptInterface;
51
    }
52
53
    /**
54
     * @return ScriptInterface
55
     */
56 63
    public function getRedeemScript(): ScriptInterface
57
    {
58 63
        if (null === $this->redeemScript) {
59 1
            throw new \RuntimeException('Redeem script requested but not set');
60
        }
61
62 62
        return $this->redeemScript;
63
    }
64
65
    /**
66
     * @param ScriptInterface $witnessScript
67
     * @return $this
68
     */
69 33
    public function p2wsh(ScriptInterface $witnessScript)
70
    {
71 33
        $this->witnessScript = $witnessScript;
72 33
        return $this;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 102
    public function hasWitnessScript(): bool
79
    {
80 102
        return $this->witnessScript instanceof ScriptInterface;
81
    }
82
83
    /**
84
     * @return ScriptInterface
85
     */
86 54
    public function getWitnessScript(): ScriptInterface
87
    {
88 54
        if (null === $this->witnessScript) {
89 1
            throw new \RuntimeException('Witness script requested but not set');
90
        }
91
92 53
        return $this->witnessScript;
93
    }
94
    
95
    /**
96
     * @param int $flags
97
     * @return $this
98
     */
99 16
    public function signaturePolicy(int $flags)
100
    {
101 16
        $this->signaturePolicy = $flags;
102 16
        return $this;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108 130
    public function hasSignaturePolicy(): bool
109
    {
110 130
        return $this->signaturePolicy !== null;
111
    }
112
113
    /**
114
     * @return int
115
     */
116 67
    public function getSignaturePolicy(): int
117
    {
118 67
        if (null === $this->signaturePolicy) {
119 1
            throw new \RuntimeException('Signature policy requested but not set');
120
        }
121 66
        return $this->signaturePolicy;
122
    }
123
124
    /**
125
     * @param bool[] $vfPathTaken
126
     * @return $this
127
     */
128 18
    public function logicalPath(array $vfPathTaken)
129
    {
130 18
        foreach ($vfPathTaken as $value) {
131 11
            if (!is_bool($value)) {
132
                throw new \RuntimeException("Invalid values for logical path, must be a boolean array");
133
            }
134
        }
135
136 18
        $this->logicalPath = $vfPathTaken;
137 18
        return $this;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function hasLogicalPath(): bool
144
    {
145
        return is_array($this->logicalPath);
146
    }
147
148
    /**
149
     * @return bool[]
150
     */
151 11
    public function getLogicalPath(): array
152
    {
153 11
        if (null === $this->logicalPath) {
154
            throw new \RuntimeException("Logical path requested but not set");
155
        }
156
157 11
        return $this->logicalPath;
158
    }
159
}
160