Completed
Push — master ( 36e8c5...2454a8 )
by Tom
12:20 queued 07:25
created

ListCommand::findInstalledModules()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 13
nc 9
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Developer\Module;
4
5
use Mage;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use N98\Util\ArrayFunctions;
8
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
9
use N98\Util\Console\Helper\TableHelper;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class ListCommand extends AbstractMagentoCommand
15
{
16 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...
17
    {
18
        $this
19
            ->setName('dev:module:list')
20
            ->addOption('codepool', null, InputOption::VALUE_OPTIONAL, 'Show modules in a specific codepool')
21
            ->addOption('status', null, InputOption::VALUE_OPTIONAL, 'Show modules with a specific status')
22
            ->addOption('vendor', null, InputOption::VALUE_OPTIONAL, 'Show modules of a specified vendor')
23
            ->setAliases(array('sys:modules:list'))// deprecated
24
            ->setDescription('List all installed modules')
25
            ->addOption(
26
                'format',
27
                null,
28
                InputOption::VALUE_OPTIONAL,
29
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
30
            );
31
    }
32
33
    /**
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     *
37
     * @return int|void
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $this->detectMagento($output, true);
42
43
        if ($input->getOption('format') === null) {
44
            $this->writeSection($output, 'Magento Modules');
45
        }
46
        $this->initMagento();
47
        $modules = $this->findInstalledModules();
48
        $modules = $this->filterModules($modules, $input);
49
50
        if (empty($modules)) {
51
            $output->writeln("No modules match the specified criteria.");
52
53
            return;
54
        }
55
56
        /** @var TableHelper $table */
57
        $table = $this->getHelper('table');
58
        $table
59
            ->setHeaders(array('Code pool', 'Name', 'Version', 'Status'))
60
            ->renderByFormat($output, $modules, $input->getOption('format'));
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    private function findInstalledModules()
67
    {
68
        $return = array();
69
70
        $modules = Mage::app()->getConfig()->getNode('modules')->asArray();
71
        foreach ($modules as $moduleName => $moduleInfo) {
72
            $codePool = isset($moduleInfo['codePool']) ? $moduleInfo['codePool'] : '';
73
            $version = isset($moduleInfo['version']) ? $moduleInfo['version'] : '';
74
            $active = isset($moduleInfo['active']) ? $moduleInfo['active'] : '';
75
76
            $return[] = array(
77
                'Code pool' => trim($codePool),
78
                'Name'      => trim($moduleName),
79
                'Version'   => trim($version),
80
                'Status'    => $this->formatActive($active),
81
            );
82
        }
83
84
        return $return;
85
    }
86
87
    /**
88
     * Filter modules by codepool, status and vendor if such options were inputted by user
89
     *
90
     * @param array $modules
91
     * @param InputInterface $input
92
     * @return array
93
     */
94
    private function filterModules(array $modules, InputInterface $input)
95
    {
96
        if ($input->getOption('codepool')) {
97
            $modules = ArrayFunctions::matrixFilterByValue($modules, "codePool", $input->getOption('codepool'));
98
        }
99
100
        if ($input->getOption('status')) {
101
            $modules = ArrayFunctions::matrixFilterByValue($modules, 'Status', $input->getOption('status'));
102
        }
103
104
        if ($input->getOption('vendor')) {
105
            $modules = ArrayFunctions::matrixFilterStartswith($modules, 'Name', $input->getOption('vendor'));
106
        }
107
108
        return $modules;
109
    }
110
}
111