Completed
Pull Request — 0.0.35 (#660)
by thomas
22:14
created

PrefixRegistry::__construct()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.392

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 5
nop 1
dl 0
loc 15
ccs 8
cts 10
cp 0.8
crap 7.392
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic\Slip132;
4
5
class PrefixRegistry
6
{
7
    /**
8
     * @var array
9
     */
10
    private $registry = [];
11
12
    /**
13
     * PrefixRegistry constructor.
14
     * @param array $registry
15
     */
16 21
    public function __construct(array $registry)
17
    {
18 21
        foreach ($registry as $scriptType => $prefixes) {
19 21
            if (count($prefixes) !== 2) {
20
                throw new \InvalidArgumentException("Expecting two BIP32 prefixes");
21
            }
22 21
            if (strlen($prefixes[0]) !== 8 || !ctype_xdigit($prefixes[0])) {
23
                throw new \InvalidArgumentException("Invalid private prefix");
24
            }
25 21
            if (strlen($prefixes[1]) !== 8 || !ctype_xdigit($prefixes[1])) {
26 21
                throw new \InvalidArgumentException("Invalid public .prefix");
27
            }
28
        }
29 21
        $this->registry = $registry;
30 21
    }
31
32
    /**
33
     * @param string $scriptType
34
     * @return array
35
     */
36 21
    public function getPrefixes($scriptType)
37
    {
38 21
        if (!array_key_exists($scriptType, $this->registry)) {
39
            throw new \InvalidArgumentException("Unknown script type");
40
        }
41 21
        return $this->registry[$scriptType];
42
    }
43
}
44