Passed
Pull Request — master (#7448)
by Ilya
14:31
created

QueryRegionCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 89.19%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 91
ccs 33
cts 37
cp 0.8919
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 49 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;
6
7
use Doctrine\ORM\Cache;
8
use Doctrine\ORM\Cache\Region\DefaultRegion;
9
use InvalidArgumentException;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
use function get_class;
17
use function gettype;
18
use function is_object;
19
use function sprintf;
20
21
/**
22
 * Command to clear a query cache region.
23
 */
24
class QueryRegionCommand extends Command
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 4
    protected function configure()
30
    {
31 4
        $this->setName('orm:clear-cache:region:query')
32 4
             ->setDescription('Clear a second-level cache query region')
33 4
             ->addArgument('region-name', InputArgument::OPTIONAL, 'The query region to clear.')
34 4
             ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all query regions will be deleted/invalidated.')
35 4
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
36 4
             ->setHelp(<<<'EOT'
37 4
The <info>%command.name%</info> command is meant to clear a second-level cache query region for an associated Entity Manager.
38
It is possible to delete/invalidate all query region, a specific query region or flushes the cache provider.
39
40
The execution type differ on how you execute the command.
41
If you want to invalidate all entries for the default query region this command would do the work:
42
43
<info>%command.name%</info>
44
45
To invalidate entries for a specific query region you should use :
46
47
<info>%command.name% my_region_name</info>
48
49
If you want to invalidate all entries for the all query region:
50
51
<info>%command.name% --all</info>
52
53
Alternatively, if you want to flush the configured cache provider use this command:
54
55
<info>%command.name% my_region_name --flush</info>
56
57
Finally, be aware that if <info>--flush</info> option is passed,
58
not all cache providers are able to flush entries, because of a limitation of its execution nature.
59
EOT
60
             );
61 4
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68 4
        $ui = new SymfonyStyle($input, $output);
69
70 4
        $em    = $this->getHelper('em')->getEntityManager();
71 4
        $name  = $input->getArgument('region-name');
72 4
        $cache = $em->getCache();
73
74 4
        if ($name === null) {
75 2
            $name = Cache::DEFAULT_QUERY_REGION_NAME;
76
        }
77
78 4
        if (! $cache instanceof Cache) {
79
            throw new InvalidArgumentException('No second-level cache is configured on the given EntityManager.');
80
        }
81
82 4
        if ($input->getOption('flush')) {
83 1
            $queryCache  = $cache->getQueryCache($name);
84 1
            $queryRegion = $queryCache->getRegion();
85
86 1
            if (! $queryRegion instanceof DefaultRegion) {
87
                throw new InvalidArgumentException(sprintf(
88
                    'The option "--flush" expects a "Doctrine\ORM\Cache\Region\DefaultRegion", but got "%s".',
89
                    is_object($queryRegion) ? get_class($queryRegion) : gettype($queryRegion)
90
                ));
91
            }
92
93 1
            $queryRegion->getCache()->flushAll();
94
95 1
            $ui->comment(
96 1
                sprintf(
97 1
                    'Flushing cache provider configured for second-level cache query region named <info>"%s"</info>',
98 1
                    $name
99
                )
100
            );
101
102 1
            return;
103
        }
104
105 3
        if ($input->getOption('all')) {
106 1
            $ui->comment('Clearing <info>all</info> second-level cache query regions');
107
108 1
            $cache->evictQueryRegions();
109
110 1
            return;
111
        }
112
113 2
        $ui->comment(sprintf('Clearing second-level cache query region named <info>"%s"</info>', $name));
114 2
        $cache->evictQueryRegion($name);
115 2
    }
116
}
117