Completed
Pull Request — develop (#891)
by Luke
03:48
created

ListMviewCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 30
nc 4
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Indexer;
4
5
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class ListMviewCommand extends AbstractMviewIndexerCommand
11
{
12 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
13
    {
14
        $this
15
            ->setName('index:list:mview')
16
            ->setDescription('Lists all magento mview indexes')
17
            ->addOption(
18
                'format',
19
                null,
20
                InputOption::VALUE_OPTIONAL,
21
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
22
            )
23
        ;
24
25
        $help = <<<HELP
26
Lists all Magento mview indexers of current installation.
27
HELP;
28
        $this->setHelp($help);
29
    }
30
31
    /**
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     *
35
     * @return int|void
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->detectMagento($output, true);
40
        if (!$this->initMagento()) {
41
            return;
42
        }
43
44
        $table = array();
45
        foreach ($this->getMetaDataCollection() as $index) {
46
            $changelogName = $index->getData('changelog_name');
47
            $versionId = $index->getData('version_id');
48
            $pendingCount = $this->getPendingChangelogsCount($changelogName, $versionId);
49
            if ($pendingCount > 0) {
50
                $pendingString = "<error>$pendingCount</error>";
51
            } else {
52
                $pendingString = "<info>$pendingCount</info>";
53
            }
54
55
            $table[] = array(
56
                $index->getData('table_name'),
57
                $index->getData('view_name'),
58
                $changelogName,
59
                $index->getData('status'),
60
                $versionId,
61
                $pendingString,
62
            );
63
        }
64
65
        $this->getHelper('table')
66
            ->setHeaders(
67
                array(
68
                    'table_name',
69
                    'view_name',
70
                    'changelog_name',
71
                    'status',
72
                    'version_id',
73
                    'entries pending reindex',
74
                )
75
            )
76
            ->renderByFormat($output, $table, $input->getOption('format'));
77
    }
78
79
    /**
80
     * @param $tableName
81
     * @param $currentVersionId
82
     * @return int
83
     */
84
    protected function getPendingChangelogsCount($tableName, $currentVersionId)
85
    {
86
        /** @var \Mage_Core_Model_Resource $resource */
87
        $resource = $this->_getSingleton('core/resource');
0 ignored issues
show
Bug introduced by
The call to _getSingleton() misses a required argument $mage2class.

This check looks for function calls that miss required arguments.

Loading history...
88
        $readConnection = $resource->getConnection('core_read');
89
90
        $select = $readConnection->select()
91
            ->from($tableName, array('count(*)'))
92
            ->where("version_id > ?", $currentVersionId);
93
        $todoCount = $readConnection->fetchOne($select);
94
95
        return (int) $todoCount;
96
    }
97
}
98