Ipv4::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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