CacheFlushAllCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 25 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\CacheBundle\Command;
15
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcher;
20
21
class CacheFlushAllCommand extends BaseCacheCommand
22
{
23
    public function configure(): void
24
    {
25
        $this->setName('sonata:cache:flush-all');
26
        $this->setDescription('Flush all information set in cache managers');
27
28
        $this->addOption(
29
            'cache',
30
            null,
31
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
32
            'Flush elements stored in given cache'
33
        );
34
    }
35
36
    public function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $output->writeln('<info>Clearing cache information.</info>');
39
40
        foreach ($this->getManager()->getCacheServices() as $name => $cache) {
41
            if ($input->getOption('cache') && !\in_array($name, $input->getOption('cache'), true)) {
42
                continue;
43
            }
44
45
            $output->write(sprintf(' > %s : starting .... ', $name));
46
47
            if (true === $cache->flushAll()) {
48
                $output->writeln('<info>Ok</info>');
49
            } else {
50
                $output->writeln('<error>Failed!</error>');
51
            }
52
        }
53
54
        // The current event dispatcher is stale, let's not use it anymore
55
        $this->getApplication()->setDispatcher(new EventDispatcher());
56
57
        $output->writeln('<info>Done!</info>');
58
59
        return 0;
60
    }
61
}
62