Passed
Push — master ( 0b6dbd...69bbdb )
by Andrea
13:36 queued 09:48
created

Utility::runCommand()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 3
rs 9.7998
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Utils;
4
5
use Symfony\Bundle\FrameworkBundle\Console\Application;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Symfony\Component\HttpKernel\Kernel;
9
use Symfony\Component\Process\Process;
10
11
class Utility
12
{
13
14
    private $apppaths;
15
    private $kernel;
16
17 1
    public function __construct($kernel, ProjectPath $projectpath)
18
    {
19 1
        $this->apppaths = $projectpath;
20 1
        $this->kernel = $kernel;
21 1
    }
22
    public function clearcache($env = '')
23
    {
24
        if (!$env) {
25
            $env = $this->kernel->getEnvironment();
26
        }
27
28
        $command = $this->apppaths->getConsole() . ' cache:clear --env=' . $env;
29
30
        return self::runCommand($command);
31
    }
32 1
    public static function runCommand($command, $workingdir = '.')
33
    {
34
        /* @var $process \Symfony\Component\Process\Process */
35 1
        $process = new Process(explode(" ", $command), $workingdir);
36
37 1
        if ($workingdir) {
38 1
            $process->setWorkingDirectory($workingdir);
39
        }
40 1
        $process->setTimeout(60 * 60 * 24);
41 1
        $process->run();
42
43 1
        if (!$process->isSuccessful()) {
44 1
            $return = array('errcode' => -1,
45 1
                'command' => $command,
46 1
                'message' => 'Errore nel comando ' . $command . "\n" . $process->getErrorOutput() . "\n" . $process->getOutput(),);
47
        } else {
48 1
            $return = array('errcode' => 0,
49 1
                'command' => $command,
50 1
                'message' => $process->getOutput(),
51
            );
52
        }
53
54 1
        return $return;
55
    }
56 1
    public function runSymfonyCommand($command, array $options = array())
57
    {
58 1
        $application = new Application($this->kernel);
59 1
        $application->setAutoExit(false);
60
61 1
        $cmdoptions = array_merge(array('command' => $command), $options);
62
63 1
        $outputbuf = new BufferedOutput();
64
        // return the output, don't use if you used NullOutput()
65 1
        $returncode = $application->run(new ArrayInput($cmdoptions), $outputbuf);
66 1
        $output = $outputbuf->fetch();
67
68 1
        return array('errcode' => (0 == $returncode ? 0 : 1), 'command' => $cmdoptions['command'], 'message' => $output);
69
    }
70
}
71