Completed
Push — master ( f7f683...a725ae )
by thomas
34:30 queued 31:27
created

ConnectionParams::setTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Peer;
4
5
use BitWasp\Bitcoin\Networking\Messages\Factory as MsgFactory;
6
use BitWasp\Bitcoin\Networking\Messages\Version;
7
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
8
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class ConnectionParams
13
{
14
    protected $defaultUserAgent = 'bitcoin-php';
15
    protected $defaultProtocolVersion = '70000';
16
    protected $defaultTxRelay = false;
17
    protected $defaultBlockHeight = '0';
18
    protected $defaultLocalIp = '0.0.0.0';
19
    protected $defaultLocalPort = '0';
20
21
    /**
22
     * @var int
23
     */
24
    private $protocolVersion;
25
26
    /**
27
     * @var int
28
     */
29
    private $timestamp;
30
31
    /**
32
     * @var bool
33
     */
34
    private $txRelay;
35
36
    /**
37
     * @var callable
38
     */
39
    private $bestBlockHeightCallback;
40
41
    /**
42
     * @var int
43
     */
44
    private $bestBlockHeight;
45
46
    /**
47
     * @var string
48
     */
49
    private $localIp;
50
51
    /**
52
     * @var int
53
     */
54
    private $localPort;
55
56
    /**
57
     * @var BufferInterface
58
     */
59
    private $localServices;
60
61
    /**
62
     * @var string
63
     */
64
    private $userAgent;
65
66
    /**
67
     * @param bool $optRelay
68
     * @return $this
69
     */
70
    public function requestTxRelay($optRelay = true)
71
    {
72
        if (!is_bool($optRelay)) {
73
            throw new \InvalidArgumentException('Invalid txrelay setting, must be a boolean');
74
        }
75
76
        $this->txRelay = $optRelay;
77
        return $this;
78
    }
79
80
    /**
81
     * @param int $blockHeight
82
     * @return $this
83
     */
84
    public function setBestBlockHeight($blockHeight)
85
    {
86
        $this->bestBlockHeight = $blockHeight;
87
        return $this;
88
    }
89
90
    /**
91
     * @param callable $callable
92
     * @return $this
93
     */
94
    public function setBestBlockHeightCallback(callable $callable)
95
    {
96
        $this->bestBlockHeightCallback = $callable;
97
        return $this;
98
    }
99
100
    /**
101
     * @param int $version
102
     * @return $this
103
     */
104
    public function setProtocolVersion($version)
105
    {
106
        $this->protocolVersion = $version;
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $ip
112
     * @return $this
113
     */
114
    public function setLocalIp($ip)
115
    {
116
        $this->localIp = $ip;
117
        return $this;
118
    }
119
120
    /**
121
     * @param int $port
122
     * @return $this
123
     */
124
    public function setLocalPort($port)
125
    {
126
        $this->localPort = $port;
127
        return $this;
128
    }
129
130
    /**
131
     * @param BufferInterface $services
132
     * @return $this
133
     */
134
    public function setLocalServices(BufferInterface $services)
135
    {
136
        $this->localServices = $services;
137
        return $this;
138
    }
139
140
    /**
141
     * @param NetworkAddressInterface $networkAddress
142
     * @return $this
143
     */
144
    public function setLocalNetAddr(NetworkAddressInterface $networkAddress)
145
    {
146
        return $this
147
            ->setLocalIp($networkAddress->getIp())
148
            ->setLocalPort($networkAddress->getPort())
149
            ->setLocalServices($networkAddress->getServices());
150
    }
151
152
    /**
153
     * @param int $timestamp
154
     * @return $this
155
     */
156
    public function setTimestamp($timestamp)
157
    {
158
        $this->timestamp = $timestamp;
159
        return $this;
160
    }
161
162
    /**
163
     * @param $string
164
     * @return $this
165
     */
166
    public function setUserAgent($string)
167
    {
168
        if (!is_string($string)) {
169
            throw new \InvalidArgumentException('User agent must be a string');
170
        }
171
172
        $this->userAgent = new Buffer($string);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \BitWasp\Buffertools\Buffer($string) of type object<BitWasp\Buffertools\Buffer> is incompatible with the declared type string of property $userAgent.

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...
173
        return $this;
174
    }
175
176
    /**
177
     * @param MsgFactory $messageFactory
178
     * @param NetworkAddressInterface $remoteAddress
179
     * @return Version
180
     */
181 12
    public function produceVersion(MsgFactory $messageFactory, NetworkAddressInterface $remoteAddress)
182
    {
183 12
        $protocolVersion = is_null($this->protocolVersion) ? $this->defaultProtocolVersion : $this->protocolVersion;
184 12
        $localServices = is_null($this->localServices) ? new Buffer('', 8) : $this->localServices;
185 12
        $timestamp = is_null($this->timestamp) ? time() : $this->timestamp;
186 12
        $localAddr = new NetworkAddress(
187 12
            $localServices,
188 12
            is_null($this->localIp) ? $this->defaultLocalIp : $this->localIp,
189 12
            is_null($this->localPort) ? $this->defaultLocalPort : $this->localPort
190 12
        );
191
192 12
        $userAgent = new Buffer(is_null($this->userAgent) ? $this->defaultUserAgent : $this->userAgent);
193
194 12
        if (is_callable($this->bestBlockHeightCallback)) {
195
            $cb = $this->bestBlockHeightCallback;
196
            $bestHeight = $cb();
197 12
        } elseif (!is_null($this->bestBlockHeight)) {
198
            $bestHeight = $this->bestBlockHeight;
199
        } else {
200 12
            $bestHeight = $this->defaultBlockHeight;
201
        }
202
203 12
        $relay = is_null($this->txRelay) ? $this->defaultTxRelay : $this->txRelay;
204
205 12
        return $messageFactory->version($protocolVersion, $localServices, $timestamp, $remoteAddress, $localAddr, $userAgent, $bestHeight, $relay);
206
    }
207
}
208