Failed Conditions
Push — 2.7 ( c036c0...266f0d )
by Jonathan
57:23 queued 50:07
created

Console/Command/ClearCache/QueryRegionCommand.php (2 issues)

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
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
112
                )
113
            );
114
115 1
            return;
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;
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
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 2
    }
129
}
130