ServiceOptions::getPhingBin()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 10
nc 12
nop 0
crap 7
1
<?php
2
3
namespace BsbPhingService\Options;
4
5
use RuntimeException;
6
use Symfony\Component\Process\ProcessBuilder;
7
use Zend\Stdlib\AbstractOptions;
8
9
class ServiceOptions extends AbstractOptions
10
{
11
12
    /**
13
     * Path to php binary
14
     *
15
     * @var string
16
     */
17
    private $phpBin = null;
18
19
    /**
20
     * Path to phing binary
21
     *
22
     * @var string
23
     */
24
    private $phingBin = null;
25
26
    /**
27
     * @param array $options
28
     */
29 19
    public function __construct(array $options = null)
30
    {
31 19
        parent::__construct($options);
32 19
    }
33
34
    /**
35
     * Sets the php executable
36
     *
37
     * Set it with a value of 'null' to automaticly discover the path
38
     *
39
     * @param null $path
40
     */
41 3
    public function setPhpBin($path = null)
42
    {
43 3
        $this->phpBin = $path;
44 3
    }
45
46
    /**
47
     * Get the path to the php executable
48
     *
49
     * Attempt auto discovery via 'which php' if null
50
     *
51
     * @return string
52
     */
53 3
    public function getPhpBin()
54
    {
55 3
        if ($this->phpBin === null) {
56 1
            $builder = new ProcessBuilder();
57 1
            $builder->setPrefix('which');
58 1
            $builder->setArguments(array('php'));
59
60 1
            $process = $builder->getProcess();
61 1
            $process->run();
62
63 1
            if ($process->getExitCodeText() == 'OK') {
64 1
                $this->phpBin = trim($process->getOutput());
65 1
            }
66 1
        }
67
68 3
        return $this->phpBin;
69
    }
70
71
    /**
72
     * Set path to the phing binary
73
     *
74
     * @param string $phingBin
75
     */
76 3
    public function setPhingBin($phingBin = null)
77
    {
78 3
        $this->phingBin = $phingBin;
79 3
    }
80
81
    /**
82
     * Get path to the phing binary
83
     *
84
     * Attempt auto discovery from composer if null
85
     *
86
     * @return string
87
     */
88 9
    public function getPhingBin()
89
    {
90 9
        if ($this->phingBin === null && file_exists('./composer.json')) {
91 7
            @$composer = json_decode(file_get_contents('./composer.json'), true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
92 7
            if (isset($composer['bin-dir'])) {
93 1
                $this->phingBin = $composer['bin-dir'] . '/phing';
94 1
            }
95 7
        }
96
97 9
        if ($this->phingBin === null && getenv('COMPOSER_BIN_DIR') !== false) {
98 1
            $this->phingBin = getenv('COMPOSER_BIN_DIR') . '/phing';
99 1
        }
100
101 9
        if ($this->phingBin === null) {
102 5
            $this->phingBin = './vendor/bin/phing';
103 5
        }
104
105 9
        return $this->phingBin;
106
    }
107
}
108