Completed
Push — master ( abcf31...a8a46d )
by thomas
28:08 queued 25:46
created

KeyToScriptHelper::getP2pkhFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\KeyToScript;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
10
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shP2wshScriptDecorator;
11
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shScriptDecorator;
12
use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2wshScriptDecorator;
13
use BitWasp\Bitcoin\Key\KeyToScript\Factory\KeyToScriptDataFactory;
14
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkhScriptDataFactory;
15
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2wpkhScriptDataFactory;
16
use BitWasp\Bitcoin\Script\ScriptType;
17
18
class KeyToScriptHelper
19
{
20
21
    /**
22
     * @var mixed
23
     */
24
    private $cache = [];
25
26
    /**
27
     * @var PublicKeySerializerInterface
28
     */
29
    private $pubKeySer;
30
31
    /**
32
     * Slip132PrefixRegistry constructor.
33
     * @param EcAdapterInterface $ecAdapter
34
     */
35 22
    public function __construct(EcAdapterInterface $ecAdapter)
36
    {
37 22
        $this->pubKeySer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter);
38 22
    }
39
40
    /**
41
     * @param array ...$scriptPaths
42
     * @return string
43
     */
44 10
    private function makeScriptKey(... $scriptPaths)
45
    {
46 10
        return implode("|", $scriptPaths);
47
    }
48
49
    /**
50
     * @return P2pkhScriptDataFactory
51
     */
52 12
    public function getP2pkhFactory()
53
    {
54 12
        $key = ScriptType::P2PKH;
55 12
        if (!array_key_exists($key, $this->cache)) {
56 12
            $this->cache[$key] = new P2pkhScriptDataFactory($this->pubKeySer);
57
        }
58 12
        return $this->cache[$key];
59
    }
60
61
    /**
62
     * @return P2wpkhScriptDataFactory
63
     */
64 10
    public function getP2wpkhFactory()
65
    {
66 10
        $key = ScriptType::P2WKH;
67 10
        if (!array_key_exists($key, $this->cache)) {
68 10
            $this->cache[$key] = new P2wpkhScriptDataFactory($this->pubKeySer);
69
        }
70 10
        return $this->cache[$key];
71
    }
72
73
    /**
74
     * @param KeyToScriptDataFactory $scriptFactory
75
     * @return P2shScriptDecorator
76
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
77
     */
78 6
    public function getP2shFactory(KeyToScriptDataFactory $scriptFactory)
79
    {
80 6
        $key = $this->makeScriptKey(ScriptType::P2SH, $scriptFactory->getScriptType());
81 6
        if (!array_key_exists($key, $this->cache)) {
82 6
            $this->cache[$key] = new P2shScriptDecorator($scriptFactory);
83
        }
84 6
        return $this->cache[$key];
85
    }
86
87
    /**
88
     * @param KeyToScriptDataFactory $scriptFactory
89
     * @return P2wshScriptDecorator
90
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
91
     */
92 2
    public function getP2wshFactory(KeyToScriptDataFactory $scriptFactory)
93
    {
94 2
        $key = $this->makeScriptKey(ScriptType::P2WSH, $scriptFactory->getScriptType());
95 2
        if (!array_key_exists($key, $this->cache)) {
96 2
            $this->cache[$key] = new P2wshScriptDecorator($scriptFactory);
97
        }
98 2
        return $this->cache[$key];
99
    }
100
101
    /**
102
     * @param KeyToScriptDataFactory $scriptFactory
103
     * @return P2shP2wshScriptDecorator
104
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
105
     */
106 2
    public function getP2shP2wshFactory(KeyToScriptDataFactory $scriptFactory)
107
    {
108 2
        $key = $this->makeScriptKey(ScriptType::P2SH, ScriptType::P2WSH, $scriptFactory->getScriptType());
109 2
        if (!array_key_exists($key, $this->cache)) {
110 2
            $this->cache[$key] = new P2shP2wshScriptDecorator($scriptFactory);
111
        }
112 2
        return $this->cache[$key];
113
    }
114
}
115