Onion::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Ip;
6
7
use Base32\Base32;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class Onion implements IpInterface
12
{
13
    const MAGIC = "\xFD\x87\xD8\x7E\xEB\x43";
14
15
    /**
16
     * @var string
17
     */
18
    private $host;
19
20
    /**
21
     * @var string
22
     */
23
    private $identifier;
24
25
    /**
26
     * Onion constructor.
27 15
     * @param string $onionHost
28
     */
29 15
    public function __construct(string $onionHost)
30 15
    {
31 3
        $array = explode(".", $onionHost);
32
        if (count($array) !== 2) {
33
            throw new \InvalidArgumentException('Malformed onion address');
34 12
        }
35 12
36 3
        list ($ident, $onion) = $array;
37
        if ($onion !== 'onion') {
38
            throw new \InvalidArgumentException('Malformed onion address');
39 9
        }
40 9
41 3
        $decoded = Base32::decode($ident);
42
        if (strlen($decoded) !== 10) {
43
            throw new \InvalidArgumentException('Malformed onion address');
44 6
        }
45 6
46 6
        $this->identifier = $decoded;
47
        $this->host = $onionHost;
48
    }
49
50
    /**
51 6
     * @return string
52
     */
53 6
    public function getHost(): string
54
    {
55
        return $this->host;
56
    }
57
58
    /**
59 6
     * @return BufferInterface
60
     */
61 6
    public function getBuffer()
62
    {
63
        return new Buffer(self::MAGIC . $this->identifier);
64
    }
65
}
66