Completed
Push — master ( e79d55...e4347d )
by thomas
15s
created

Bitcoin::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
93
    }
94
95
    /**
96
     * @param EcAdapterInterface $adapter
97
     */
98
    public static function setAdapter(EcAdapterInterface $adapter)
99
    {
100
        self::$adapter = $adapter;
101
    }
102
103
    /**
104
     * @param NetworkInterface $network
105
     */
106
    public static function setNetwork(NetworkInterface $network)
107
    {
108
        self::$network = $network;
109
    }
110
111
    /**
112
     * @return Network
113
     */
114
    public static function getNetwork()
115
    {
116
        if (null === self::$network) {
117
            self::$network = self::getDefaultNetwork();
118
        }
119
120
        return self::$network;
121
    }
122
123
    /**
124
     * @return NetworkInterface
125
     */
126
    public static function getDefaultNetwork()
127
    {
128
        return NetworkFactory::bitcoin();
129
    }
130
}
131