Completed
Pull Request — master (#49)
by thomas
22:48 queued 06:30
created

Util   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 95.59%

Importance

Changes 0
Metric Value
dl 0
loc 125
ccs 65
cts 68
cp 0.9559
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 4 2
C parseApiNetwork() 0 31 7
C normalizeNetwork() 0 44 15
C arrayMapWithIndex() 0 23 8
1
<?php
2
3
namespace Blocktrail\SDK;
4
5
abstract class Util {
6 4
    public static function all(callable $fn, array $arr) {
7 4
        $allvalues = array_map($fn, $arr);
8 4
        return count(array_unique($allvalues)) === 1 && end($allvalues) === true;
9
    }
10
11
    /**
12
     * Given a $network string and $testnet bool, this function
13
     * will apply the old method (conditionally prepending 't'
14
     * to the network if ($testnet).. but also some new special
15
     * cases which ignore the testnet setting.
16
     *
17
     * @param string $network
18
     * @param bool $testnet
19
     * @return array
20
     */
21 117
    public static function parseApiNetwork($network, $testnet) {
22 117
        $network = strtoupper($network);
23
24
        // deal with testnet special cases
25 117
        if (strlen($network) === 4 && $network[0] === 'T') {
26 4
            $testnet = true;
27 4
            $network = substr($network, 1);
28
        }
29
30 117
        if (strlen($network) === 3) {
31
            // work out apiNetwork
32 117
            $apiNetwork = $network;
33 117
            if ($testnet) {
34 117
                $apiNetwork = "t{$network}";
35
            }
36 4
        } else if ($network === "RBTC") {
37
            // Regtest is magic
38 2
            $apiNetwork = "rBTC";
39 2
            $testnet = true;
40 2
        } else if ($network === "RBCH") {
41
            // Regtest is magic
42 1
            $apiNetwork = "rBCH";
43 1
            $testnet = true;
44
        } else {
45
            // Default to bitcoin if they make no sense.
46 1
            $apiNetwork = "BTC";
47 1
            $testnet = false;
48
        }
49
50 117
        return [$apiNetwork, $testnet];
51
    }
52
53
    /**
54
     * normalize network string
55
     *
56
     * @param $network
57
     * @param $testnet
58
     * @return array
59
     * @throws \Exception
60
     */
61 117
    public static function normalizeNetwork($network, $testnet) {
62 117
        switch (strtolower($network)) {
63 117
            case 'btc':
64 14
            case 'bitcoin':
65 117
                $network = 'bitcoin';
66 117
                break;
67
68 14
            case 'tbtc':
69 11
            case 'bitcoin-testnet':
70 3
                $network = 'bitcoin';
71 3
                $testnet = true;
72 3
                break;
73 11
            case 'bcc':
74 6
            case 'bch':
75 6
            case 'bitcoincash':
76 5
                $network = 'bitcoincash';
77 5
                break;
78
79 6
            case 'tbch':
80 6
            case 'tbcc':
81 3
            case 'bitcoincash-testnet':
82 3
                $network = 'bitcoincash';
83 3
                $testnet = true;
84 3
                break;
85
86 3
            case 'rbtc':
87 1
            case 'bitcoin-regtest':
88 2
                $network = 'bitcoin';
89 2
                $testnet = true;
90 2
                break;
91
92 1
            case 'rbch':
93
            case 'bitcoincash-regtest':
94 1
                $network = 'bitcoincash';
95 1
                $testnet = true;
96 1
                break;
97
98
            default:
99
                throw new \Exception("Unknown network [{$network}]");
100
            // this comment silences a phpcs error.
101
        }
102
103 117
        return [$network, $testnet];
104
    }
105
106 26
    public static function arrayMapWithIndex(callable $fn, $arr) {
107 26
        $result = [];
108 26
        $assoc = null;
109 26
        foreach ($arr as $idx => $value) {
110 26
            list($newidx, $newvalue) = $fn($idx, $value);
111
112 26
            if ($assoc === null) {
113 26
                $assoc = $newidx !== null;
114
            }
115
116 26
            if ($newidx === null && $assoc || $newidx !== null && !$assoc) {
117
                throw new \Exception("Mix of non assoc and assoc keys");
118
            }
119
120 26
            if ($assoc) {
121 26
                $result[$newidx] = $newvalue;
122
            } else {
123 26
                $result[] = $newvalue;
124
            }
125
        }
126
127 26
        return $result;
128
    }
129
}
130