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

Utility   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 83.87%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 30
c 2
b 1
f 0
dl 0
loc 58
ccs 26
cts 31
cp 0.8387
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A clearcache() 0 9 2
A runSymfonyCommand() 0 13 2
A runCommand() 0 23 3
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