Completed
Push — develop ( 531ade...a739c1 )
by Tom
04:50
created

CleanCommand::execute()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 14
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Cache;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class CleanCommand extends AbstractCacheCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('cache:clean')
16
            ->addArgument('type', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Cache type code like "config"')
17
            ->addOption(
18
                'reinit',
19
                null,
20
                InputOption::VALUE_NONE,
21
                'Reinitialise the config cache after cleaning'
22
            )
23
            ->addOption(
24
                'no-reinit',
25
                null,
26
                InputOption::VALUE_NONE,
27
                "Don't reinitialise the config cache after flushing"
28
            )
29
            ->setDescription('Clean magento cache')
30
        ;
31
32
        $help = <<<HELP
33
Cleans expired cache entries.
34
35
If you would like to clean only one cache type use like:
36
37
   $ n98-magerun.phar cache:clean full_page
38
39
If you would like to clean multiple cache types at once use like:
40
41
   $ n98-magerun.phar cache:clean full_page block_html
42
43
If you would like to remove all cache entries use `cache:flush`
44
45
Options:
46
    --reinit Reinitialise the config cache after cleaning (Default)
47
    --no-reinit Don't reinitialise the config cache after cleaning
48
HELP;
49
        $this->setHelp($help);
50
    }
51
52
    /**
53
     * @param InputInterface  $input
54
     * @param OutputInterface $output
55
     *
56
     * @return int|void
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $noReinitOption = $input->getOption('no-reinit');
61
        if (!$noReinitOption) {
62
            $this->banUseCache();
63
        }
64
65
        $this->detectMagento($output, true);
66
        if (!$this->initMagento()) {
67
            return;
68
        }
69
70
        \Mage::app()->loadAreaPart('adminhtml', 'events');
71
        $allTypes = \Mage::app()->getCacheInstance()->getTypes();
72
        $typesToClean = $input->getArgument('type');
73
        $this->validateCacheCodes($typesToClean);
74
        $typeKeys = array_keys($allTypes);
75
76
        foreach ($typeKeys as $type) {
77
            if (count($typesToClean) == 0 || in_array($type, $typesToClean)) {
78
                \Mage::app()->getCacheInstance()->cleanType($type);
79
                \Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
80
                $output->writeln('<info>Cache <comment>' . $type . '</comment> cleaned</info>');
81
            }
82
        }
83
84
        if (!$noReinitOption) {
85
            $this->reinitCache();
86
        }
87
    }
88
}
89