Ping::getNetworkCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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 BufferInterface
17
     */
18
    private $nonce;
19
20
    /**
21 9
     * Ping constructor.
22
     * @param BufferInterface $nonce
23 9
     */
24 9
    public function __construct(BufferInterface $nonce)
25
    {
26
        if ($nonce->getSize() !== 8) {
27
            throw new \RuntimeException("Invalid nonce size");
28
        }
29 9
        $this->nonce = $nonce;
30
    }
31 9
32
    /**
33
     * @param Random $random
34
     * @return Ping
35
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
36
     */
37 18
    public static function generate(Random $random): Ping
38
    {
39 18
        return new Ping($random->bytes(8));
40
    }
41
42
    /**
43
     * @return string
44
     * @see https://en.bitcoin.it/wiki/Protocol_documentation#ping
45 9
     */
46
    public function getNetworkCommand(): string
47 9
    {
48
        return Message::PING;
49
    }
50
51
    /**
52
     * @return BufferInterface
53
     */
54
    public function getNonce(): BufferInterface
55
    {
56
        return $this->nonce;
57
    }
58
59
    /**
60
     * @return BufferInterface
61
     */
62
    public function getBuffer(): BufferInterface
63
    {
64
        return (new PingSerializer())->serialize($this);
65
    }
66
}
67