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

NetworkHdKeyPrefixConfig   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
dl 0
loc 132
ccs 22
cts 44
cp 0.5
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

9 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 getPrefixList() 0 4 1
A getConfigForPrefix() 0 8 2
A getConfigForScriptType() 0 8 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Key\ScriptedHierarchicalKey;
4
5
use BitWasp\Bitcoin\Network\Network;
6
use BitWasp\Bitcoin\Network\NetworkInterface;
7
8
class NetworkHdKeyPrefixConfig
9
{
10
    /**
11
     * @var Network
12
     */
13
    private $network;
14
15
    /**
16
     * @var NetworkScriptPrefix[]
17
     */
18
    private $scriptPrefixMap = [];
19
20
    /**
21
     * @var NetworkScriptPrefix[]
22
     */
23
    private $scriptTypeMap = [];
24
25
    /**
26
     * NetworkHdKeyPrefixConfig constructor.
27
     * @param NetworkInterface $network
28
     * @param NetworkScriptPrefix[] $prefixConfigList
29
     */
30 1
    public function __construct(NetworkInterface $network, array $prefixConfigList)
31
    {
32 1
        foreach ($prefixConfigList as $config) {
33 1
            if (!($config instanceof NetworkScriptPrefix)) {
34
                throw new \InvalidArgumentException();
35
            }
36 1
            $this->setupConfig($config);
37
        }
38
39 1
        $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 1
    }
41
42
    /**
43
     * @param NetworkScriptPrefix $config
44
     */
45 1
    private function setupConfig(NetworkScriptPrefix $config)
46
    {
47 1
        $this->checkForOverwriting($config);
48
49 1
        $this->scriptPrefixMap[$config->getPrivatePrefix()] = $config;
50 1
        $this->scriptPrefixMap[$config->getPublicPrefix()] = $config;
51 1
        $this->scriptTypeMap[$config->getScriptDataFactory()->getScriptType()] = $config;
52 1
    }
53
54
    /**
55
     * @param NetworkScriptPrefix $config
56
     */
57 1
    private function checkForOverwriting(NetworkScriptPrefix $config)
58
    {
59 1
        if (array_key_exists($config->getPublicPrefix(), $this->scriptPrefixMap)) {
60
            $this->rejectConflictPrefix($config, $config->getPublicPrefix());
61
        }
62
63 1
        if (array_key_exists($config->getPrivatePrefix(), $this->scriptPrefixMap)) {
64
            $this->rejectConflictPrefix($config, $config->getPrivatePrefix());
65
        }
66
67 1
        if (array_key_exists($config->getScriptDataFactory()->getScriptType(), $this->scriptTypeMap)) {
68
            $this->rejectConflictScriptType($config);
69
        }
70 1
    }
71
72
    /**
73
     * @param NetworkScriptPrefix $config
74
     * @param string $prefix
75
     */
76
    private function rejectConflictPrefix(NetworkScriptPrefix $config, $prefix)
77
    {
78
        $conflict = $this->scriptPrefixMap[$prefix];
79
        throw new \RuntimeException(sprintf(
80
            "A BIP32 prefix for %s conflicts with the %s BIP32 prefix of %s",
81
            $config->getScriptDataFactory()->getScriptType(),
82
            $prefix === $config->getPublicPrefix() ? "public" : "private",
83
            $conflict->getScriptDataFactory()->getScriptType()
84
        ));
85
    }
86
87
    /**
88
     * @param NetworkScriptPrefix $config
89
     */
90
    private function rejectConflictScriptType(NetworkScriptPrefix $config)
91
    {
92
        throw new \RuntimeException(sprintf(
93
            "The script type %s conflicts with another",
94
            $config->getScriptDataFactory()->getScriptType()
95
        ));
96
    }
97
98
    /**
99
     * @return Network
100
     */
101 1
    public function getNetwork()
102
    {
103 1
        return $this->network;
104
    }
105
106
    /**
107
     * @return NetworkScriptPrefix[]
108
     */
109
    public function getPrefixList()
110
    {
111
        return $this->scriptPrefixMap;
112
    }
113
114
    /**
115
     * @param string $prefix
116
     * @return NetworkScriptPrefix
117
     */
118
    public function getConfigForPrefix($prefix)
119
    {
120
        if (!array_key_exists($prefix, $this->scriptPrefixMap)) {
121
            throw new \InvalidArgumentException("Prefix not configured for network");
122
        }
123
124
        return $this->scriptPrefixMap[$prefix];
125
    }
126
127
    /**
128
     * @param string $scriptType
129
     * @return NetworkScriptPrefix
130
     */
131 1
    public function getConfigForScriptType($scriptType)
132
    {
133 1
        if (!array_key_exists($scriptType, $this->scriptTypeMap)) {
134
            throw new \InvalidArgumentException("Script type not configured for network");
135
        }
136
137 1
        return $this->scriptTypeMap[$scriptType];
138
    }
139
}
140