NodeOptions::getBitcoindPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoind\Node;
6
7
use BitWasp\Bitcoind\Config\Config;
8
9
class NodeOptions
10
{
11
    /**
12
     * @var string
13
     */
14
    private $configFileName = "bitcoin.conf";
15
16
    /**
17
     * @var string
18
     */
19
    private $dataDir;
20
21
    /**
22
     * @var string
23
     */
24
    private $bitcoind;
25
26
    /**
27
     * NodeOptions constructor.
28
     * @param string $bitcoind - path to bitcoind executable
29
     * @param string $dataDir - path to bitcoin datadir
30
     */
31 16
    public function __construct(string $bitcoind, string $dataDir)
32
    {
33 16
        if (substr($dataDir, -1) !== "/") {
34 13
            $dataDir = "{$dataDir}/";
35
        }
36
37 16
        $this->bitcoind = $bitcoind;
38 16
        $this->dataDir = $dataDir;
39 16
    }
40
41 1
    public function withConfigFileName(string $fileName): NodeOptions
42
    {
43 1
        $this->configFileName = $fileName;
44 1
        return $this;
45
    }
46
47 11
    public function getBitcoindPath(): string
48
    {
49 11
        return $this->bitcoind;
50
    }
51
52 12
    public function getDataDir(): string
53
    {
54 12
        return $this->dataDir;
55
    }
56
57 3
    public function getConfigFileName(): string
58
    {
59 3
        return $this->configFileName;
60
    }
61
62 11
    private function getAbsolutePath(string $path): string
63
    {
64 11
        return "{$this->dataDir}{$path}";
65
    }
66
67 10
    public function getAbsoluteConfigPath(): string
68
    {
69 10
        return $this->getAbsolutePath($this->configFileName);
70
    }
71
72 3
    public function getAbsolutePidPath(Config $config): string
73
    {
74 3
        return $this->getAbsolutePath($config->getRelativePidPath());
75
    }
76
77
    public function getAbsoluteLogPath(Config $config): string
78
    {
79
        return $this->getAbsolutePath($config->getRelativeLogPath());
80
    }
81
82 3
    public function getStartupCommand(): string
83
    {
84 3
        return sprintf("%s -datadir=%s", $this->getBitcoindPath(), $this->getDataDir());
85
    }
86
87 2
    public function hasConfig(): bool
88
    {
89 2
        $configPath = $this->getAbsoluteConfigPath();
90 2
        return file_exists($configPath) && is_file($configPath);
91
    }
92
}
93