Failed Conditions
Push — master ( ee09ca...254dbe )
by thomas
97:03 queued 67:04
created

NodeOptions::getBitcoindPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
    public function __construct(string $bitcoind, string $dataDir)
32
    {
33
        if (substr($dataDir, -1) !== "/") {
34
            $dataDir = "{$dataDir}/";
35
        }
36
37
        $this->bitcoind = $bitcoind;
38
        $this->dataDir = $dataDir;
39
    }
40
41
    public function withConfigFileName(string $fileName): NodeOptions
42
    {
43
        $this->configFileName = $fileName;
44
        return $this;
45
    }
46
47
    public function getBitcoindPath(): string
48
    {
49
        return $this->bitcoind;
50
    }
51
52
    public function getDataDir(): string
53
    {
54
        return $this->dataDir;
55
    }
56
57
    public function getConfigFileName(): string
58
    {
59
        return $this->configFileName;
60
    }
61
62
    private function getAbsolutePath(string $path): string
63
    {
64
        return "{$this->dataDir}{$path}";
65
    }
66
67
    public function getAbsoluteConfigPath(): string
68
    {
69
        return $this->getAbsolutePath($this->configFileName);
70
    }
71
72
    public function getAbsolutePidPath(Config $config): string
73
    {
74
        return $this->getAbsolutePath($config->getRelativePidPath());
75
    }
76
77
    public function getAbsoluteLogPath(Config $config): string
78
    {
79
        return $this->getAbsolutePath($config->getRelativeLogPath());
80
    }
81
82
    public function getStartupCommand(): string
83
    {
84
        return sprintf("%s -datadir=%s", $this->getBitcoindPath(), $this->getDataDir());
85
    }
86
87
    public function hasConfig(): bool
88
    {
89
        $configPath = $this->getAbsoluteConfigPath();
90
        return file_exists($configPath) && is_file($configPath);
91
    }
92
}
93