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