Completed
Push — master ( d7f7e8...4d00a0 )
by thomas
33:43
created

Factory::getSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
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
        $this->serializer = new NetworkMessageSerializer($this->network);
0 ignored issues
show
Bug introduced by
The property serializer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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 $this->serializer->fromParser($parser);
301
    }
302
303
    /**
304
     * @return NetworkMessageSerializer
305 12
     */
306
    public function getSerializer()
307 12
    {
308
        return $this->serializer;
309
    }
310
311
    /**
312
     * @return NetworkInterface
313
     */
314
    public function getNetwork()
315
    {
316
        return $this->network;
317
    }
318
}
319