Completed
Pull Request — master (#353)
by Ruben de
71:45
created

EcSerializer::getImplPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\CompactSignatureSerializerInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface;
11
12
class EcSerializer
13
{
14
    const PATH_PHPECC = 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\\';
15
    const PATH_SECP256K1 = 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\\';
16
17
    /**
18
     * @var string[]
19
     */
20
    private static $serializerInterface = [
21
        PrivateKeySerializerInterface::class,
22
        PublicKeySerializerInterface::class,
23
        CompactSignatureSerializerInterface::class,
24
        DerSignatureSerializerInterface::class,
25
    ];
26
27
    /**
28
     * @var string[]
29
     */
30
    private static $serializerImpl = [
31
        'Serializer\Key\PrivateKeySerializer',
32
        'Serializer\Key\PublicKeySerializer',
33
        'Serializer\Signature\CompactSignatureSerializer',
34
        'Serializer\Signature\DerSignatureSerializer'
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $map = [];
41
42
    /**
43
     * @var bool
44
     */
45
    private static $useCache = true;
46
47
    /**
48
     * @var array
49
     */
50
    private static $cache = [];
51
52
    /**
53
     * @param $interface
54
     * @return mixed
55 549
     */
56
    public static function getImplRelPath($interface)
57 549
    {
58
        if (0 === count(self::$map)) {
59
            if (!in_array($interface, self::$serializerInterface, true)) {
60
                throw new \InvalidArgumentException('Interface not known');
61
            }
62
63
            $cInterface = count(self::$serializerInterface);
64
            if ($cInterface !== count(self::$serializerImpl)) {
65
                throw new \InvalidArgumentException('Invalid serializer interface map');
66
            }
67
68
            for ($i = 0; $i < $cInterface; $i++) {
69
                /** @var string $iface */
70
                $iface = self::$serializerInterface[$i];
71
                $ipath = self::$serializerImpl[$i];
72
                self::$map[$iface] = $ipath;
73
            }
74
        }
75 549
76
        return self::$map[$interface];
77
    }
78
79
    /**
80
     * @return array
81 549
     */
82
    public static function getImplPaths()
83
    {
84 549
        return [
85
            'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter' => 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\\',
86 341
            'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter' => 'BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\\'
87
        ];
88
    }
89
90
    /**
91
     * @param EcAdapterInterface $adapter
92
     * @return mixed
93 549
     */
94
    public static function getAdapterImplPath(EcAdapterInterface $adapter)
95 549
    {
96 549
        $paths = static::getImplPaths();
97 549
        $class = get_class($adapter);
98
        if (!isset($paths[$class])) {
99
            throw new \RuntimeException('Unknown EcAdapter');
100
        }
101 549
102
        return $paths[$class];
103
    }
104
105
    /**
106
     * @param $interface
107
     * @param bool|true $useCache
108
     * @param EcAdapterInterface $adapter
109
     * @return mixed
110 549
     */
111
    public static function getSerializer($interface, $useCache = true, EcAdapterInterface $adapter = null)
112 549
    {
113
        $adapter = $adapter ?: Bitcoin::getEcAdapter();
114
115
        if (isset(self::$cache[$interface])) {
116 549
            return self::$cache[$interface];
117 549
        }
118
119 549
        $classPath = self::getAdapterImplPath($adapter) . self::getImplRelPath($interface);
120
        $class = new $classPath($adapter);
121
122
        if ($useCache && self::$useCache) {
123 549
            self::$cache[$interface] = $class;
124
        }
125
126
        return $class;
127
    }
128
129
    /**
130
     * Disables caching of serializers
131
     */
132
    public static function disableCache()
133
    {
134
        self::$useCache = false;
135
    }
136
}
137