Failed Conditions
Pull Request — 2.6 (#7180)
by Ben
11:16
created

ResultCommand::execute()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

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