Completed
Pull Request — master (#662)
by thomas
38:24
created

GlobalPrefixConfig::getNetworkConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
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
    public function __construct(array $config)
21
    {
22
        foreach ($config as $networkPrefixConfig) {
23
            if (!($networkPrefixConfig instanceof NetworkConfig)) {
24
                throw new \InvalidArgumentException("expecting array of NetworkPrefixConfig");
25
            }
26
27
            $networkClass = get_class($networkPrefixConfig->getNetwork());
28
            if (array_key_exists($networkClass, $this->networkConfigs)) {
29
                throw new \InvalidArgumentException("multiple configs for network");
30
            }
31
32
            $this->networkConfigs[$networkClass] = $networkPrefixConfig;
33
        }
34
    }
35
36
    /**
37
     * @param NetworkInterface $network
38
     * @return NetworkConfig
39
     */
40
    public function getNetworkConfig(NetworkInterface $network): NetworkConfig
41
    {
42
        $class = get_class($network);
43
        if (!array_key_exists($class, $this->networkConfigs)) {
44
            throw new \InvalidArgumentException("Network not registered with GlobalHdPrefixConfig");
45
        }
46
47
        return $this->networkConfigs[$class];
48
    }
49
}
50