Completed
Pull Request — master (#278)
by thomas
73:26 queued 01:49
created

OutPoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 7
c 6
b 0
f 3
lcom 1
cbo 5
dl 0
loc 71
ccs 15
cts 15
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
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 1140
    public function __construct(BufferInterface $hashPrevOutput, $nPrevOutput)
30
    {
31 1140
        if ($hashPrevOutput->getSize() !== 32) {
32 6
            throw new \InvalidArgumentException('OutPoint: hashPrevOut must be a 32-byte Buffer');
33
        }
34
        
35 1134
        $this->hashPrevOutput = $hashPrevOutput;
36 1134
        $this->nPrevOutput = $nPrevOutput;
37
38 1134
        $this
39 1134
            ->initFunctionAlias('txid', 'getTxId')
40 1134
            ->initFunctionAlias('vout', 'getVout');
41 1134
    }
42
43
    /**
44
     * @return BufferInterface
45
     */
46 342
    public function getTxId()
47
    {
48 342
        return $this->hashPrevOutput;
49
    }
50
51
    /**
52
     * @return int
53
     */
54 348
    public function getVout()
55
    {
56 348
        return $this->nPrevOutput;
57
    }
58
59
    /**
60
     * @param OutPointInterface $outPoint
61
     * @return int
62 72
     */
63
    public function equals(OutPointInterface $outPoint)
64 72
    {
65
        $txid = strcmp($this->getTxId()->getBinary(), $outPoint->getTxId()->getBinary());
66
        if ($txid !== 0) {
67
            return false;
68
        }
69
70
        return gmp_cmp($this->getVout(), $outPoint->getVout()) === 0;
71
    }
72
73
    /**
74
     * @return BufferInterface
75
     */
76
    public function getBuffer()
77
    {
78
        return (new OutPointSerializer())->serialize($this);
79
    }
80
}
81