Completed
Push — master ( d2dd08...01649b )
by thomas
13:53 queued 11:41
created

Factory::version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.2
cc 1
eloc 19
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 51
            $version,
68 51
            $services,
69 51
            $timestamp,
70 51
            $addrRecv,
71 51
            $addrFrom,
72 51
            $this->random->bytes(8)->getInt(),
73 51
            $userAgent,
74 51
            $startHeight,
75
            $relay
76 51
        );
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 3
        }
271
272 9
        return new Reject(
273 9
            $message,
274 9
            $code,
275 9
            $reason,
276
            $data
277 9
        );
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 3
            $detail,
289
            $sig
290 3
        );
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