Completed
Push — master ( 410ea8...0c9ae2 )
by thomas
105:23 queued 33:17
created

InputMutator::replace()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
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 118
    /**
20
     * @param TransactionInputInterface $input
21 118
     */
22 118
    public function __construct(TransactionInputInterface $input)
23
    {
24
        $this->input = $input;
25
    }
26
27 106
    /**
28
     * @return TransactionInputInterface
29 106
     */
30
    public function done()
31
    {
32
        return $this->input;
33
    }
34
35
    /**
36 112
     * @param array $array
37
     * @return $this
38 112
     */
39 112
    private function replace(array $array = [])
40 112
    {
41 112
        $this->input = new TransactionInput(
42 112
            array_key_exists('outpoint', $array) ? $array['outpoint'] : $this->input->getOutPoint(),
43 112
            array_key_exists('script', $array) ? $array['script'] : $this->input->getScript(),
44
            array_key_exists('nSequence', $array) ? $array['nSequence'] : $this->input->getSequence()
45 112
        );
46
47
        return $this;
48
    }
49
50
    /**
51 6
     * @param OutPointInterface $outPoint
52
     * @return InputMutator
53 6
     */
54
    public function outpoint(OutPointInterface $outPoint)
55
    {
56
        return $this->replace(array('outpoint' => $outPoint));
57
    }
58
59
60 6
    /**
61
     * @return $this
62 6
     */
63
    public function null()
64
    {
65
        return $this->replace(array('outpoint' => new OutPoint(new Buffer(str_pad('', 32, "\x00"), 32), 0xffffffff)));
66
    }
67
68
    /**
69 12
     * @param Buffer $txid
70
     * @return $this
71 12
     */
72
    public function txid(Buffer $txid)
73
    {
74
        return $this->replace(array('txid' => $txid));
75
    }
76
77
    /**
78 106
     * @param int $vout
79
     * @return InputMutator
80 106
     */
81
    public function vout($vout)
82
    {
83
        return $this->replace(array('vout' => $vout));
84
    }
85
86
    /**
87 12
     * @param ScriptInterface $script
88
     * @return $this
89 12
     */
90
    public function script(ScriptInterface $script)
91
    {
92
        return $this->replace(array('script' => $script));
93
    }
94
95
    /**
96
     * @param int $nSequence
97
     * @return $this
98
     */
99
    public function sequence($nSequence)
100
    {
101
        return $this->replace(array('nSequence' => $nSequence));
102
    }
103
}
104