EcAdapterFactory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 8.69%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 79
ccs 2
cts 23
cp 0.0869
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSecp256k1() 0 3 1
A getAdapter() 0 13 4
A getSecp256k1Context() 0 15 5
A setAdapter() 0 3 1
A getPhpEcc() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Crypto\EcAdapter;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter as PhpEcc;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter as Secp256k1;
10
use BitWasp\Bitcoin\Math\Math;
11
use Composer\Semver\Semver;
12
use Mdanter\Ecc\Primitives\GeneratorPoint;
13
14
class EcAdapterFactory
15
{
16
    /**
17
     * @var EcAdapterInterface
18
     */
19
    private static $adapter;
20
21
    /**
22
     * @var resource
23
     */
24
    private static $context;
25
26
    /**
27
     * @param int|null $flags
28
     * @return resource
29
     */
30
    public static function getSecp256k1Context(int $flags = null)
31
    {
32
        if (!extension_loaded('secp256k1')) {
33
            throw new \RuntimeException('Secp256k1 not installed');
34
        }
35
36
        if (self::$context === null) {
0 ignored issues
show
introduced by
The condition self::context === null is always false.
Loading history...
37
            $context = secp256k1_context_create($flags ?: SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
38
            if (null === $context) {
39
                throw new \RuntimeException("Failed to initialize secp256k1 context");
40
            }
41
            self::$context = $context;
42
        }
43
44
        return self::$context;
45
    }
46
47
    /**
48
     * @param Math $math
49
     * @param GeneratorPoint $generator
50
     * @return EcAdapterInterface
51
     */
52
    public static function getAdapter(Math $math, GeneratorPoint $generator): EcAdapterInterface
53
    {
54
        if (self::$adapter !== null) {
55
            return self::$adapter;
56
        }
57
58
        if (extension_loaded('secp256k1') && Semver::satisfies(phpversion('secp256k1'), "^0.2.0")) {
59
            self::$adapter = self::getSecp256k1($math, $generator);
60
        } else {
61
            self::$adapter = self::getPhpEcc($math, $generator);
62
        }
63
64
        return self::$adapter;
65
    }
66
67
    /**
68
     * @param EcAdapterInterface $ecAdapter
69
     */
70
    public static function setAdapter(EcAdapterInterface $ecAdapter)
71
    {
72
        self::$adapter = $ecAdapter;
73
    }
74
75
    /**
76
     * @param Math $math
77
     * @param GeneratorPoint $generator
78
     * @return PhpEcc
79
     */
80 1
    public static function getPhpEcc(Math $math, GeneratorPoint $generator): PhpEcc
81
    {
82 1
        return new PhpEcc($math, $generator);
83
    }
84
85
    /**
86
     * @param Math $math
87
     * @param GeneratorPoint $generator
88
     * @return Secp256k1
89
     */
90
    public static function getSecp256k1(Math $math, GeneratorPoint $generator): Secp256k1
91
    {
92
        return new Secp256k1($math, $generator, self::getSecp256k1Context());
93
    }
94
}
95