ExecutionContext::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
namespace ivol;
3
4
use ivol\Config\ConfigurationFactory;
5
6
class ExecutionContext
7
{
8
    /**
9
     * @var string Sprintf formatted string @see http://php.net/manual/en/function.sprintf.php
10
     */
11
    private $command;
12
    /** @var  array */
13
    private $params;
14
    /** @var  array */
15
    private $config;
16
17
    /**
18
     * @param string $command
19
     * @param array $params
20
     */
21 10
    public function __construct($command, $params)
22
    {
23 10
        $this->command = $command;
24 10
        $this->params = $params;
25 10
        $this->config = ConfigurationFactory::createFromArray();
26 10
    }
27
28
    /**
29
     * @return string
30
     */
31
    public function getCommand()
32
    {
33
        return $this->command;
34
    }
35
36
    /**
37
     * @return array
38
     */
39 10
    public function getParams()
40
    {
41 10
        return $this->config['escape_shell_args'] ? $this->getEscapedParams() : $this->params;
42
    }
43
44
    /**
45
     * @return string
46
     */
47 8
    public function getFullCommand() {
48 8
        $params = $this->getParams();
49 8
        array_unshift($params, $this->command);
50 8
        $command = call_user_func_array('sprintf', $params);
51 8
        return $this->config['escape_shell_cmd'] ? escapeshellcmd($command) : $command;
52
    }
53
54
    /**
55
     * @param array $config
56
     */
57 8
    public function setConfig($config)
58
    {
59 8
        $this->config = $config;
60 8
    }
61
62
    /**
63
     * @return array
64
     */
65 8
    private function getEscapedParams() {
66 8
        $escapedArgs = array();
67 8
        foreach ($this->params as $param) {
68 8
            $escapedArgs[] = escapeshellarg($param);
69 8
        }
70 8
        return $escapedArgs;
71
    }
72
}