LightningInvoiceMessageTrait::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 0
cts 9
cp 0
crap 2
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Natural;
13
use Daikon\ValueObject\Text;
14
use Daikon\ValueObject\Timestamp;
15
use NGUtech\Bitcoin\ValueObject\Bitcoin;
16
use NGUtech\Bitcoin\ValueObject\Hash;
17
18
trait LightningInvoiceMessageTrait
19
{
20
    private Hash $preimage;
21
22
    private Hash $preimageHash;
23
24
    private ?Text $request;
25
26
    private ?Text $label;
27
28
    private ?Bitcoin $amount;
29
30
    private ?Bitcoin $amountPaid;
31
32
    private ?Timestamp $timestamp;
33
34
    private ?Natural $cltvExpiry;
35
36
    /** @param array $state */
37
    public static function fromNative($state): self
38
    {
39
        Assertion::isArray($state);
40
        Assertion::keyExists($state, 'preimageHash');
41
42
        return new self(
43
            Hash::fromNative($state['preimage'] ?? null),
44
            Hash::fromNative($state['preimageHash']),
45
            isset($state['request']) ? Text::fromNative($state['request']) : null,
46
            isset($state['label']) ? Text::fromNative($state['label']) : null,
47
            isset($state['amount']) ? Bitcoin::fromNative($state['amount']) : null,
48
            isset($state['amountPaid']) ? Bitcoin::fromNative($state['amountPaid']) : null,
49
            isset($state['timestamp']) ? Timestamp::fromNative($state['timestamp']) : null,
50
            isset($state['cltvExpiry']) ? Natural::fromNative($state['cltvExpiry']) : null
51
        );
52
    }
53
54
    public function getPreimage(): Hash
55
    {
56
        return $this->preimage;
57
    }
58
59
    public function getPreimageHash(): Hash
60
    {
61
        return $this->preimageHash;
62
    }
63
64
    public function getRequest(): ?Text
65
    {
66
        return $this->request;
67
    }
68
69
    public function getLabel(): ?Text
70
    {
71
        return $this->label;
72
    }
73
74
    public function getAmount(): ?Bitcoin
75
    {
76
        return $this->amount;
77
    }
78
79
    public function getAmountPaid(): ?Bitcoin
80
    {
81
        return $this->amountPaid;
82
    }
83
84
    public function getTimestamp(): ?Timestamp
85
    {
86
        return $this->timestamp;
87
    }
88
89
    public function getCltvExpiry(): ?Natural
90
    {
91
        return $this->cltvExpiry;
92
    }
93
94
    public function toNative(): array
95
    {
96
        return [
97
            'preimage' => $this->preimage->toNative(),
98
            'preimageHash' => $this->preimageHash->toNative(),
99
            'request' => $this->request ? (string)$this->request : null,
100
            'label' => $this->label ? (string)$this->label : null,
101
            'amount' => $this->amount ? $this->amount->toNative() : null,
102
            'amountPaid' => $this->amountPaid ? $this->amountPaid->toNative() : null,
103
            'timestamp' => $this->timestamp ? (string)$this->timestamp : null,
104
            'cltvExpiry' => $this->cltvExpiry ? $this->cltvExpiry->toNative() : null
105
        ];
106
    }
107
108
    private function __construct(
109
        Hash $preimage,
110
        Hash $preimageHash,
111
        Text $request = null,
112
        Text $label = null,
113
        Bitcoin $amount = null,
114
        Bitcoin $amountPaid = null,
115
        Timestamp $timestamp = null,
116
        Natural $cltvExpiry = null
117
    ) {
118
        $this->preimage = $preimage;
119
        $this->preimageHash = $preimageHash;
120
        $this->request = $request;
121
        $this->label = $label;
122
        $this->amount = $amount;
123
        $this->amountPaid = $amountPaid;
124
        $this->timestamp = $timestamp;
125
        $this->cltvExpiry = $cltvExpiry;
126
    }
127
}
128