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