Failed Conditions
Push — master ( ddb3cd...4476ec )
by Marco
11:47
created

ResultCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
C execute() 0 38 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;
6
7
use Doctrine\Common\Cache\ApcCache;
8
use Doctrine\Common\Cache\XcacheCache;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
/**
16
 * Command to clear the result cache of the various cache drivers.
17
 */
18
class ResultCommand extends Command
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function configure()
24
    {
25
        $this->setName('orm:clear-cache:result')
26
             ->setDescription('Clear all result cache of the various cache drivers')
27
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.')
28
             ->setHelp(<<<'EOT'
29
The <info>%command.name%</info> command is meant to clear the result cache of associated Entity Manager.
30
It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
31
instance completely.
32
33
The execution type differ on how you execute the command.
34
If you want to invalidate the entries (and not delete from cache instance), this command would do the work:
35
36
<info>%command.name%</info>
37
38
Alternatively, if you want to flush the cache provider using this command:
39
40
<info>%command.name% --flush</info>
41
42
Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
43
because of a limitation of its execution nature.
44
EOT
45
             );
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $ui = new SymfonyStyle($input, $output);
54
55
        $em          = $this->getHelper('em')->getEntityManager();
56
        $cacheDriver = $em->getConfiguration()->getResultCacheImpl();
57
58
        if (! $cacheDriver) {
59
            throw new \InvalidArgumentException('No Result cache driver is configured on given EntityManager.');
60
        }
61
62
        if ($cacheDriver instanceof ApcCache) {
63
            throw new \LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
64
        }
65
66
        if ($cacheDriver instanceof XcacheCache) {
67
            throw new \LogicException('Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
68
        }
69
70
        $ui->comment('Clearing <info>all</info> Result cache entries');
71
72
        $result  = $cacheDriver->deleteAll();
73
        $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
74
75
        if ($input->getOption('flush') === true) {
76
            $result  = $cacheDriver->flushAll();
77
            $message = ($result) ? 'Successfully flushed cache entries.' : $message;
78
        }
79
80
        if (! $result) {
81
            $ui->error($message);
82
83
            return 1;
84
        }
85
86
        $ui->success($message);
87
88
        return 0;
89
    }
90
}
91