Completed
Pull Request — master (#591)
by thomas
30:19 queued 12s
created

Uri::setRequestUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin;
4
5
use BitWasp\Bitcoin\Address\AddressInterface;
6
use BitWasp\Bitcoin\Network\NetworkInterface;
7
8
class Uri
9
{
10
    const BIP0021 = 0;
11
    const BIP0072 = 1;
12
13
    /**
14
     * @var AddressInterface
15
     */
16
    private $address;
17
18
    /**
19
     * @var null|int
20
     */
21
    private $amount;
22
23
    /**
24
     * @var
25
     */
26
    private $label;
27
28
    /**
29
     * @var string
30
     */
31
    private $message;
32
33
    /**
34
     * @var string
35
     */
36
    private $request;
37
38
    /**
39
     * @var int
40
     */
41
    private $rule;
42
43
    /**
44
     * Uri constructor.
45
     * @param AddressInterface|null $address
46
     * @param int $convention
47
     */
48 8
    public function __construct(AddressInterface $address = null, int $convention = self::BIP0021)
49
    {
50 8
        if ($convention === self::BIP0021) {
51 7
            if ($address === null) {
52 7
                throw new \InvalidArgumentException('Cannot provide a null address with bip0021');
53
            }
54 1
        } else if ($convention !== self::BIP0072) {
55
            throw new \InvalidArgumentException("Invalid convention for bitcoin uri");
56
        }
57
58 7
        $this->address = $address;
59 7
        $this->rule = $convention;
60 7
    }
61
62
    /**
63
     * @param float $value
64
     * @return $this
65
     */
66 1
    public function setAmountBtc(float $value)
67
    {
68 1
        $this->amount = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type double is incompatible with the declared type null|integer 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...
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param Amount $amount
74
     * @param int $value
75
     * @return $this
76
     */
77 1
    public function setAmount(Amount $amount, int $value)
78
    {
79 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 null|integer 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...
80 1
        return $this;
81
    }
82
83
    /**
84
     * @param string $label
85
     * @return $this
86
     */
87 1
    public function setLabel(string $label)
88
    {
89 1
        $this->label = $label;
90 1
        return $this;
91
    }
92
93
    /**
94
     * @param string $message
95
     * @return $this
96
     */
97 1
    public function setMessage(string $message)
98
    {
99 1
        $this->message = $message;
100 1
        return $this;
101
    }
102
103
    /**
104
     * @param string $url
105
     * @return $this
106
     */
107 2
    public function setRequestUrl(string $url)
108
    {
109 2
        $this->request = $url;
110 2
        return $this;
111
    }
112
113
    /**
114
     * @param NetworkInterface|null $network
115
     * @return string
116
     */
117 7
    public function uri(NetworkInterface $network = null): string
118
    {
119 7
        if ($this->rule === self::BIP0072) {
120 1
            $address = $this->address === null ? '' : $this->address->getAddress($network);
121
        } else {
122 6
            $address = $this->address->getAddress($network);
123
        }
124
125 7
        $url = 'bitcoin:' . $address;
126
127 7
        $params = [];
128 7
        if (null !== $this->amount) {
129 2
            $params['amount'] = $this->amount;
130
        }
131
132 7
        if (null !== $this->label) {
133 1
            $params['label'] = $this->label;
134
        }
135
136 7
        if (null !== $this->message) {
137 1
            $params['message'] = $this->message;
138
        }
139
140 7
        if (null !== $this->request) {
141 2
            $params['r'] = $this->request;
142
        }
143
144 7
        if (count($params) > 0) {
145 6
            $url .= '?' . http_build_query($params);
146
        }
147
148 7
        return $url;
149
    }
150
}
151