Completed
Pull Request — 0.0.35 (#659)
by thomas
30:11 queued 21:19
created

NetworkConfig::checkForOverwriting()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 8
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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 14
    public function __construct(NetworkInterface $network, array $prefixConfigList)
31
    {
32 14
        foreach ($prefixConfigList as $config) {
33 8
            if (!($config instanceof ScriptPrefix)) {
34 1
                throw new \InvalidArgumentException("expecting array of NetworkPrefixConfig");
35
            }
36 8
            $this->setupConfig($config);
37
        }
38
39 10
        $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 10
    }
41
42
    /**
43
     * @param ScriptPrefix $config
44
     */
45 8
    private function setupConfig(ScriptPrefix $config)
46
    {
47 8
        $this->checkForOverwriting($config);
48
49 8
        $this->scriptPrefixMap[$config->getPrivatePrefix()] = $config;
50 8
        $this->scriptPrefixMap[$config->getPublicPrefix()] = $config;
51 8
        $this->scriptTypeMap[$config->getScriptDataFactory()->getScriptType()] = $config;
52 8
    }
53
54
    /**
55
     * @param ScriptPrefix $config
56
     */
57 8
    private function checkForOverwriting(ScriptPrefix $config)
58
    {
59 8
        if (array_key_exists($config->getPublicPrefix(), $this->scriptPrefixMap)) {
60 1
            $this->rejectConflictPrefix($config, $config->getPublicPrefix());
61
        }
62
63 8
        if (array_key_exists($config->getPrivatePrefix(), $this->scriptPrefixMap)) {
64 1
            $this->rejectConflictPrefix($config, $config->getPrivatePrefix());
65
        }
66
67 8
        if (array_key_exists($config->getScriptDataFactory()->getScriptType(), $this->scriptTypeMap)) {
68 1
            $this->rejectConflictScriptType($config);
69
        }
70 8
    }
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 6
    public function getNetwork()
102
    {
103 6
        return $this->network;
104
    }
105
106
    /**
107
     * @param string $prefix
108
     * @return ScriptPrefix
109
     */
110 2
    public function getConfigForPrefix($prefix)
111
    {
112 2
        if (!array_key_exists($prefix, $this->scriptPrefixMap)) {
113 1
            throw new \InvalidArgumentException("Prefix not configured for network");
114
        }
115
116 1
        return $this->scriptPrefixMap[$prefix];
117
    }
118
119
    /**
120
     * @param string $scriptType
121
     * @return ScriptPrefix
122
     */
123 4
    public function getConfigForScriptType($scriptType)
124
    {
125 4
        if (!array_key_exists($scriptType, $this->scriptTypeMap)) {
126 1
            throw new \InvalidArgumentException("Script type not configured for network");
127
        }
128
129 3
        return $this->scriptTypeMap[$scriptType];
130
    }
131
}
132