Completed
Pull Request — 0.0.35 (#659)
by thomas
26:42
created

GlobalHdKeyPrefixConfig::__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\Serializer\Key\ScriptedHierarchicalKey;
4
5
use BitWasp\Bitcoin\Network\NetworkInterface;
6
7
class GlobalHdKeyPrefixConfig
8
{
9
    /**
10
     * @var NetworkHdKeyPrefixConfig[]
11
     */
12
    private $networkConfigs = [];
13
14
    /**
15
     * ScriptPrefixConfig constructor.
16
     * @param NetworkHdKeyPrefixConfig[] $config
17
     */
18 1
    public function __construct(array $config)
19
    {
20 1
        foreach ($config as $networkPrefixConfig) {
21 1
            $networkClass = get_class($networkPrefixConfig->getNetwork());
22 1
            if (!array_key_exists($networkClass, $this->networkConfigs)) {
23 1
                $this->networkConfigs[$networkClass] = [];
24
            }
25
26 1
            $this->networkConfigs[$networkClass] = $networkPrefixConfig;
27
        }
28 1
    }
29
30
    /**
31
     * @param NetworkInterface $network
32
     * @return NetworkHdKeyPrefixConfig
33
     */
34 1
    public function getNetworkHdPrefixConfig(NetworkInterface $network)
35
    {
36 1
        $class = get_class($network);
37 1
        if (!array_key_exists($class, $this->networkConfigs)) {
38
            throw new \InvalidArgumentException("Network not registered with GlobalHdPrefixConfig");
39
        }
40
41 1
        return $this->networkConfigs[$class];
42
    }
43
}
44