Ipv4   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBuffer() 0 4 1
A getHost() 0 3 1
A __construct() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Ip;
6
7
use BitWasp\Buffertools\Buffer;
8
use BitWasp\Buffertools\BufferInterface;
9
10
class Ipv4 implements IpInterface
11
{
12
    const MAGIC = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff";
13
14
    /**
15
     * @var string
16
     */
17
    private $ip;
18
19
    /**
20
     * Ipv4 constructor.
21 86
     * @param string $ip
22
     */
23 86
    public function __construct(string $ip)
24 3
    {
25
        if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
26
            throw new \InvalidArgumentException('IPv4: a valid IPv4 address is required');
27 83
        }
28 83
29
        $this->ip = $ip;
30
    }
31
32
    /**
33 30
     * @return string
34
     */
35 30
    public function getHost(): string
36
    {
37
        return $this->ip;
38
    }
39
40
    /**
41 36
     * @return BufferInterface
42
     */
43 36
    public function getBuffer(): BufferInterface
44
    {
45
        return new Buffer(
46
            sprintf("%s%s", self::MAGIC, pack("N", ip2long($this->ip)))
47
        );
48
    }
49
}
50