Completed
Push — master ( 9a7b87...ad20a3 )
by Alessandro
03:07
created

PHPDbgBinFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Configuration;
5
6
use Symfony\Component\Process\Process;
7
8
/**
9
 * Class PHPDbgBinFile
10
 * @package Paraunit\Configuration
11
 */
12
class PHPDbgBinFile
13
{
14
    /** @var string Realpath to the PHPDbg bin location */
15
    private $phpDbgBin;
16
17
    /**
18
     * PHPDbgBinFile constructor.
19
     */
20
    public function __construct()
21
    {
22
        $this->phpDbgBin = $this->getPhpDbgBinLocation();
23
    }
24
25
    /**
26
     * @return string
27
     * @throws \RuntimeException When PHPDBG is not available
28
     */
29
    public function getPhpDbgBin(): string
30
    {
31
        if (! $this->isAvailable()) {
32
            throw new \RuntimeException('PHPDbg is not available!');
33
        }
34
35
        return $this->phpDbgBin;
36
    }
37
38
    public function isAvailable(): bool
39
    {
40
        return $this->phpDbgBin !== '';
41
    }
42
43
    private function getPhpDbgBinLocation(): string
44
    {
45
        $checkInPath = new Process('phpdbg --version');
46
        $checkInPath->run();
47
        if ($checkInPath->getExitCode() === 0) {
48
            return 'phpdbg';
49
        }
50
51
        $locator = new Process('command -v phpdbg');
52
        $locator->run();
53
54
        return (string)preg_replace('/\s/', '', $locator->getOutput());
55
    }
56
}
57