Completed
Push — master ( ee79de...f41ec2 )
by thomas
05:10
created

Factory::sendheaders()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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\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 102
    public function __construct(NetworkInterface $network, Random $random)
39
    {
40 102
        $this->network = $network;
41 102
        $this->random = $random;
42 102
    }
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 3
    public function sendheaders()
90
    {
91 3
        return new SendHeaders();
92
    }
93
94
    /**
95
     * @param NetworkAddressTimestamp[] $addrs
96
     * @return Addr
97
     */
98 3
    public function addr(array $addrs)
99
    {
100 3
        return new Addr($addrs);
101
    }
102
103
    /**
104
     * @param Inventory[] $vectors
105
     * @return Inv
106
     */
107 3
    public function inv(array $vectors)
108
    {
109 3
        return new Inv($vectors);
110
    }
111
112
    /**
113
     * @param Inventory[] $vectors
114
     * @return GetData
115
     */
116 3
    public function getdata(array $vectors)
117
    {
118 3
        return new GetData($vectors);
119
    }
120
121
    /**
122
     * @param Inventory[] $vectors
123
     * @return NotFound
124
     */
125 3
    public function notfound(array $vectors)
126
    {
127 3
        return new NotFound($vectors);
128
    }
129
130
    /**
131
     * @param $version
132
     * @param BlockLocator $blockLocator
133
     * @return GetBlocks
134
     */
135 3
    public function getblocks($version, BlockLocator $blockLocator)
136
    {
137 3
        return new GetBlocks($version, $blockLocator);
138
    }
139
140
    /**
141
     * @param $version
142
     * @param BlockLocator $blockLocator
143
     * @return GetHeaders
144
     */
145 3
    public function getheaders($version, BlockLocator $blockLocator)
146
    {
147 3
        return new GetHeaders($version, $blockLocator);
148
    }
149
150
    /**
151
     * @param TransactionInterface $tx
152
     * @return Tx
153
     */
154 3
    public function tx(TransactionInterface $tx)
155
    {
156 3
        return new Tx($tx);
157
    }
158
159
    /**
160
     * @param BlockInterface $block
161
     * @return Block
162
     */
163 3
    public function block(BlockInterface $block)
164
    {
165 3
        return new Block($block);
166
    }
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 3
    public function mempool()
189
    {
190 3
        return new MemPool();
191
    }
192
193
    /**
194
     * @param Buffer $data
195
     * @return FilterAdd
196
     */
197 3
    public function filteradd(Buffer $data)
198
    {
199 3
        return new FilterAdd($data);
200
    }
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 3
    public function filterclear()
215
    {
216 3
        return new FilterClear();
217
    }
218
219
    /**
220
     * @param FilteredBlock $filtered
221
     * @return MerkleBlock
222
     */
223 3
    public function merkleblock(FilteredBlock $filtered)
224
    {
225 3
        return new MerkleBlock($filtered);
226
    }
227
    /**
228
     * @return Ping
229
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
230
     */
231 6
    public function ping()
232
    {
233 6
        return new Ping($this->random->bytes(8)->getInt());
234
    }
235
236
    /**
237
     * @param Ping $ping
238
     * @return Pong
239
     */
240 3
    public function pong(Ping $ping)
241
    {
242 3
        return new Pong($ping->getNonce());
243
    }
244
245
    /**
246
     * @param Buffer $message
247
     * @param int $code
248
     * @param Buffer $reason
249
     * @param Buffer|null $data
250
     * @return Reject
251
     */
252 6
    public function reject(
253
        Buffer $message,
254
        $code,
255
        Buffer $reason,
256
        Buffer $data = null
257
    ) {
258 6
        if (null === $data) {
259
            $data = new Buffer();
260
        }
261
262 6
        return new Reject(
263 6
            $message,
264 6
            $code,
265 6
            $reason,
266
            $data
267 6
        );
268
    }
269
270
    /**
271
     * @param AlertDetail $detail
272
     * @param SignatureInterface $sig
273
     * @return Alert
274
     */
275 3
    public function alert(AlertDetail $detail, SignatureInterface $sig)
276
    {
277 3
        return new Alert(
278 3
            $detail,
279
            $sig
280 3
        );
281
    }
282
283
    /**
284
     * @param Parser $parser
285
     * @return NetworkMessage
286
     */
287 36
    public function parse(Parser & $parser)
288
    {
289 36
        return (new NetworkMessageSerializer($this->network))->fromParser($parser);
290
    }
291
}
292