Completed
Push — master ( eabe8c...61f277 )
by thomas
25:26
created

TransactionOutput::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Transaction;
6
7
use BitWasp\Bitcoin\Script\ScriptInterface;
8
use BitWasp\Bitcoin\Serializable;
9
use BitWasp\Bitcoin\Serializer\Transaction\TransactionOutputSerializer;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class TransactionOutput extends Serializable implements TransactionOutputInterface
13
{
14
15
    /**
16
     * @var int
17
     */
18
    private $value;
19
20
    /**
21
     * @var ScriptInterface
22
     */
23
    private $script;
24
25
    /**
26
     * Initialize class
27
     *
28
     * @param int $value
29
     * @param ScriptInterface $script
30
     */
31 5212
    public function __construct(int $value, ScriptInterface $script)
32
    {
33 5212
        if ($value < 0) {
34
            throw new \RuntimeException('Transaction output value cannot be negative');
35
        }
36 5212
        $this->value = $value;
37 5212
        $this->script = $script;
38 5212
    }
39
40
    /**
41
     * {@inheritdoc}
42
     * @see TransactionOutputInterface::getValue()
43
     */
44 5298
    public function getValue(): int
45
    {
46 5298
        return $this->value;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     * @see TransactionOutputInterface::getScript()
52
     */
53 5295
    public function getScript(): ScriptInterface
54
    {
55 5295
        return $this->script;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     * @see TransactionOutputInterface::equals()
61
     */
62 9
    public function equals(TransactionOutputInterface $output): bool
63
    {
64 9
        $script = $this->script->equals($output->getScript());
65 9
        if (!$script) {
66 2
            return false;
67
        }
68
69 9
        return gmp_cmp($this->value, $output->getValue()) === 0;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     * @see \BitWasp\Bitcoin\SerializableInterface::getBuffer()
75
     */
76 1
    public function getBuffer(): BufferInterface
77
    {
78 1
        return (new TransactionOutputSerializer())->serialize($this);
79
    }
80
}
81