Completed
Pull Request — master (#591)
by thomas
19:03
created

PublicKeyFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromHex() 0 4 1
A fromBuffer() 0 6 1
A validateHex() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class PublicKeyFactory
15
{
16
    /**
17
     * @param string $hex
18
     * @param EcAdapterInterface|null $ecAdapter
19
     * @return PublicKeyInterface
20
     * @throws \Exception
21
     */
22 86
    public static function fromHex(string $hex, EcAdapterInterface $ecAdapter = null): PublicKeyInterface
23
    {
24 86
        return self::fromBuffer(Buffer::hex($hex), $ecAdapter);
25
    }
26
27
    /**
28
     * @param BufferInterface $buffer
29
     * @param EcAdapterInterface|null $ecAdapter
30
     * @return PublicKeyInterface
31
     */
32 108
    public static function fromBuffer(BufferInterface $buffer, EcAdapterInterface $ecAdapter = null): PublicKeyInterface
33
    {
34 108
        return EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter)
35 108
            ->parse($buffer)
36
        ;
37
    }
38
39
    /**
40
     * @param BufferInterface|string $hex
41
     * @param EcAdapterInterface|null $ecAdapter
42
     * @return bool
43
     */
44 2
    public static function validateHex($hex, EcAdapterInterface $ecAdapter = null)
45
    {
46
        try {
47 2
            self::fromHex($hex, $ecAdapter);
0 ignored issues
show
Bug introduced by
It seems like $hex defined by parameter $hex on line 44 can also be of type object<BitWasp\Buffertools\BufferInterface>; however, BitWasp\Bitcoin\Key\PublicKeyFactory::fromHex() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48 2
            return true;
49 2
        } catch (\Exception $e) {
50 2
            return false;
51
        }
52
    }
53
}
54