Passed
Pull Request — 2.6 (#7494)
by Woody
08:09
created

MetadataCommand::execute()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 20.5013

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 16
nop 2
dl 0
loc 42
rs 8.0555
c 0
b 0
f 0
ccs 11
cts 23
cp 0.4783
crap 20.5013
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\Common\Cache\ApcCache;
23
use Doctrine\Common\Cache\ApcuCache;
24
use Doctrine\Common\Cache\XcacheCache;
25
use Symfony\Component\Console\Command\Command;
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 the metadata cache of the various cache drivers.
33
 *
34
 * @link    www.doctrine-project.org
35
 * @since   2.0
36
 * @author  Benjamin Eberlei <[email protected]>
37
 * @author  Guilherme Blanco <[email protected]>
38
 * @author  Jonathan Wage <[email protected]>
39
 * @author  Roman Borschel <[email protected]>
40
 */
41
class MetadataCommand extends Command
42
{
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    protected function configure()
47
    {
48 3
        $this->setName('orm:clear-cache:metadata')
49 3
             ->setDescription('Clear all metadata cache of the various cache drivers')
50 3
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.')
51 3
             ->setHelp(<<<EOT
52 3
The <info>%command.name%</info> command is meant to clear the metadata cache of associated Entity Manager.
53
It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
54
instance completely.
55
56
The execution type differ on how you execute the command.
57
If you want to invalidate the entries (and not delete from cache instance), this command would do the work:
58
59
<info>%command.name%</info>
60
61
Alternatively, if you want to flush the cache provider using this command:
62
63
<info>%command.name% --flush</info>
64
65
Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
66
because of a limitation of its execution nature.
67
EOT
68
             );
69 3
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 3
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76 3
        $ui = new SymfonyStyle($input, $output);
77
78 3
        $em = $this->getHelper('em')->getEntityManager();
79 3
        $cacheDriver = $em->getConfiguration()->getMetadataCacheImpl();
80
81 3
        if ( ! $cacheDriver) {
82
            throw new \InvalidArgumentException('No Metadata cache driver is configured on given EntityManager.');
83
        }
84
85 3
        if ($cacheDriver instanceof ApcCache) {
86 1
            throw new \LogicException("Cannot clear APC Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.");
87
        }
88
89 2
        if ($cacheDriver instanceof ApcuCache) {
90 1
            throw new \LogicException("Cannot clear APCu Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.");
91
        }
92
93 1
        if ($cacheDriver instanceof XcacheCache) {
94 1
            throw new \LogicException("Cannot clear XCache Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.");
95
        }
96
97
        $ui->comment('Clearing <info>all</info> Metadata cache entries');
98
99
        $result  = $cacheDriver->deleteAll();
100
        $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
101
102
        if (true === $input->getOption('flush')) {
103
            $result  = $cacheDriver->flushAll();
104
            $message = ($result) ? 'Successfully flushed cache entries.' : $message;
105
        }
106
107
        if ( ! $result) {
108
            $ui->error($message);
109
110
            return 1;
111
        }
112
113
        $ui->success($message);
114
115
        return 0;
116
    }
117
}
118