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