Completed
Push — master ( 202f3c...15bdf1 )
by Tom
09:14 queued 04:46
created

ListCommand::sanitizeModuleProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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('codePool', '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
            $this->infos[] = array(
67
                'codePool' => $this->sanitizeModuleProperty($moduleInfo['codePool']),
68
                'Name'     => $this->sanitizeModuleProperty($moduleName),
69
                'Version'  => isset($moduleInfo['version'])
70
                    ? $this->sanitizeModuleProperty($moduleInfo['version'])
71
                    : '',
72
                'Status'   => $this->formatActive($moduleInfo['active']),
73
            );
74
        }
75
    }
76
77
    /**
78
     * Filter modules by codepool, status and vendor if such options were inputted by user
79
     *
80
     * @param InputInterface $input
81
     */
82
    protected function filterModules(InputInterface $input)
83
    {
84
        if ($input->getOption('codepool')) {
85
            $this->filterByField("codePool", $input->getOption('codepool'));
86
        }
87
88
        if ($input->getOption('status')) {
89
            $this->filterByField('Status', $input->getOption('status'));
90
        }
91
92
        if ($input->getOption('vendor')) {
93
            $this->filterByFieldStartsWith('Name', $input->getOption('vendor'));
94
        }
95
    }
96
97
    /**
98
     * @param string $field
99
     * @param string $value
100
     */
101
    protected function filterByField($field, $value)
102
    {
103
        foreach ($this->infos as $k => $info) {
104
            if ($info[$field] != $value) {
105
                unset($this->infos[$k]);
106
            }
107
        }
108
    }
109
110
    /**
111
     * @param string $field
112
     * @param string $value
113
     */
114
    protected function filterByFieldStartsWith($field, $value)
115
    {
116
        foreach ($this->infos as $k => $info) {
117
            if (strncmp($info[$field], $value, strlen($value))) {
118
                unset($this->infos[$k]);
119
            }
120
        }
121
    }
122
123
    /**
124
     * @param string $input Module property to be sanitized
125
     *
126
     * @return string
127
     */
128
    private function sanitizeModuleProperty($input)
129
    {
130
        return trim($input);
131
    }
132
}
133