Completed
Pull Request — 0.0.35 (#660)
by thomas
29:47 queued 20:44
created

PrefixRegistry::__construct()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 11
nc 6
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 8
rs 7.7777
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 41
    public function __construct(array $registry)
17
    {
18 41
        foreach ($registry as $scriptType => $prefixes) {
19 40
            if (!is_string($scriptType)) {
20 1
                throw new \InvalidArgumentException("Expecting script type as key");
21
            }
22 39
            if (count($prefixes) !== 2) {
23 1
                throw new \InvalidArgumentException("Expecting two BIP32 prefixes");
24
            }
25
            // private, public
26 38
            if (strlen($prefixes[0]) !== 8 || !ctype_xdigit($prefixes[0])) {
27 1
                throw new \InvalidArgumentException("Invalid private prefix");
28
            }
29 37
            if (strlen($prefixes[1]) !== 8 || !ctype_xdigit($prefixes[1])) {
30 37
                throw new \InvalidArgumentException("Invalid public prefix");
31
            }
32
        }
33 37
        $this->registry = $registry;
34 37
    }
35
36
    /**
37
     * @param string $scriptType
38
     * @return array
39
     */
40 37
    public function getPrefixes($scriptType)
41
    {
42 37
        if (!array_key_exists($scriptType, $this->registry)) {
43 2
            throw new \InvalidArgumentException("Unknown script type");
44
        }
45 35
        return $this->registry[$scriptType];
46
    }
47
}
48