Completed
Push — master ( 9b53e2...2758ff )
by Andrea
19:08 queued 08:29
created

PannelloAmministrazioneUtils::runCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 14
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
crap 2.0932
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\DependencyInjection;
4
5
use Symfony\Bundle\FrameworkBundle\Console\Application;
6
use Symfony\Component\Console\Output\BufferedOutput;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Process\Process;
9
use Fi\PannelloAmministrazioneBundle\DependencyInjection\ProjectPath;
10
use Fi\OsBundle\DependencyInjection\OsFunctions;
11
12
class PannelloAmministrazioneUtils
13
{
14
15
    private $container;
16
    private $apppaths;
17
18 2
    public function __construct($container)
19
    {
20 2
        $this->container = $container;
21 2
        $this->apppaths = $container->get("pannelloamministrazione.projectpath");
22 2
    }
23
24
    public function clearcache($env = "")
25
    {
26
        if (!$env) {
27
            $env = $this->container->get('kernel')->getEnvironment();
28
        }
29
30
        $phpPath = OsFunctions::getPHPExecutableFromPath();
31
32
        if (in_array($env, array('dev', 'test', 'localhost'))) {
33
            $command = $phpPath . ' ' . $this->apppaths->getConsole() . ' cache:clear '
34
                    . '--no-warmup --env=' . $env;
35
        } else {
36
            $command = $phpPath . ' ' . $this->apppaths->getConsole() . ' cache:clear '
37
                    . '--no-debug --no-warmup --env=' . $env;
38
39
            $command = $command . " && " . $phpPath . ' ' . $this->apppaths->getConsole() . ' cache:warmup '
40
                    . '--no-debug --env=' . $env;
41
        }
42
43
        return self::runCommand($command);
44
    }
45
46 1
    public static function runCommand($command)
47
    {
48
        /* @var $process \Symfony\Component\Process\Process */
49 1
        $process = new Process($command);
50 1
        $process->setTimeout(60 * 100);
51 1
        $process->run();
52
53 1
        if (!$process->isSuccessful()) {
54
            $return = array("errcode" => -1,
55
                "command" => $command,
56
                "errmsg" => 'Errore nel comando ' . $command . $process->getErrorOutput() . $process->getOutput());
57
        } else {
58 1
            $return = array("errcode" => 0,
59 1
                "command" => $command,
60 1
                "errmsg" => $process->getOutput()
61 1
            );
62
        }
63
64 1
        return $return;
65
    }
66
67
    public function runSymfonyCommand($command, array $options = array())
68
    {
69
        $application = new Application($this->container->get('kernel'));
70
        $application->setAutoExit(false);
71
72
        $cmdoptions = array_merge(array('command' => $command), $options);
73
74
        $outputbuf = new BufferedOutput();
75
        // return the output, don't use if you used NullOutput()
76
        $returncode = $application->run(new ArrayInput($cmdoptions), $outputbuf);
77
        $output = $outputbuf->fetch();
78
79
        return array('errcode' => ($returncode == 0 ? 0 : 1), 'command' => $cmdoptions['command'], 'message' => $output);
80
    }
81
}
82