LightningPaymentMessageTrait::getAmountPaid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the ngutech/lightning-interop project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace NGUtech\Lightning\Message;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\ValueObject\Timestamp;
13
use NGUtech\Bitcoin\ValueObject\Bitcoin;
14
use NGUtech\Bitcoin\ValueObject\Hash;
15
16
trait LightningPaymentMessageTrait
17
{
18
    private Hash $preimage;
19
20
    private Hash $preimageHash;
21
22
    private ?Bitcoin $amount;
23
24
    private ?Bitcoin $amountPaid;
25
26
    private ?Timestamp $timestamp;
27
28
    /** @param array $state */
29
    public static function fromNative($state): self
30
    {
31
        Assertion::isArray($state);
32
        Assertion::keyExists($state, 'preimage');
33
        Assertion::keyExists($state, 'preimageHash');
34
35
        return new self(
36
            Hash::fromNative($state['preimage']),
37
            Hash::fromNative($state['preimageHash']),
38
            $state['amount'] ? Bitcoin::fromNative($state['amount']) : null,
39
            $state['amountPaid'] ? Bitcoin::fromNative($state['amountPaid']) : null,
40
            $state['timestamp'] ? Timestamp::fromNative($state['timestamp']) : null,
41
        );
42
    }
43
44
    public function getPreimage(): Hash
45
    {
46
        return $this->preimage;
47
    }
48
49
    public function getPreimageHash(): Hash
50
    {
51
        return $this->preimageHash;
52
    }
53
54
    public function getAmount(): ?Bitcoin
55
    {
56
        return $this->amount;
57
    }
58
59
    public function getAmountPaid(): ?Bitcoin
60
    {
61
        return $this->amountPaid;
62
    }
63
64
    public function getTimestamp(): ?Timestamp
65
    {
66
        return $this->timestamp;
67
    }
68
69
    public function toNative(): array
70
    {
71
        return [
72
            'preimage' => $this->preimage->toNative(),
73
            'preimageHash' => $this->preimageHash->toNative(),
74
            'amount' => $this->amount ? $this->amount->toNative() : null,
75
            'amountPaid' => $this->amountPaid ? $this->amountPaid->toNative() : null,
76
            'timestamp' => $this->timestamp ? (string)$this->timestamp : null,
77
        ];
78
    }
79
80
    private function __construct(
81
        Hash $preimage,
82
        Hash $preimageHash,
83
        Bitcoin $amount = null,
84
        Bitcoin $amountPaid = null,
85
        Timestamp $timestamp = null
86
    ) {
87
        $this->preimage = $preimage;
88
        $this->preimageHash = $preimageHash;
89
        $this->amount = $amount;
90
        $this->amountPaid = $amountPaid;
91
        $this->timestamp = $timestamp;
92
    }
93
}
94