Completed
Pull Request — master (#222)
by thomas
24:34
created

InputMutator::replace()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 6
nc 1
nop 1
crap 4
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Mutator;
4
5
use BitWasp\Bitcoin\Script\ScriptInterface;
6
use BitWasp\Bitcoin\Transaction\OutPoint;
7
use BitWasp\Bitcoin\Transaction\OutPointInterface;
8
use BitWasp\Bitcoin\Transaction\TransactionInput;
9
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
10
use BitWasp\Buffertools\Buffer;
11
12
class InputMutator
13
{
14
    /**
15
     * @var TransactionInputInterface
16
     */
17
    private $input;
18
19
    /**
20
     * @param TransactionInputInterface $input
21
     */
22 117
    public function __construct(TransactionInputInterface $input)
23
    {
24 117
        $this->input = $input;
25 117
    }
26
27
    /**
28
     * @return TransactionInputInterface
29
     */
30 105
    public function done()
31
    {
32 105
        return $this->input;
33
    }
34
35
    /**
36
     * @param array $array
37
     * @return $this
38
     */
39 111
    private function replace(array $array = [])
40
    {
41 111
        $this->input = new TransactionInput(
42 111
            array_key_exists('outpoint', $array) ? $array['outpoint'] : $this->input->getOutPoint(),
43 111
            array_key_exists('script', $array) ? $array['script'] : $this->input->getScript(),
44 111
            array_key_exists('nSequence', $array) ? $array['nSequence'] : $this->input->getSequence()
45 111
        );
46
47 111
        return $this;
48
    }
49
50
    /**
51
     * @param OutPointInterface $outPoint
52
     * @return InputMutator
53
     */
54 6
    public function outpoint(OutPointInterface $outPoint)
55
    {
56 6
        return $this->replace(array('outpoint' => $outPoint));
57
    }
58
59
60
    /**
61
     * @return $this
62
     */
63 6
    public function null()
64
    {
65 6
        return $this->replace(array('outpoint' => new OutPoint(new Buffer(str_pad('', 32, "\x00"), 32), 0xffffffff)));
66
    }
67
68
    /**
69
     * @param Buffer $txid
70
     * @return $this
71
     */
72
    public function txid(Buffer $txid)
73
    {
74
        return $this->replace(array('txid' => $txid));
75
    }
76
77
    /**
78
     * @param int $vout
79
     * @return InputMutator
80
     */
81
    public function vout($vout)
82
    {
83
        return $this->replace(array('vout' => $vout));
84
    }
85
86
    /**
87
     * @param ScriptInterface $script
88
     * @return $this
89
     */
90 105
    public function script(ScriptInterface $script)
91
    {
92 105
        return $this->replace(array('script' => $script));
93
    }
94
95
    /**
96
     * @param int $nSequence
97
     * @return $this
98
     */
99 12
    public function sequence($nSequence)
100
    {
101 12
        return $this->replace(array('nSequence' => $nSequence));
102
    }
103
}
104