Completed
Pull Request — master (#73)
by Stefan
38:41
created

Factory::getNetwork()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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