NodeService::createNewNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoind;
6
7
use BitWasp\Bitcoind\Config;
8
use BitWasp\Bitcoind\Node\NodeOptions;
9
use BitWasp\Bitcoind\Node\Server;
10
11
class NodeService
12
{
13 10
    protected function checkBitcoindExists(string $bitcoind)
14
    {
15 10
        if (!file_exists($bitcoind)) {
16 1
            throw new Exception\SetupException("Path to bitcoind executable is invalid: {$bitcoind}");
17
        }
18
19 9
        if (!is_executable($bitcoind)) {
20 1
            throw new Exception\SetupException("Bitcoind must be executable");
21
        }
22 8
    }
23
24 8
    protected function setupDataDir(NodeOptions $options, Config\Config $config, Config\Writer $writer)
25
    {
26 8
        if (is_dir($options->getDataDir())) {
27 1
            throw new Exception\SetupException("Cannot create a node in non-empty datadir");
28
        }
29
30 7
        if (!mkdir($options->getDataDir())) {
31
            throw new Exception\SetupException("Could not create datadir ({$options->getDataDir()}) - is it writable?");
32
        }
33
34 7
        $writer->create($options->getAbsoluteConfigPath(), $config);
35 7
    }
36
37 10
    public function createNewNode(NodeOptions $options, Config\Config $config, Config\Writer $writer): Server
38
    {
39 10
        $this->checkBitcoindExists($options->getBitcoindPath());
40 8
        $this->setupDataDir($options, $config, $writer);
41
42 7
        return new Server($options);
43
    }
44
45 1
    public function loadNode(NodeOptions $options): Server
46
    {
47 1
        $this->checkBitcoindExists($options->getBitcoindPath());
48 1
        return new Server($options);
49
    }
50
}
51