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

TransactionOutput   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 72%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 86
ccs 18
cts 25
cp 0.72
rs 10
wmc 8
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __clone() 0 4 1
A __debugInfo() 0 7 1
A getValue() 0 4 1
A getScript() 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\Script\ScriptInterface;
6
use BitWasp\Bitcoin\Serializable;
7
use BitWasp\Bitcoin\Serializer\Transaction\TransactionOutputSerializer;
8
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
9
10
class TransactionOutput extends Serializable implements TransactionOutputInterface
11
{
12
    use FunctionAliasArrayAccess;
13
14
    /**
15
     * @var string|int
16
     */
17
    private $value;
18
19
    /**
20
     * @var ScriptInterface
21
     */
22
    private $script;
23
24
    /**
25
     * Initialize class
26
     *
27
     * @param int $value
28
     * @param ScriptInterface $script
29
     */
30 3534
    public function __construct($value, ScriptInterface $script)
31
    {
32 3534
        $this->value = $value;
33 3534
        $this->script = $script;
34 2356
        $this
35 3534
            ->initFunctionAlias('value', 'getValue')
36 3534
            ->initFunctionAlias('script', 'getScript');
37 3534
    }
38
39
    /**
40
     * @return void
41
     */
42
    public function __clone()
43
    {
44
        $this->script = clone $this->script;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function __debugInfo()
51
    {
52
        return [
53
            'value' => $this->value,
54
            'script' => $this->script
55
        ];
56
    }
57
58
    /**
59
     * @see TransactionOutputInterface::getValue()
60
     */
61 3483
    public function getValue()
62
    {
63 3483
        return $this->value;
64
    }
65
66
    /**
67
     * @see TransactionOutputInterface::getScript()
68
     */
69 3477
    public function getScript()
70
    {
71 3477
        return $this->script;
72
    }
73
74
    /**
75
     * @param TransactionOutputInterface $output
76
     * @return bool
77
     */
78 6
    public function equals(TransactionOutputInterface $output)
79
    {
80 6
        $script = $this->script->equals($output->getScript());
81 6
        if (!$script) {
82 6
            return false;
83
        }
84
85 6
        return gmp_cmp($this->value, $output->getValue()) === 0;
86
    }
87
88
    /**
89
     * @see \BitWasp\Bitcoin\SerializableInterface::getBuffer()
90
     */
91 3423
    public function getBuffer()
92
    {
93 3423
        return (new TransactionOutputSerializer())->serialize($this);
94
    }
95
}
96