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

ReportCommand::execute()   C

Complexity

Conditions 14
Paths 58

Size

Total Lines 53
Code Lines 33

Duplication

Lines 8
Ratio 15.09 %
Metric Value
dl 8
loc 53
rs 6.3132
cc 14
eloc 33
nc 58
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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