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

GlobalPrefixConfig::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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