ReportCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
namespace N98\Magento\Command\Cache;
4
5
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
6
use N98\Util\Console\Helper\TableHelper;
7
use RuntimeException;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ReportCommand extends AbstractCacheCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('cache:report')
18
            ->setDescription('View inside the cache')
19
            ->addOption('tags', 't', InputOption::VALUE_NONE, 'Output tags')
20
            ->addOption('mtime', 'm', InputOption::VALUE_NONE, 'Output last modification time')
21
            ->addOption('filter-id', '', InputOption::VALUE_OPTIONAL, 'Filter output by ID (substring)')
22
            ->addOption(
23
                'filter-tag',
24
                '',
25
                InputOption::VALUE_OPTIONAL,
26
                'Filter output by TAG (seperate multiple tags by comma)'
27
            )
28
            ->addOption(
29
                'fpc',
30
                null,
31
                InputOption::VALUE_NONE,
32
                'Use full page cache instead of core cache (Enterprise only!)'
33
            )
34
            ->addOption(
35
                'format',
36
                null,
37
                InputOption::VALUE_OPTIONAL,
38
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
39
            )
40
        ;
41
    }
42
43
    protected function isTagFiltered($metaData, $input)
44
    {
45
        return (bool) count(array_intersect($metaData['tags'], explode(',', $input->getOption('filter-tag'))));
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     * @throws RuntimeException
52
     * @return int|void
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $this->detectMagento($output, true);
57
        if (!$this->initMagento()) {
58
            return;
59
        }
60
61 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...
62
            if (!class_exists('\Enterprise_PageCache_Model_Cache')) {
63
                throw new RuntimeException('Enterprise page cache not found');
64
            }
65
            $cacheInstance = \Enterprise_PageCache_Model_Cache::getCacheInstance()->getFrontend();
66
        } else {
67
            $cacheInstance = \Mage::app()->getCache();
68
        }
69
        /* @var $cacheInstance \Varien_Cache_Core */
70
        $cacheIds = $cacheInstance->getIds();
71
        $table = array();
72
        foreach ($cacheIds as $cacheId) {
73
            if ($input->getOption('filter-id') !== null && !stristr($cacheId, $input->getOption('filter-id'))) {
74
                continue;
75
            }
76
77
            $metaData = $cacheInstance->getMetadatas($cacheId);
78
            if ($input->getOption('filter-tag') !== null && !$this->isTagFiltered($metaData, $input)) {
79
                continue;
80
            }
81
82
            $row = array(
83
                $cacheId,
84
                date('Y-m-d H:i:s', $metaData['expire']),
85
            );
86
            if ($input->getOption('mtime')) {
87
                $row[] = date('Y-m-d H:i:s', $metaData['mtime']);
88
            }
89
            if ($input->getOption('tags')) {
90
                $row[] = implode(',', $metaData['tags']);
91
            }
92
93
            $table[] = $row;
94
        }
95
96
        $headers = array('ID', 'EXPIRE');
97
        if ($input->getOption('mtime')) {
98
            $headers[] = 'MTIME';
99
        }
100
        if ($input->getOption('tags')) {
101
            $headers[] = 'TAGS';
102
        }
103
104
        /* @var $tableHelper TableHelper */
105
        $tableHelper = $this->getHelper('table');
106
        $tableHelper
107
            ->setHeaders($headers)
108
            ->renderByFormat($output, $table, $input->getOption('format'));
109
    }
110
}
111