Passed
Push — master ( ce12d9...d4e42c )
by Dāvis
02:47
created

ScriptController::lexikAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
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
11
class ScriptController extends Controller
12
{
13
    public function redisAction()
14
    {
15
        $data['success'] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
16
17
        $clients = [];
18
        foreach ($this->container->getServiceIds() as $id) {
1 ignored issue
show
Bug introduced by
The method getServiceIds() does not exist on Symfony\Component\Depend...tion\ContainerInterface. It seems like you code against a sub-type of Symfony\Component\Depend...tion\ContainerInterface such as Symfony\Component\DependencyInjection\Container or Symfony\Component\Depend...ection\ContainerBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        foreach ($this->container->/** @scrutinizer ignore-call */ getServiceIds() as $id) {
Loading history...
19
            if (substr($id, 0, 9) === 'snc_redis' && $this->container->get($id) instanceof Client) {
20
                $clients[] = $id;
21
            }
22
        }
23
24
        foreach ($clients as $snc) {
25
            $this->container->get($snc)->flushdb();
26
        }
27
28
        return new JsonResponse($data, 200, [
29
            'Cache-Control' => 'no-cache',
30
        ]);
31
    }
32
33
    public function cacheAction()
34
    {
35
        global $kernel;
36
37
        if ('AppCache' === get_class($kernel)) {
38
            $kernel = $kernel->getKernel();
39
        }
40
41
        $commands = [
42
            'clear',
43
            'warmup',
44
        ];
45
        foreach ($commands as $command) {
46
            $application = new Application($kernel);
47
            $application->setAutoExit(false);
48
            $input = new ArrayInput([
49
                'command' => 'cache:'.$command,
50
                '--env' => $kernel->getEnvironment(),
51
            ]);
52
            $application->run($input);
53
            $data['success'] = 1;
54
        }
55
56
        return new JsonResponse($data, 200, [
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be defined by a foreach iteration on line 45. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
57
            'Cache-Control' => 'no-cache',
58
        ]);
59
    }
60
61
    public function lexikAction()
62
    {
63
        global $kernel;
64
65
        if ('AppCache' === get_class($kernel)) {
66
            $kernel = $kernel->getKernel();
67
        }
68
69
        $application = new Application($kernel);
70
        $application->setAutoExit(false);
71
72
        $input = new ArrayInput([
73
            'command' => 'sludio:lexik:clear',
74
        ]);
75
        $application->run($input);
76
        $data['success'] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
77
78
        return new JsonResponse($data, 200, [
79
            'Cache-Control' => 'no-cache',
80
        ]);
81
    }
82
}
83