Completed
Push — master ( abcf31...a8a46d )
by thomas
28:08 queued 25:46
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
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\Deterministic\Slip132;
6
7
class PrefixRegistry
8
{
9
    /**
10
     * @var array
11
     */
12
    private $registry = [];
13
14
    /**
15
     * PrefixRegistry constructor.
16
     * @param array $registry
17
     */
18 40
    public function __construct(array $registry)
19
    {
20 40
        foreach ($registry as $scriptType => $prefixes) {
21 39
            if (!is_string($scriptType)) {
22 1
                throw new \InvalidArgumentException("Expecting script type as key");
23
            }
24 38
            if (count($prefixes) !== 2) {
25 1
                throw new \InvalidArgumentException("Expecting two BIP32 prefixes");
26
            }
27
            // private, public
28 37
            if (strlen($prefixes[0]) !== 8 || !ctype_xdigit($prefixes[0])) {
29 1
                throw new \InvalidArgumentException("Invalid private prefix");
30
            }
31 36
            if (strlen($prefixes[1]) !== 8 || !ctype_xdigit($prefixes[1])) {
32 36
                throw new \InvalidArgumentException("Invalid public prefix");
33
            }
34
        }
35 36
        $this->registry = $registry;
36 36
    }
37
38
    /**
39
     * @param string $scriptType
40
     * @return array
41
     */
42 36
    public function getPrefixes($scriptType): array
43
    {
44 36
        if (!array_key_exists($scriptType, $this->registry)) {
45 2
            throw new \InvalidArgumentException("Unknown script type");
46
        }
47 34
        return $this->registry[$scriptType];
48
    }
49
}
50