Config::getRpcDsn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoind\Config;
6
7
use BitWasp\Bitcoind\Exception\BitcoindException;
8
use BitWasp\Bitcoind\Exception\ServerException;
9
10
class Config
11
{
12
    private $options = [];
13
14 23
    public function __construct(array $options = [])
15
    {
16 23
        $this->withOptions($options);
17 22
    }
18
19 23
    private function withOptions(array $options)
20
    {
21 23
        foreach ($options as $key => $option) {
22 22
            if (!is_string($key)) {
23 22
                throw new BitcoindException("Invalid config key");
24
            }
25
        }
26
27 23
        if ((array_key_exists('regtest', $options) && (bool) $options['regtest'])  &&
28 23
            (array_key_exists('testnet', $options) && (bool) $options['testnet'])) {
29 1
            throw new ServerException("Configuration conflicts, cannot be regtest and testnet");
30
        }
31
32 22
        $this->options = $options;
33 22
    }
34
35 8
    public function has(string $option): bool
36
    {
37 8
        return array_key_exists($option, $this->options);
38
    }
39
40 8
    public function get(string $option, string $default = null)
41
    {
42 8
        if ($this->has($option)) {
43 7
            return $this->options[$option];
44
        }
45
46 4
        return $default;
47
    }
48
49 12
    public function all(): array
50
    {
51 12
        return $this->options;
52
    }
53
54 1
    public function isRpcServer(): bool
55
    {
56 1
        return $this->has('server') &&
57 1
            (bool) $this->get('server')
58
        ;
59
    }
60
61 3
    public function hasRpcCredential(): bool
62
    {
63 3
        return $this->has('rpcuser') &&
64 3
            $this->has('rpcpassword');
65
    }
66
67 4
    public function getDefaultRpcPort(): int
68
    {
69 4
        if ($this->isTestnet()) {
70 2
            return 18332;
71 4
        } else if ($this->isRegtest()) {
72 3
            return 18443;
73
        } else {
74 3
            return 8332;
75
        }
76
    }
77
78 3
    public function getRpcDsn(): string
79
    {
80 3
        $host = $this->get('rpcconnect', '127.0.0.1');
81 3
        $port = (int) $this->get('rpcport', (string) $this->getDefaultRpcPort());
82
83 3
        if (!$this->hasRpcCredential()) {
84 1
            throw new BitcoindException("Missing rpc credential fields");
85
        }
86
87 2
        $user = $this->get('rpcuser');
88 2
        $pass = $this->get('rpcpassword');
89
90 2
        return "http://{$user}:{$pass}@{$host}:{$port}";
91
    }
92
93 6
    public function isTestnet(): bool
94
    {
95 6
        return $this->has('testnet') && (bool) $this->get('testnet');
96
    }
97
98 6
    public function isRegtest(): bool
99
    {
100 6
        return $this->has('regtest') && (bool) $this->get('regtest');
101
    }
102
103 4
    public function getRelativeChainPath(): string
104
    {
105 4
        if ($this->isTestnet()) {
106 2
            return "testnet3/";
107 4
        } else if ($this->isRegtest()) {
108 4
            return "regtest/";
109
        }
110
111 2
        return "";
112
    }
113
114 4
    private function getPathInChainDir($path): string
115
    {
116 4
        return "{$this->getRelativeChainPath()}$path";
117
    }
118
119 4
    public function getRelativePidPath(): string
120
    {
121 4
        return $this->getPathInChainDir("bitcoind.pid");
122
    }
123
124
    public function getRelativeLogPath(): string
125
    {
126
        return $this->getPathInChainDir("debug.log");
127
    }
128
}
129