Completed
Pull Request — master (#591)
by thomas
22:54 queued 09:12
created

RegtestBitcoinFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\RpcTest;
6
7
use BitWasp\Bitcoin\Crypto\Random\Random;
8
use BitWasp\Bitcoin\Network\NetworkFactory;
9
10
class RegtestBitcoinFactory
11
{
12
    const TESTS_DIR = "BITCOIND_TEST_DIR";
13
    const BITCOIND = "BITCOIND_PATH";
14
15
    /**
16
     * @var array|false|null|string
17
     */
18
    private $testsDirPath;
19
20
    /**
21
     * @var array|false|null|string
22
     */
23
    private $bitcoindPath;
24
25
    /**
26
     * @var string[]
27
     */
28
    private $testDir = [];
29
30
    /**
31
     * @var RpcServer[]
32
     */
33
    private $server = [];
34
35
    /**
36
     * @var \BitWasp\Bitcoin\Network\NetworkInterface
37
     */
38
    private $network;
39
40
    /**
41
     * @var RpcCredential
42
     */
43
    private $credential;
44
45
    public function __construct()
46
    {
47
        $this->testsDirPath = $this->envOrDefault("BITCOIND_TEST_DIR", "/tmp");
48
        $this->bitcoindPath = $this->envOrDefault("BITCOIND_PATH");
49
        if (null === $this->bitcoindPath) {
50
            throw new \RuntimeException("Missing BITCOIND_PATH variable");
51
        }
52
53
        $this->network = NetworkFactory::bitcoinTestnet();
54
        $this->credential = new RpcCredential("127.0.0.1", 18332, "rpcuser", "rpcpass", false);
55
    }
56
57
    /**
58
     * @param string $var
59
     * @param string|null $default
60
     * @return string
61
     */
62
    private function envOrDefault(string $var, string $default = null): string
63
    {
64
        $value = getenv($var);
65
        if (in_array($value, [null, false, ""])) {
66
            $value = $default;
67
        }
68
        return $value;
69
    }
70
71
    /**
72
     * @return string
73
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
74
     */
75
    protected function createRandomTestDir(): string
76
    {
77
        $this->testDir[] = $dir = $this->testsDirPath . "/" . (new Random())->bytes(5)->getHex();
78
        if (!mkdir($dir)) {
79
            throw new \RuntimeException("Failed to create test dir!");
80
        }
81
        return $dir;
82
    }
83
84
    /**
85
     * @param array $options
86
     * @return RpcServer
87
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
88
     */
89
    public function startBitcoind($options = []): RpcServer
90
    {
91
        $testDir = $this->createRandomTestDir();
92
        $rpcServer = new RpcServer($this->bitcoindPath, $testDir, $this->network, $this->credential, $options);
93
        $rpcServer->start();
94
        $this->server[] = $rpcServer;
95
        return $rpcServer;
96
    }
97
98
    /**
99
     *
100
     */
101
    protected function cleanup()
102
    {
103
        $servers = 0;
104
        $dirs = 0;
105
        foreach ($this->server as $server) {
106
            if ($server->isRunning()) {
107
                $servers++;
108
                $server->destroy();
109
            }
110
        }
111
112
        echo "Cleaned up {$servers} servers, and {$dirs} directories\n";
113
    }
114
115
    public function __destruct()
116
    {
117
        $this->cleanup();
118
    }
119
}
120