Passed
Push — master ( c6d84a...2633b8 )
by Dāvis
03:50
created

RedisFlushCommand::execute()   D

Complexity

Conditions 9
Paths 21

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 17
c 1
b 0
f 0
nc 21
nop 2
dl 0
loc 26
rs 4.909
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
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        $clients = [];
25
        $ids = $this->getContainer()->getServiceIds();
26
        /** @var $ids array */
27
        foreach ($ids as $id) {
28
            if (0 === strpos($id, 'snc_redis') && $this->getContainer()->get($id) instanceof Client) {
29
                $clients[] = $id;
30
            }
31
        }
32
33
        if (!empty($clients)) {
34
            $allowed = $clients;
35
            $clientsInput = $input->getArgument('clients');
36
            if (!empty($clientsInput)) {
37
                /** @var $clientsInput array */
38
                foreach ($clientsInput as &$client) {
39
                    $client = 'snc_redis.'.$client;
40
                }
41
                unset($client);
42
                $allowed = array_intersect($clients, $clientsInput);
43
            }
44
            foreach ($clients as $snc) {
45
                if (\in_array($snc, $allowed, true)) {
46
                    $this->getContainer()->get($snc)->flushdb();
47
                    $output->writeln('redis database '.$snc.' flushed');
48
                }
49
            }
50
        }
51
    }
52
}
53