Completed
Push — master ( 00a863...d2dd08 )
by thomas
50:05 queued 12:04
created

Onion::getBuffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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