Completed
Pull Request — master (#546)
by thomas
75:35 queued 05:34
created

RegtestBitcoinFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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