Passed
Pull Request — 2.6 (#7899)
by Andreas
07:29
created

QueryRegionCommand::execute()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.1284

Importance

Changes 0
Metric Value
cc 7
eloc 28
c 0
b 0
f 0
nc 10
nop 2
dl 0
loc 51
ccs 25
cts 29
cp 0.8621
crap 7.1284
rs 8.5386

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;
21
22
use Doctrine\ORM\Cache;
23
use Doctrine\ORM\Cache\Region\DefaultRegion;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
31
/**
32
 * Command to clear a query cache region.
33
 *
34
 * @since   2.5
35
 * @author  Fabio B. Silva <[email protected]>
36
 */
37
class QueryRegionCommand extends Command
38
{
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    protected function configure()
43
    {
44 4
        $this->setName('orm:clear-cache:region:query')
45 4
             ->setDescription('Clear a second-level cache query region')
46 4
             ->addArgument('region-name', InputArgument::OPTIONAL, 'The query region to clear.')
47 4
             ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all query regions will be deleted/invalidated.')
48 4
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
49 4
             ->setHelp(<<<EOT
50 4
The <info>%command.name%</info> command is meant to clear a second-level cache query region for an associated Entity Manager.
51
It is possible to delete/invalidate all query region, a specific query region or flushes the cache provider.
52
53
The execution type differ on how you execute the command.
54
If you want to invalidate all entries for the default query region this command would do the work:
55
56
<info>%command.name%</info>
57
58
To invalidate entries for a specific query region you should use :
59
60
<info>%command.name% my_region_name</info>
61
62
If you want to invalidate all entries for the all query region:
63
64
<info>%command.name% --all</info>
65
66
Alternatively, if you want to flush the configured cache provider use this command:
67
68
<info>%command.name% my_region_name --flush</info>
69
70
Finally, be aware that if <info>--flush</info> option is passed,
71
not all cache providers are able to flush entries, because of a limitation of its execution nature.
72
EOT
73
             );
74 4
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81 4
        $ui = new SymfonyStyle($input, $output);
82
83 4
        $em    = $this->getHelper('em')->getEntityManager();
84 4
        $name  = $input->getArgument('region-name');
85 4
        $cache = $em->getCache();
86
87 4
        if ($name === null) {
88 2
            $name = Cache::DEFAULT_QUERY_REGION_NAME;
89
        }
90
91 4
        if ( ! $cache instanceof Cache) {
92
            throw new \InvalidArgumentException('No second-level cache is configured on the given EntityManager.');
93
        }
94
95 4
        if ($input->getOption('flush')) {
96 1
            $queryCache  = $cache->getQueryCache($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string[]; however, parameter $regionName of Doctrine\ORM\Cache::getQueryCache() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

96
            $queryCache  = $cache->getQueryCache(/** @scrutinizer ignore-type */ $name);
Loading history...
97 1
            $queryRegion = $queryCache->getRegion();
98
99 1
            if ( ! $queryRegion instanceof DefaultRegion) {
100
                throw new \InvalidArgumentException(sprintf(
101
                    'The option "--flush" expects a "Doctrine\ORM\Cache\Region\DefaultRegion", but got "%s".',
102
                    is_object($queryRegion) ? get_class($queryRegion) : gettype($queryRegion)
103
                ));
104
            }
105
106 1
            $queryRegion->getCache()->flushAll();
107
108 1
            $ui->comment(
109 1
                sprintf(
110 1
                    'Flushing cache provider configured for second-level cache query region named <info>"%s"</info>',
111 1
                    $name
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

111
                    /** @scrutinizer ignore-type */ $name
Loading history...
112
                )
113
            );
114
115 1
            return 0;
116
        }
117
118 3
        if ($input->getOption('all')) {
119 1
            $ui->comment('Clearing <info>all</info> second-level cache query regions');
120
121 1
            $cache->evictQueryRegions();
122
123 1
            return 0;
124
        }
125
126 2
        $ui->comment(sprintf('Clearing second-level cache query region named <info>"%s"</info>', $name));
127 2
        $cache->evictQueryRegion($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string[]; however, parameter $regionName of Doctrine\ORM\Cache::evictQueryRegion() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

127
        $cache->evictQueryRegion(/** @scrutinizer ignore-type */ $name);
Loading history...
128
129 2
        return 0;
130
    }
131
}
132