Ping   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 9
dl 0
loc 52
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A generate() 0 3 1
A getBuffer() 0 3 1
A getNonce() 0 3 1
A getNetworkCommand() 0 3 1
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