Console   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 42
c 3
b 0
f 1
ccs 0
cts 25
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onTerminate() 0 23 4
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