Completed
Pull Request — 0.0.35 (#659)
by thomas
25:58
created

GlobalPrefixConfig::getNetworkConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic\HdPrefix;
4
5
use BitWasp\Bitcoin\Network\NetworkInterface;
6
7
class GlobalPrefixConfig
8
{
9
    /**
10
     * @var NetworkConfig[]
11
     */
12
    private $networkConfigs = [];
13
14
    /**
15
     * ScriptPrefixConfig constructor.
16
     * @param NetworkConfig[] $config
17
     */
18 7
    public function __construct(array $config)
19
    {
20 7
        foreach ($config as $networkPrefixConfig) {
21 7
            if (!($networkPrefixConfig instanceof NetworkConfig)) {
22 1
                throw new \InvalidArgumentException("expecting array of NetworkPrefixConfig");
23
            }
24
25 6
            $networkClass = get_class($networkPrefixConfig->getNetwork());
26 6
            if (array_key_exists($networkClass, $this->networkConfigs)) {
27 1
                throw new \InvalidArgumentException("multiple configs for network");
28
            }
29
30 6
            $this->networkConfigs[$networkClass] = $networkPrefixConfig;
31
        }
32 5
    }
33
34
    /**
35
     * @param NetworkInterface $network
36
     * @return NetworkConfig
37
     */
38 5
    public function getNetworkConfig(NetworkInterface $network)
39
    {
40 5
        $class = get_class($network);
41 5
        if (!array_key_exists($class, $this->networkConfigs)) {
42 1
            throw new \InvalidArgumentException("Network not registered with GlobalHdPrefixConfig");
43
        }
44
45 4
        return $this->networkConfigs[$class];
46
    }
47
}
48