Completed
Pull Request — master (#224)
by thomas
26:02 queued 07:51
created

Bitcoin::setAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BitWasp\Bitcoin;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Math\Math;
8
use BitWasp\Bitcoin\Network\Network;
9
use BitWasp\Bitcoin\Network\NetworkFactory;
10
use BitWasp\Bitcoin\Network\NetworkInterface;
11
use Mdanter\Ecc\EccFactory;
12
use Mdanter\Ecc\Primitives\GeneratorPoint;
13
14
class Bitcoin
15
{
16
    /**
17
     * @var NetworkInterface
18
     */
19
    private static $network;
20
21
    private static $adapter;
22
23
    /**
24
     * @return Math
25
     */
26 1962
    public static function getMath()
27
    {
28 1962
        return new Math();
29
    }
30
31
    /**
32
     * Load the generator to be used throughout
33
     */
34 6
    public static function getGenerator()
35
    {
36 6
        return EccFactory::getSecgCurves(self::getMath())->generator256k1();
37
    }
38
39
    /**
40
     * @param Math $math
41
     * @param GeneratorPoint $generator
42
     * @return EcAdapterInterface
43
     */
44 639
    public static function getEcAdapter(Math $math = null, GeneratorPoint $generator = null)
45
    {
46 639
        if (null === self::$adapter) {
47
            self::$adapter = EcAdapterFactory::getAdapter(
48
                ($math ?: self::getMath()),
49
                ($generator ?: self::getGenerator())
50
            );
51
        }
52
53 639
        return self::$adapter;
54
    }
55
56
    public static function setAdapter(EcAdapterInterface $adapter)
57
    {
58
        self::$adapter = $adapter;
59
    }
60
61
    /**
62
     * @param NetworkInterface $network
63
     */
64 6
    public static function setNetwork(NetworkInterface $network)
65
    {
66 6
        self::$network = $network;
67 6
    }
68
69
    /**
70
     * @return Network
71
     */
72 210
    public static function getNetwork()
73
    {
74 210
        if (null === self::$network) {
75 6
            self::$network = self::getDefaultNetwork();
76 6
        }
77
78 210
        return self::$network;
79
    }
80
81 12
    public static function getDefaultNetwork()
82
    {
83 12
        return NetworkFactory::bitcoin();
84
    }
85
}
86