Passed
Push — master ( 3dc4f8...535c32 )
by thomas
15:05
created

Ping::getBuffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Messages;
6
7
use BitWasp\Bitcoin\Crypto\Random\Random;
8
use BitWasp\Bitcoin\Networking\Message;
9
use BitWasp\Bitcoin\Networking\NetworkSerializable;
10
use BitWasp\Bitcoin\Networking\Serializer\Message\PingSerializer;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class Ping extends NetworkSerializable
14
{
15
    /**
16
     * @var int
17
     */
18
    private $nonce;
19
20
    /**
21 9
     * Ping constructor.
22
     * @param int $nonce
23 9
     */
24 9
    public function __construct(int $nonce)
25
    {
26
        $this->nonce = $nonce;
27
    }
28
29 9
    /**
30
     * @param Random $random
31 9
     * @return Ping
32
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
33
     */
34
    public static function generate(Random $random): Ping
35
    {
36
        $nonce = (int) $random->bytes(8)->getInt();
37 18
        return new Ping($nonce);
38
    }
39 18
40
    /**
41
     * @return string
42
     */
43
    public function getNetworkCommand(): string
44
    {
45 9
        return Message::PING;
46
    }
47 9
48
    /**
49
     * @return int
50
     */
51
    public function getNonce()
52
    {
53
        return $this->nonce;
54
    }
55
56
    /**
57
     * @return BufferInterface
58
     */
59
    public function getBuffer(): BufferInterface
60
    {
61
        return (new PingSerializer())->serialize($this);
62
    }
63
}
64