Completed
Push — develop ( 6f71dd...40edc3 )
by Tom
11s
created

ReportCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Cache;
4
5
use RuntimeException;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
10
11
class ReportCommand extends AbstractCacheCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('cache:report')
17
            ->setDescription('View inside the cache')
18
            ->addOption('tags', 't', InputOption::VALUE_NONE, 'Output tags')
19
            ->addOption('mtime', 'm', InputOption::VALUE_NONE, 'Output last modification time')
20
            ->addOption('filter-id', '', InputOption::VALUE_OPTIONAL, 'Filter output by ID (substring)')
21
            ->addOption(
22
                'filter-tag',
23
                '',
24
                InputOption::VALUE_OPTIONAL,
25
                'Filter output by TAG (seperate multiple tags by comma)'
26
            )
27
            ->addOption(
28
                'fpc',
29
                null,
30
                InputOption::VALUE_NONE,
31
                'Use full page cache instead of core cache (Enterprise only!)'
32
            )
33
            ->addOption(
34
                'format',
35
                null,
36
                InputOption::VALUE_OPTIONAL,
37
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
38
            )
39
        ;
40
    }
41
42
    protected function isTagFiltered($metaData, $input)
43
    {
44
        return (bool) count(array_intersect($metaData['tags'], explode(',', $input->getOption('filter-tag'))));
45
    }
46
47
    /**
48
     * @param InputInterface $input
49
     * @param OutputInterface $output
50
     * @throws RuntimeException
51
     * @return int|void
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $this->detectMagento($output, true);
56
        if ($this->initMagento()) {
57 View Code Duplication
            if ($input->hasOption('fpc') && $input->getOption('fpc')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
                if (!class_exists('\Enterprise_PageCache_Model_Cache')) {
59
                    throw new RuntimeException('Enterprise page cache not found');
60
                }
61
                $cacheInstance = \Enterprise_PageCache_Model_Cache::getCacheInstance()->getFrontend();
62
            } else {
63
                $cacheInstance = \Mage::app()->getCache();
64
            }
65
            /* @var $cacheInstance \Varien_Cache_Core */
66
            $cacheIds = $cacheInstance->getIds();
67
            $table = array();
68
            foreach ($cacheIds as $cacheId) {
69
                if ($input->getOption('filter-id') !== null && !stristr($cacheId, $input->getOption('filter-id'))) {
70
                    continue;
71
                }
72
73
74
                $metaData = $cacheInstance->getMetadatas($cacheId);
75
                if ($input->getOption('filter-tag') !== null && !$this->isTagFiltered($metaData, $input)) {
76
                    continue;
77
                }
78
79
                $row = array(
80
                    $cacheId,
81
                    date('Y-m-d H:i:s', $metaData['expire']),
82
                );
83
                if ($input->getOption('mtime')) {
84
                    $row[] = date('Y-m-d H:i:s', $metaData['mtime']);
85
                }
86
                if ($input->getOption('tags')) {
87
                    $row[] = implode(',', $metaData['tags']);
88
                }
89
90
                $table[] = $row;
91
            }
92
93
            $headers = array('ID', 'EXPIRE');
94
            if ($input->getOption('mtime')) {
95
                $headers[] = 'MTIME';
96
            }
97
            if ($input->getOption('tags')) {
98
                $headers[] = 'TAGS';
99
            }
100
101
            $this->getHelper('table')
102
                ->setHeaders($headers)
103
                ->renderByFormat($output, $table, $input->getOption('format'));
104
        }
105
    }
106
}
107