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

CollectionRegionCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 18.35 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 20
loc 109
rs 10
c 0
b 0
f 0
ccs 45
cts 50
cp 0.9
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
C execute() 20 65 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 collection cache region.
33
 *
34
 * @since   2.5
35
 * @author  Fabio B. Silva <[email protected]>
36
 */
37
class CollectionRegionCommand extends Command
38
{
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    protected function configure()
43
    {
44 4
        $this->setName('orm:clear-cache:region:collection')
45 4
             ->setDescription('Clear a second-level cache collection region')
46 4
             ->addArgument('owner-class', InputArgument::OPTIONAL, 'The owner entity name.')
47 4
             ->addArgument('association', InputArgument::OPTIONAL, 'The association collection name.')
48 4
             ->addArgument('owner-id', InputArgument::OPTIONAL, 'The owner identifier.')
49 4
             ->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.')
50 4
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
51 4
             ->setHelp(<<<EOT
52 4
The <info>%command.name%</info> command is meant to clear a second-level cache collection regions for an associated Entity Manager.
53
It is possible to delete/invalidate all collection region, a specific collection region or flushes the cache provider.
54
55
The execution type differ on how you execute the command.
56
If you want to invalidate all entries for an collection region this command would do the work:
57
58
<info>%command.name% 'Entities\MyEntity' 'collectionName'</info>
59
60
To invalidate a specific entry you should use :
61
62
<info>%command.name% 'Entities\MyEntity' 'collectionName' 1</info>
63
64
If you want to invalidate all entries for the all collection regions:
65
66
<info>%command.name% --all</info>
67
68
Alternatively, if you want to flush the configured cache provider for an collection region use this command:
69
70
<info>%command.name% 'Entities\MyEntity' 'collectionName' --flush</info>
71
72
Finally, be aware that if <info>--flush</info> option is passed,
73
not all cache providers are able to flush entries, because of a limitation of its execution nature.
74
EOT
75
             );
76 4
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 4
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83 4
        $ui = new SymfonyStyle($input, $output);
84
85 4
        $em          = $this->getHelper('em')->getEntityManager();
86 4
        $ownerClass  = $input->getArgument('owner-class');
87 4
        $assoc       = $input->getArgument('association');
88 4
        $ownerId     = $input->getArgument('owner-id');
89 4
        $cache       = $em->getCache();
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 (( ! $ownerClass || ! $assoc) && ! $input->getOption('all')) {
96
            throw new \InvalidArgumentException('Missing arguments "--owner-class" "--association"');
97
        }
98
99 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...
100 1
            $collectionRegion  = $cache->getCollectionCacheRegion($ownerClass, $assoc);
101
102 1
            if ( ! $collectionRegion instanceof DefaultRegion) {
103
                throw new \InvalidArgumentException(sprintf(
104
                    'The option "--flush" expects a "Doctrine\ORM\Cache\Region\DefaultRegion", but got "%s".',
105
                    is_object($collectionRegion) ? get_class($collectionRegion) : gettype($collectionRegion)
106
                ));
107
            }
108
109 1
            $collectionRegion->getCache()->flushAll();
110
111 1
            $ui->comment(
112 1
                sprintf(
113 1
                    'Flushing cache provider configured for <info>"%s#%s"</info>',
114 1
                    $ownerClass,
115 1
                    $assoc
116
                )
117
            );
118
119 1
            return;
120
        }
121
122 3
        if ($input->getOption('all')) {
123 1
            $ui->comment('Clearing <info>all</info> second-level cache collection regions');
124
125 1
            $cache->evictEntityRegions();
126
127 1
            return;
128
        }
129
130 2
        if ($ownerId) {
131 1
            $ui->comment(
132 1
                sprintf(
133 1
                    'Clearing second-level cache entry for collection <info>"%s#%s"</info> owner entity identified by <info>"%s"</info>',
134 1
                    $ownerClass,
135 1
                    $assoc,
136 1
                    $ownerId
137
                )
138
            );
139 1
            $cache->evictCollection($ownerClass, $assoc, $ownerId);
140
141 1
            return;
142
        }
143
144 1
        $ui->comment(sprintf('Clearing second-level cache for collection <info>"%s#%s"</info>', $ownerClass, $assoc));
145 1
        $cache->evictCollectionRegion($ownerClass, $assoc);
146 1
    }
147
}
148