Console::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AniDbFillerBundle\Event\Listener;
10
11
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
12
use Symfony\Component\Process\PhpExecutableFinder;
13
use Symfony\Component\Process\Process;
14
15
/**
16
 * Update the titles db on clear cache.
17
 */
18
class Console
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $root_dir;
24
25
    /**
26
     * @param string $root_dir
27
     */
28
    public function __construct($root_dir)
29
    {
30
        $this->root_dir = $root_dir;
31
    }
32
33
    /**
34
     * @param ConsoleTerminateEvent $event
35
     */
36
    public function onTerminate(ConsoleTerminateEvent $event)
37
    {
38
        if ($event->getCommand()->getName() == 'cache:clear') {
39
            $env = ltrim($event->getInput()->getOption('env'), '=');
40
            $cmd = 'animedb:update-titles -e='.$env;
41
42
            $phpFinder = new PhpExecutableFinder();
43
            if (!($phpPath = $phpFinder->find())) {
44
                throw new \RuntimeException(
45
                    'The php executable could not be found, add it to your PATH environment variable and try again'
46
                );
47
            }
48
49
            $php = escapeshellarg($phpPath);
50
            $process = new Process($php.' app/console '.$cmd, $this->root_dir.'/../', null, null, 1500);
51
            $process->run(function ($type, $buffer) {
52
                echo $buffer;
53
            });
54
            if (!$process->isSuccessful()) {
55
                throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', $cmd));
56
            }
57
        }
58
    }
59
}
60