Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

EntityRegionCommand::execute()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 57
Code Lines 32

Duplication

Lines 16
Ratio 28.07 %

Code Coverage

Tests 28
CRAP Score 9.2815

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 4
nop 2
dl 16
loc 57
rs 7.0745
c 0
b 0
f 0
ccs 28
cts 33
cp 0.8485
crap 9.2815

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 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 View Code Duplication
        if ($input->getOption('flush')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98 1
            $entityRegion  = $cache->getEntityCacheRegion($entityClass);
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);
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);
137 1
    }
138
}
139