ExecutionWrapper::exec()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
namespace ivol;
3
4
use ivol\Config\ConfigurationFactory;
5
use ivol\EventDispatcher\AfterExecuteEvent;
6
use ivol\EventDispatcher\BeforeExecuteEvent;
7
use ivol\EventDispatcher\EscapeArgsSubscriber;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
10
class ExecutionWrapper
11
{
12
    /** @var array  */
13
    private $config;
14
    /** @var EventDispatcher */
15
    private $eventDispatcher;
16
17
    /**
18
     * @param array $config
19
     */
20 5
    public function __construct($config = array())
21
    {
22 5
        $this->config = ConfigurationFactory::createFromArray($config);
23 5
        $this->eventDispatcher = new EventDispatcher();
24 5
    }
25
26
    /**
27
     * @return EventDispatcher
28
     */
29 2
    public function getEventDispatcher()
30
    {
31 2
        return $this->eventDispatcher;
32
    }
33
34
    /**
35
     * @param string $command Sprintf formatted string @see http://php.net/manual/en/function.sprintf.php
36
     * @param array $params
37
     * @return ExecutionResult
38
     */
39 5
    public function exec($command, $params)
40
    {
41 5
        $execParams = new ExecutionContext($command, $params);
42 5
        $execParams->setConfig($this->config);
43 5
        $beforeExecuteEvent = new BeforeExecuteEvent($execParams);
44 5
        $this->eventDispatcher->dispatch(BeforeExecuteEvent::EVENT_NAME, $beforeExecuteEvent);
45 5
        exec($beforeExecuteEvent->getParams()->getFullCommand(), $output, $returnValue);
46 5
        $output = $output ?: array();
47 5
        $afterExecuteEvent = new AfterExecuteEvent(new ExecutionResult($returnValue, $output));
48 5
        $this->eventDispatcher->dispatch(AfterExecuteEvent::EVENT_NAME, $afterExecuteEvent);
49 5
        return $afterExecuteEvent->getResult();
50
    }
51
}