Completed
Pull Request — master (#332)
by thomas
38:04
created

OutPoint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 83.33%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 82
ccs 20
cts 24
cp 0.8333
rs 10
wmc 8
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A __debugInfo() 0 7 1
A getTxId() 0 4 1
A getVout() 0 4 1
A equals() 0 9 2
A getBuffer() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Serializable;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Buffertools\BufferInterface;
8
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
9
10
class OutPoint extends Serializable implements OutPointInterface
11
{
12
    use FunctionAliasArrayAccess;
13
14
    /**
15
     * @var BufferInterface
16
     */
17
    private $hashPrevOutput;
18
19
    /**
20
     * @var int
21
     */
22
    private $nPrevOutput;
23
24
    /**
25
     * OutPoint constructor.
26
     * @param BufferInterface $hashPrevOutput
27
     * @param int $nPrevOutput
28
     */
29 3864
    public function __construct(BufferInterface $hashPrevOutput, $nPrevOutput)
30
    {
31 3864
        if ($hashPrevOutput->getSize() !== 32) {
32 3
            throw new \InvalidArgumentException('OutPoint: hashPrevOut must be a 32-byte Buffer');
33
        }
34
        
35 3861
        $this->hashPrevOutput = $hashPrevOutput;
36 3861
        $this->nPrevOutput = $nPrevOutput;
37
38 2574
        $this
39 3861
            ->initFunctionAlias('txid', 'getTxId')
40 3861
            ->initFunctionAlias('vout', 'getVout');
41 3861
    }
42
43
    /**
44
     * @return array
45
     */
46
    public function __debugInfo()
47
    {
48
        return [
49
            'txid' => $this->hashPrevOutput,
50
            'vout' => $this->nPrevOutput,
51
        ];
52
    }
53
54
    /**
55
     * @return BufferInterface
56
     */
57 3453
    public function getTxId()
58
    {
59 3453
        return $this->hashPrevOutput;
60
    }
61
62
    /**
63
     * @return int
64
     */
65 3456
    public function getVout()
66
    {
67 3456
        return $this->nPrevOutput;
68
    }
69
70
    /**
71
     * @param OutPointInterface $outPoint
72
     * @return int
73
     */
74 9
    public function equals(OutPointInterface $outPoint)
75
    {
76 9
        $txid = strcmp($this->getTxId()->getBinary(), $outPoint->getTxId()->getBinary());
77 9
        if ($txid !== 0) {
78 6
            return false;
79
        }
80
81 9
        return gmp_cmp($this->getVout(), $outPoint->getVout()) === 0;
82
    }
83
84
    /**
85
     * @return BufferInterface
86
     */
87 96
    public function getBuffer()
88
    {
89 96
        return (new OutPointSerializer())->serialize($this);
90
    }
91
}
92