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

GlobalPrefixConfig::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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