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

Console/Command/ClearCache/EntityRegionCommand.php (3 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 entity cache region.
33
 *
34
 * @since   2.5
35
 * @author  Fabio B. Silva <[email protected]>
36
 */
37
class EntityRegionCommand extends Command
38
{
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    protected function configure()
43
    {
44 4
        $this->setName('orm:clear-cache:region:entity')
45 4
             ->setDescription('Clear a second-level cache entity region')
46 4
             ->addArgument('entity-class', InputArgument::OPTIONAL, 'The entity name.')
47 4
             ->addArgument('entity-id', InputArgument::OPTIONAL, 'The entity identifier.')
48 4
             ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.')
49 4
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
50 4
             ->setHelp(<<<EOT
51 4
The <info>%command.name%</info> command is meant to clear a second-level cache entity region for an associated Entity Manager.
52
It is possible to delete/invalidate all entity region, a specific entity region or flushes the cache provider.
53
54
The execution type differ on how you execute the command.
55
If you want to invalidate all entries for an entity region this command would do the work:
56
57
<info>%command.name% 'Entities\MyEntity'</info>
58
59
To invalidate a specific entry you should use :
60
61
<info>%command.name% 'Entities\MyEntity' 1</info>
62
63
If you want to invalidate all entries for the all entity regions:
64
65
<info>%command.name% --all</info>
66
67
Alternatively, if you want to flush the configured cache provider for an entity region use this command:
68
69
<info>%command.name% 'Entities\MyEntity' --flush</info>
70
71
Finally, be aware that if <info>--flush</info> option is passed,
72
not all cache providers are able to flush entries, because of a limitation of its execution nature.
73
EOT
74
             );
75 4
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 4
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82 4
        $ui = new SymfonyStyle($input, $output);
83
84 4
        $em          = $this->getHelper('em')->getEntityManager();
85 4
        $entityClass = $input->getArgument('entity-class');
86 4
        $entityId    = $input->getArgument('entity-id');
87 4
        $cache       = $em->getCache();
88
89 4
        if ( ! $cache instanceof Cache) {
90
            throw new \InvalidArgumentException('No second-level cache is configured on the given EntityManager.');
91
        }
92
93 4
        if ( ! $entityClass && ! $input->getOption('all')) {
94
            throw new \InvalidArgumentException('Invalid argument "--entity-class"');
95
        }
96
97 4
        if ($input->getOption('flush')) {
98 1
            $entityRegion  = $cache->getEntityCacheRegion($entityClass);
0 ignored issues
show
It seems like $entityClass can also be of type string[]; however, parameter $className of Doctrine\ORM\Cache::getEntityCacheRegion() 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

98
            $entityRegion  = $cache->getEntityCacheRegion(/** @scrutinizer ignore-type */ $entityClass);
Loading history...
99
100 1
            if ( ! $entityRegion instanceof DefaultRegion) {
101
                throw new \InvalidArgumentException(sprintf(
102
                    'The option "--flush" expects a "Doctrine\ORM\Cache\Region\DefaultRegion", but got "%s".',
103
                    is_object($entityRegion) ? get_class($entityRegion) : gettype($entityRegion)
104
                ));
105
            }
106
107 1
            $entityRegion->getCache()->flushAll();
108
109 1
            $ui->comment(sprintf('Flushing cache provider configured for entity named <info>"%s"</info>', $entityClass));
110
111 1
            return;
112
        }
113
114 3
        if ($input->getOption('all')) {
115 1
            $ui->comment('Clearing <info>all</info> second-level cache entity regions');
116
117 1
            $cache->evictEntityRegions();
118
119 1
            return;
120
        }
121
122 2
        if ($entityId) {
123 1
            $ui->comment(
124 1
                sprintf(
125 1
                    'Clearing second-level cache entry for entity <info>"%s"</info> identified by <info>"%s"</info>',
126 1
                    $entityClass,
127 1
                    $entityId
128
                )
129
            );
130 1
            $cache->evictEntity($entityClass, $entityId);
0 ignored issues
show
It seems like $entityClass can also be of type string[]; however, parameter $className of Doctrine\ORM\Cache::evictEntity() 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

130
            $cache->evictEntity(/** @scrutinizer ignore-type */ $entityClass, $entityId);
Loading history...
131
132 1
            return;
133
        }
134
135 1
        $ui->comment(sprintf('Clearing second-level cache for entity <info>"%s"</info>', $entityClass));
136 1
        $cache->evictEntityRegion($entityClass);
0 ignored issues
show
It seems like $entityClass can also be of type string[]; however, parameter $className of Doctrine\ORM\Cache::evictEntityRegion() 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

136
        $cache->evictEntityRegion(/** @scrutinizer ignore-type */ $entityClass);
Loading history...
137 1
    }
138
}
139