Completed
Push — master ( 802a45...8d6568 )
by thomas
133:18 queued 60:55
created

RegtestBitcoinFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A envOrDefault() 0 7 2
A createRandomTestDir() 0 7 2
A startBitcoind() 0 7 1
A cleanup() 0 12 3
A __destruct() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\RpcTest;
4
5
6
use BitWasp\Bitcoin\Crypto\Random\Random;
7
use BitWasp\Bitcoin\Network\NetworkFactory;
8
9
class RegtestBitcoinFactory
10
{
11
    const TESTS_DIR = "BITCOIND_TEST_DIR";
12
    const BITCOIND = "BITCOIND_PATH";
13
14
    /**
15
     * @var array|false|null|string
16
     */
17
    private $testsDirPath;
18
19
    /**
20
     * @var array|false|null|string
21
     */
22
    private $bitcoindPath;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $testDir = [];
28
29
    /**
30
     * @var RpcServer[]
31
     */
32
    private $server = [];
33
34
    public function __construct() {
35
        $this->testsDirPath = $this->envOrDefault("BITCOIND_TEST_DIR", "/tmp");
36
        $this->bitcoindPath = $this->envOrDefault("BITCOIND_PATH");
37
        $this->network = NetworkFactory::bitcoinTestnet();
0 ignored issues
show
Bug introduced by
The property network does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        $this->credential = new RpcCredential("127.0.0.1", 18332, "rpcuser", "rpcpass");
0 ignored issues
show
Bug introduced by
The property credential does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
    }
40
41
    private function envOrDefault($var, $default = null) {
42
        $value = getenv($var);
43
        if (in_array($value, [null, false, ""])) {
44
            $value = $default;
45
        }
46
        return $value;
47
    }
48
49
    protected function createRandomTestDir() {
50
        $this->testDir[] = $dir = $this->testsDirPath . "/" . (new Random())->bytes(5)->getHex();
51
        if (!mkdir($dir)) {
52
            throw new \RuntimeException("Failed to create test dir!");
53
        }
54
        return $dir;
55
    }
56
57
    public function startBitcoind() {
58
        $testDir = $this->createRandomTestDir();
59
        $rpcServer = new RpcServer($this->bitcoindPath, $testDir, $this->network, $this->credential);
60
        $rpcServer->start();
61
        $this->server[] = $rpcServer;
62
        return $rpcServer;
63
    }
64
65
    protected function cleanup() {
66
        $servers = 0;
67
        $dirs = 0;
68
        foreach ($this->server as $server) {
69
            if ($server->isRunning()) {
70
                $servers++;
71
                $server->destroy();
72
            }
73
        }
74
75
        echo "Cleaned up {$servers} servers, and {$dirs} directories\n";
76
    }
77
78
    public function __destruct()
79
    {
80
        $this->cleanup();
81
    }
82
}
83