Completed
Pull Request — 0.0.35 (#659)
by thomas
26:42
created

KeyToScriptDataFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
convertKeyToScriptData() 0 1 ?
A convertKey() 0 9 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\KeyToScript\Factory;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
11
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData;
12
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactoryInterface;
13
14
abstract class KeyToScriptDataFactory implements ScriptDataFactoryInterface
15
{
16
    /**
17
     * @var PublicKeySerializerInterface
18
     */
19
    protected $pubKeySerializer;
20
21
    /**
22
     * KeyToP2PKScriptFactory constructor.
23
     * @param PublicKeySerializerInterface|null $pubKeySerializer
24
     */
25 2
    public function __construct(PublicKeySerializerInterface $pubKeySerializer = null)
26
    {
27 2
        if (null === $pubKeySerializer) {
28 2
            $pubKeySerializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true);
29
        }
30
31 2
        $this->pubKeySerializer = $pubKeySerializer;
32 2
    }
33
34
    /**
35
     * @param PublicKeyInterface $publicKey
36
     * @return ScriptAndSignData
37
     */
38
    abstract protected function convertKeyToScriptData(PublicKeyInterface $publicKey);
39
40
    /**
41
     * @param KeyInterface $key
42
     * @return ScriptAndSignData
43
     */
44 2
    public function convertKey(KeyInterface $key)
45
    {
46 2
        if ($key->isPrivate()) {
47
            /** @var PrivateKeyInterface $key */
48
            $key = $key->getPublicKey();
49
        }
50
        /** @var PublicKeyInterface $key */
51 2
        return $this->convertKeyToScriptData($key);
52
    }
53
}
54