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

NetworkConfig   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A setupConfig() 0 8 1
A checkForOverwriting() 0 14 4
A rejectConflictPrefix() 0 10 2
A rejectConflictScriptType() 0 7 1
A getNetwork() 0 4 1
A getConfigForPrefix() 0 8 2
A getConfigForScriptType() 0 8 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic\HdPrefix;
4
5
use BitWasp\Bitcoin\Network\Network;
6
use BitWasp\Bitcoin\Network\NetworkInterface;
7
8
class NetworkConfig
9
{
10
    /**
11
     * @var Network
12
     */
13
    private $network;
14
15
    /**
16
     * @var ScriptPrefix[]
17
     */
18
    private $scriptPrefixMap = [];
19
20
    /**
21
     * @var ScriptPrefix[]
22
     */
23
    private $scriptTypeMap = [];
24
25
    /**
26
     * NetworkHdKeyPrefixConfig constructor.
27
     * @param NetworkInterface $network
28
     * @param ScriptPrefix[] $prefixConfigList
29
     */
30 15
    public function __construct(NetworkInterface $network, array $prefixConfigList)
31
    {
32 15
        foreach ($prefixConfigList as $config) {
33 9
            if (!($config instanceof ScriptPrefix)) {
34 1
                throw new \InvalidArgumentException("expecting array of NetworkPrefixConfig");
35
            }
36 9
            $this->setupConfig($config);
37
        }
38
39 11
        $this->network = $network;
0 ignored issues
show
Documentation Bug introduced by
$network is of type object<BitWasp\Bitcoin\Network\NetworkInterface>, but the property $network was declared to be of type object<BitWasp\Bitcoin\Network\Network>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
40 11
    }
41
42
    /**
43
     * @param ScriptPrefix $config
44
     */
45 9
    private function setupConfig(ScriptPrefix $config)
46
    {
47 9
        $this->checkForOverwriting($config);
48
49 9
        $this->scriptPrefixMap[$config->getPrivatePrefix()] = $config;
50 9
        $this->scriptPrefixMap[$config->getPublicPrefix()] = $config;
51 9
        $this->scriptTypeMap[$config->getScriptDataFactory()->getScriptType()] = $config;
52 9
    }
53
54
    /**
55
     * @param ScriptPrefix $config
56
     */
57 9
    private function checkForOverwriting(ScriptPrefix $config)
58
    {
59 9
        if (array_key_exists($config->getPublicPrefix(), $this->scriptPrefixMap)) {
60 1
            $this->rejectConflictPrefix($config, $config->getPublicPrefix());
61
        }
62
63 9
        if (array_key_exists($config->getPrivatePrefix(), $this->scriptPrefixMap)) {
64 1
            $this->rejectConflictPrefix($config, $config->getPrivatePrefix());
65
        }
66
67 9
        if (array_key_exists($config->getScriptDataFactory()->getScriptType(), $this->scriptTypeMap)) {
68 1
            $this->rejectConflictScriptType($config);
69
        }
70 9
    }
71
72
    /**
73
     * @param ScriptPrefix $config
74
     * @param string $prefix
75
     */
76 2
    private function rejectConflictPrefix(ScriptPrefix $config, $prefix)
77
    {
78 2
        $conflict = $this->scriptPrefixMap[$prefix];
79 2
        throw new \RuntimeException(sprintf(
80 2
            "A BIP32 prefix for %s conflicts with the %s BIP32 prefix of %s",
81 2
            $config->getScriptDataFactory()->getScriptType(),
82 2
            $prefix === $config->getPublicPrefix() ? "public" : "private",
83 2
            $conflict->getScriptDataFactory()->getScriptType()
84
        ));
85
    }
86
87
    /**
88
     * @param ScriptPrefix $config
89
     */
90 1
    private function rejectConflictScriptType(ScriptPrefix $config)
91
    {
92 1
        throw new \RuntimeException(sprintf(
93 1
            "The script type %s has a conflict",
94 1
            $config->getScriptDataFactory()->getScriptType()
95
        ));
96
    }
97
98
    /**
99
     * @return Network
100
     */
101 7
    public function getNetwork()
102
    {
103 7
        return $this->network;
104
    }
105
106
    /**
107
     * @param string $prefix
108
     * @return ScriptPrefix
109
     */
110 3
    public function getConfigForPrefix($prefix)
111
    {
112 3
        if (!array_key_exists($prefix, $this->scriptPrefixMap)) {
113 1
            throw new \InvalidArgumentException("Prefix not configured for network");
114
        }
115
116 2
        return $this->scriptPrefixMap[$prefix];
117
    }
118
119
    /**
120
     * @param string $scriptType
121
     * @return ScriptPrefix
122
     */
123 5
    public function getConfigForScriptType($scriptType)
124
    {
125 5
        if (!array_key_exists($scriptType, $this->scriptTypeMap)) {
126 1
            throw new \InvalidArgumentException("Script type not configured for network");
127
        }
128
129 4
        return $this->scriptTypeMap[$scriptType];
130
    }
131
}
132