Completed
Push — master ( fc4613...2e6493 )
by thomas
30:51
created

Factory::getNetwork()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Messages;
4
5
use BitWasp\Bitcoin\Block\BlockInterface;
6
use BitWasp\Bitcoin\Block\FilteredBlock;
7
use BitWasp\Bitcoin\Bloom\BloomFilter;
8
use BitWasp\Bitcoin\Chain\BlockLocator;
9
use BitWasp\Bitcoin\Crypto\Random\Random;
10
use BitWasp\Bitcoin\Network\NetworkInterface;
11
use BitWasp\Bitcoin\Networking\NetworkMessage;
12
use BitWasp\Bitcoin\Networking\Structure\AlertDetail;
13
use BitWasp\Bitcoin\Networking\Structure\Inventory;
14
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
15
use BitWasp\Bitcoin\Networking\Serializer\NetworkMessageSerializer;
16
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp;
17
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
18
use BitWasp\Bitcoin\Transaction\TransactionInterface;
19
use BitWasp\Buffertools\Buffer;
20
use BitWasp\Buffertools\BufferInterface;
21
use BitWasp\Buffertools\Parser;
22
23
class Factory
24
{
25
    /**
26
     * @var NetworkInterface
27
     */
28
    private $network;
29
30
    /**
31
     * @var Random
32
     */
33
    private $random;
34
35
    /**
36
     * @param NetworkInterface $network
37
     * @param Random $random
38
     */
39 126
    public function __construct(NetworkInterface $network, Random $random)
40
    {
41 126
        $this->network = $network;
42 126
        $this->random = $random;
43 126
    }
44
45
    /**
46
     * @param int $version
47
     * @param int $services
48
     * @param int $timestamp
49
     * @param NetworkAddressInterface $addrRecv
50
     * @param NetworkAddressInterface $addrFrom
51
     * @param BufferInterface $userAgent
52
     * @param int $startHeight
53
     * @param bool $relay
54
     * @return Version
55
     */
56 51
    public function version(
57
        $version,
58
        $services,
59
        $timestamp,
60
        NetworkAddressInterface $addrRecv,
61
        NetworkAddressInterface $addrFrom,
62
        BufferInterface $userAgent,
63
        $startHeight,
64
        $relay
65
    ) {
66 51
        return new Version(
67 34
            $version,
68 34
            $services,
69 34
            $timestamp,
70 34
            $addrRecv,
71 34
            $addrFrom,
72 51
            $this->random->bytes(8)->getInt(),
73 34
            $userAgent,
74 34
            $startHeight,
75
            $relay
76 34
        );
77
    }
78
79
    /**
80
     * @return VerAck
81
     */
82 15
    public function verack()
83
    {
84 15
        return new VerAck();
85
    }
86
87
    /**
88
     * @return SendHeaders
89
     */
90 3
    public function sendheaders()
91
    {
92 3
        return new SendHeaders();
93
    }
94
95
    /**
96
     * @param NetworkAddressTimestamp[] $addrs
97
     * @return Addr
98
     */
99 3
    public function addr(array $addrs)
100
    {
101 3
        return new Addr($addrs);
102
    }
103
104
    /**
105
     * @param Inventory[] $vectors
106
     * @return Inv
107
     */
108 3
    public function inv(array $vectors)
109
    {
110 3
        return new Inv($vectors);
111
    }
112
113
    /**
114
     * @param Inventory[] $vectors
115
     * @return GetData
116
     */
117 3
    public function getdata(array $vectors)
118
    {
119 3
        return new GetData($vectors);
120
    }
121
122
    /**
123
     * @param Inventory[] $vectors
124
     * @return NotFound
125
     */
126 3
    public function notfound(array $vectors)
127
    {
128 3
        return new NotFound($vectors);
129
    }
130
131
    /**
132
     * @param $version
133
     * @param BlockLocator $blockLocator
134
     * @return GetBlocks
135
     */
136 3
    public function getblocks($version, BlockLocator $blockLocator)
137
    {
138 3
        return new GetBlocks($version, $blockLocator);
139
    }
140
141
    /**
142
     * @param $version
143
     * @param BlockLocator $blockLocator
144
     * @return GetHeaders
145
     */
146 3
    public function getheaders($version, BlockLocator $blockLocator)
147
    {
148 3
        return new GetHeaders($version, $blockLocator);
149
    }
150
151
    /**
152
     * @param TransactionInterface $tx
153
     * @return Tx
154
     */
155 3
    public function tx(TransactionInterface $tx)
156
    {
157 3
        return new Tx($tx);
158
    }
159
160
    /**
161
     * @param BlockInterface $block
162
     * @return Block
163
     */
164 3
    public function block(BlockInterface $block)
165
    {
166 3
        return new Block($block);
167
    }
168
169
    /**
170
     * @param \BitWasp\Bitcoin\Block\BlockHeaderInterface[] $headers
171
     * @return Headers
172
     */
173 3
    public function headers(array $headers)
174
    {
175 3
        return new Headers($headers);
176
    }
177
178
    /**
179
     * @return GetAddr
180
     */
181 3
    public function getaddr()
182
    {
183 3
        return new GetAddr();
184
    }
185
186
    /**
187
     * @return MemPool
188
     */
189 3
    public function mempool()
190
    {
191 3
        return new MemPool();
192
    }
193
194
    /**
195
     * @param int $feeRate
196
     * @return FeeFilter
197
     */
198 3
    public function feefilter($feeRate)
199
    {
200 3
        return new FeeFilter($feeRate);
201
    }
202
203
    /**
204
     * @param BufferInterface $data
205
     * @return FilterAdd
206
     */
207 3
    public function filteradd(BufferInterface $data)
208
    {
209 3
        return new FilterAdd($data);
210
    }
211
212
    /**
213
     * @param BloomFilter $filter
214
     * @return FilterLoad
215
     */
216 3
    public function filterload(BloomFilter $filter)
217
    {
218 3
        return new FilterLoad($filter);
219
    }
220
221
    /**
222
     * @return FilterClear
223
     */
224 3
    public function filterclear()
225
    {
226 3
        return new FilterClear();
227
    }
228
229
    /**
230
     * @param FilteredBlock $filtered
231
     * @return MerkleBlock
232
     */
233 3
    public function merkleblock(FilteredBlock $filtered)
234
    {
235 3
        return new MerkleBlock($filtered);
236
    }
237
    /**
238
     * @return Ping
239
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
240
     */
241 6
    public function ping()
242
    {
243 6
        return new Ping($this->random->bytes(8)->getInt());
244
    }
245
246
    /**
247
     * @param Ping $ping
248
     * @return Pong
249
     */
250 3
    public function pong(Ping $ping)
251
    {
252 3
        return new Pong($ping->getNonce());
253
    }
254
255
    /**
256
     * @param BufferInterface $message
257
     * @param int $code
258
     * @param BufferInterface $reason
259
     * @param BufferInterface|null $data
260
     * @return Reject
261
     */
262 9
    public function reject(
263
        BufferInterface $message,
264
        $code,
265
        BufferInterface $reason,
266
        BufferInterface $data = null
267
    ) {
268 9
        if (null === $data) {
269 3
            $data = new Buffer();
270 2
        }
271
272 9
        return new Reject(
273 6
            $message,
274 6
            $code,
275 6
            $reason,
276
            $data
277 6
        );
278
    }
279
280
    /**
281
     * @param AlertDetail $detail
282
     * @param SignatureInterface $sig
283
     * @return Alert
284
     */
285 3
    public function alert(AlertDetail $detail, SignatureInterface $sig)
286
    {
287 3
        return new Alert(
288 2
            $detail,
289
            $sig
290 2
        );
291
    }
292
293
    /**
294
     * @param Parser $parser
295
     * @return NetworkMessage
296
     */
297 36
    public function parse(Parser $parser)
298
    {
299 36
        return (new NetworkMessageSerializer($this->network))->fromParser($parser);
300
    }
301
302
    /**
303
     * @return NetworkInterface
304
     */
305 12
    public function getNetwork()
306
    {
307 12
        return $this->network;
308
    }
309
}
310