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

Uri::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin;
6
7
use BitWasp\Bitcoin\Address\AddressInterface;
8
use BitWasp\Bitcoin\Network\NetworkInterface;
9
10
class Uri
11
{
12
    const BIP0021 = 0;
13
    const BIP0072 = 1;
14
15
    /**
16
     * @var AddressInterface
17
     */
18
    private $address;
19
20
    /**
21
     * @var null|float
22
     */
23
    private $amount;
24
25
    /**
26
     * @var string|null
27
     */
28
    private $label;
29
30
    /**
31
     * @var string
32
     */
33
    private $message;
34
35
    /**
36
     * @var string
37
     */
38
    private $request;
39
40
    /**
41
     * @var int
42
     */
43
    private $rule;
44
45
    /**
46
     * Uri constructor.
47
     * @param AddressInterface|null $address
48
     * @param int $convention
49
     */
50 8
    public function __construct(AddressInterface $address = null, int $convention = self::BIP0021)
51
    {
52 8
        if ($convention === self::BIP0021) {
53 7
            if ($address === null) {
54 7
                throw new \InvalidArgumentException('Cannot provide a null address with bip0021');
55
            }
56 1
        } else if ($convention !== self::BIP0072) {
57
            throw new \InvalidArgumentException("Invalid convention for bitcoin uri");
58
        }
59
60 7
        $this->address = $address;
61 7
        $this->rule = $convention;
62 7
    }
63
64
    /**
65
     * @param float $value
66
     * @return $this
67
     */
68 1
    public function setAmountBtc(float $value)
69
    {
70 1
        $this->amount = $value;
71 1
        return $this;
72
    }
73
74
    /**
75
     * @param Amount $amount
76
     * @param int $value
77
     * @return $this
78
     */
79 1
    public function setAmount(Amount $amount, int $value)
80
    {
81 1
        $this->amount = $amount->toBtc($value);
0 ignored issues
show
Documentation Bug introduced by
It seems like $amount->toBtc($value) of type string is incompatible with the declared type double|null of property $amount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82 1
        return $this;
83
    }
84
85
    /**
86
     * @param string $label
87
     * @return $this
88
     */
89 1
    public function setLabel(string $label)
90
    {
91 1
        $this->label = $label;
92 1
        return $this;
93
    }
94
95
    /**
96
     * @param string $message
97
     * @return $this
98
     */
99 1
    public function setMessage(string $message)
100
    {
101 1
        $this->message = $message;
102 1
        return $this;
103
    }
104
105
    /**
106
     * @param string $url
107
     * @return $this
108
     */
109 2
    public function setRequestUrl(string $url)
110
    {
111 2
        $this->request = $url;
112 2
        return $this;
113
    }
114
115
    /**
116
     * @param NetworkInterface|null $network
117
     * @return string
118
     */
119 7
    public function uri(NetworkInterface $network = null): string
120
    {
121 7
        if ($this->rule === self::BIP0072) {
122 1
            $address = $this->address === null ? '' : $this->address->getAddress($network);
123
        } else {
124 6
            $address = $this->address->getAddress($network);
125
        }
126
127 7
        $url = 'bitcoin:' . $address;
128
129 7
        $params = [];
130 7
        if (null !== $this->amount) {
131 2
            $params['amount'] = $this->amount;
132
        }
133
134 7
        if (null !== $this->label) {
135 1
            $params['label'] = $this->label;
136
        }
137
138 7
        if (null !== $this->message) {
139 1
            $params['message'] = $this->message;
140
        }
141
142 7
        if (null !== $this->request) {
143 2
            $params['r'] = $this->request;
144
        }
145
146 7
        if (count($params) > 0) {
147 6
            $url .= '?' . http_build_query($params);
148
        }
149
150 7
        return $url;
151
    }
152
}
153