Passed
Push — master ( 721d1a...6f7d76 )
by Dāvis
05:10
created

RedisFlushCommand::execute()   C

Complexity

Conditions 9
Paths 21

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 15
c 1
b 0
f 0
nc 21
nop 2
dl 0
loc 22
rs 6.412
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Command;
4
5
use Predis\Client;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputDefinition;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class RedisFlushCommand extends ContainerAwareCommand
13
{
14
    protected function configure()
15
    {
16
        $this->setName('sludio:redis:flush')->setDefinition(new InputDefinition([
17
            new InputArgument('clients', InputArgument::IS_ARRAY | InputArgument::OPTIONAL),
18
        ]));
19
    }
20
21
    protected function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        $clients = [];
24
        foreach ($this->getContainer()->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

24
        foreach ($this->getContainer()->/** @scrutinizer ignore-call */ getServiceIds() as $id) {
Loading history...
25
            if (substr($id, 0, 9) === 'snc_redis' && $this->getContainer()->get($id) instanceof Client) {
26
                $clients[] = $id;
27
            }
28
        }
29
30
        if (!empty($clients)) {
31
            $allowed = $clients;
32
            $clientsInput = $input->getArgument('clients');
33
            if (!empty($clientsInput)) {
34
                foreach ($clientsInput as &$client) {
35
                    $client = 'snc_redis.'.$client;
36
                }
37
                $allowed = array_intersect($clients, $clientsInput);
38
            }
39
            foreach ($clients as $snc) {
40
                if (in_array($snc, $allowed)) {
41
                    $this->getContainer()->get($snc)->flushdb();
42
                    $output->writeln('redis database '.$snc.' flushed');
43
                }
44
            }
45
        }
46
    }
47
}
48