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

NetworkPrefixConfig::setupConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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