Passed
Push — master ( 7e6548...8ba614 )
by Dāvis
02:53
created

ScriptController::cacheAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 1
b 1
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Controller;
4
5
use Predis\Client;
6
use Symfony\Bundle\FrameworkBundle\Console\Application;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Sludio\HelperBundle\Translatable\Repository\TranslatableRepository as Sludio;
11
12
class ScriptController extends Controller
13
{
14
    private function result()
15
    {
16
        return new JsonResponse(['success' => 1], 200, [
17
            'Cache-Control' => 'no-cache',
18
        ]);
19
    }
20
21
    private function runApp($command, $params = [], $return = true)
22
    {
23
        $data = [
24
            'command' => $command,
25
        ];
26
        if (!empty($params)) {
27
            $data = array_merge($data, $params);
28
        }
29
30
        $application = new Application($this->container->get('kernel'));
31
        $application->setAutoExit(false);
32
        $input = new ArrayInput($data);
33
        $application->run($input);
34
35
        if ($return === true) {
36
            return $this->result();
37
        }
38
    }
39
40
    public function redisAction()
41
    {
42
        return $this->runApp('sludio:redis:flush');
43
    }
44
45
    public function cacheAction()
46
    {
47
        $commands = [
48
            'clear',
49
            'warmup',
50
        ];
51
        foreach ($commands as $command) {
52
            $this->runApp('cache:'.$command, ['--env' => $this->container->get('kernel')->getEnvironment()], false);
53
        }
54
55
        return $this->result();
56
    }
57
58
    public function lexikAction()
59
    {
60
        return $this->runApp('sludio:lexik:clear');
61
    }
62
63
    public function generateAction()
64
    {
65
        Sludio::getAllTranslations();
66
67
        return $this->result();
68
    }
69
}
70