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

Onion   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 52
wmc 6
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
A getHost() 0 4 1
A getBuffer() 0 4 1
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