Completed
Push — develop ( d89a13...8e8026 )
by Tom
15s
created

ListCommand::findInstalledModules()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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