Console::onTerminate()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 23
c 3
b 0
f 1
ccs 0
cts 21
cp 0
rs 8.7972
cc 4
eloc 14
nc 4
nop 1
crap 20
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