Completed
Push — master ( abcf31...a8a46d )
by thomas
28:08 queued 25:46
created

NetworkConfig   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A setupConfig() 0 8 1
A checkForOverwriting() 0 14 4
A rejectConflictPrefix() 0 10 2
A rejectConflictScriptType() 0 7 1
A getNetwork() 0 4 1
A getConfigForPrefix() 0 8 2
A getConfigForScriptType() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\Deterministic\HdPrefix;
6
7
use BitWasp\Bitcoin\Network\NetworkInterface;
8
9
class NetworkConfig
10
{
11
    /**
12
     * @var NetworkInterface
13
     */
14
    private $network;
15
16
    /**
17
     * @var ScriptPrefix[]
18
     */
19
    private $scriptPrefixMap = [];
20
21
    /**
22
     * @var ScriptPrefix[]
23
     */
24
    private $scriptTypeMap = [];
25
26
    /**
27
     * NetworkHdKeyPrefixConfig constructor.
28
     * @param NetworkInterface $network
29
     * @param ScriptPrefix[] $prefixConfigList
30
     */
31 20
    public function __construct(NetworkInterface $network, array $prefixConfigList)
32
    {
33 20
        foreach ($prefixConfigList as $config) {
34 14
            if (!($config instanceof ScriptPrefix)) {
35 1
                throw new \InvalidArgumentException("expecting array of NetworkPrefixConfig");
36
            }
37 14
            $this->setupConfig($config);
38
        }
39
40 16
        $this->network = $network;
41 16
    }
42
43
    /**
44
     * @param ScriptPrefix $config
45
     */
46 14
    private function setupConfig(ScriptPrefix $config)
47
    {
48 14
        $this->checkForOverwriting($config);
49
50 14
        $this->scriptPrefixMap[$config->getPrivatePrefix()] = $config;
51 14
        $this->scriptPrefixMap[$config->getPublicPrefix()] = $config;
52 14
        $this->scriptTypeMap[$config->getScriptDataFactory()->getScriptType()] = $config;
53 14
    }
54
55
    /**
56
     * @param ScriptPrefix $config
57
     */
58 14
    private function checkForOverwriting(ScriptPrefix $config)
59
    {
60 14
        if (array_key_exists($config->getPublicPrefix(), $this->scriptPrefixMap)) {
61 1
            $this->rejectConflictPrefix($config, $config->getPublicPrefix());
62
        }
63
64 14
        if (array_key_exists($config->getPrivatePrefix(), $this->scriptPrefixMap)) {
65 1
            $this->rejectConflictPrefix($config, $config->getPrivatePrefix());
66
        }
67
68 14
        if (array_key_exists($config->getScriptDataFactory()->getScriptType(), $this->scriptTypeMap)) {
69 1
            $this->rejectConflictScriptType($config);
70
        }
71 14
    }
72
73
    /**
74
     * @param ScriptPrefix $config
75
     * @param string $prefix
76
     */
77 2
    private function rejectConflictPrefix(ScriptPrefix $config, $prefix)
78
    {
79 2
        $conflict = $this->scriptPrefixMap[$prefix];
80 2
        throw new \RuntimeException(sprintf(
81 2
            "A BIP32 prefix for %s conflicts with the %s BIP32 prefix of %s",
82 2
            $config->getScriptDataFactory()->getScriptType(),
83 2
            $prefix === $config->getPublicPrefix() ? "public" : "private",
84 2
            $conflict->getScriptDataFactory()->getScriptType()
85
        ));
86
    }
87
88
    /**
89
     * @param ScriptPrefix $config
90
     */
91 1
    private function rejectConflictScriptType(ScriptPrefix $config)
92
    {
93 1
        throw new \RuntimeException(sprintf(
94 1
            "The script type %s has a conflict",
95 1
            $config->getScriptDataFactory()->getScriptType()
96
        ));
97
    }
98
99
    /**
100
     * @return NetworkInterface
101
     */
102 12
    public function getNetwork()
103
    {
104 12
        return $this->network;
105
    }
106
107
    /**
108
     * @param string $prefix
109
     * @return ScriptPrefix
110
     */
111 2
    public function getConfigForPrefix($prefix)
112
    {
113 2
        if (!array_key_exists($prefix, $this->scriptPrefixMap)) {
114 1
            throw new \InvalidArgumentException("Prefix not configured for network");
115
        }
116
117 1
        return $this->scriptPrefixMap[$prefix];
118
    }
119
120
    /**
121
     * @param string $scriptType
122
     * @return ScriptPrefix
123
     */
124 10
    public function getConfigForScriptType($scriptType)
125
    {
126 10
        if (!array_key_exists($scriptType, $this->scriptTypeMap)) {
127 1
            throw new \InvalidArgumentException("Script type not configured for network");
128
        }
129
130 9
        return $this->scriptTypeMap[$scriptType];
131
    }
132
}
133