Completed
Push — develop ( b3a7ba...37ddcf )
by Tom
16:38
created

Modules::filterModules()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 11
nc 8
nop 1
1
<?php
2
/**
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Magento;
9
10
use ArrayIterator;
11
use Countable;
12
use IteratorAggregate;
13
use Mage;
14
use N98\Util\ArrayFunctions;
15
use N98\Util\StringTyped;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Traversable;
18
19
/**
20
 * Magento Modules
21
 *
22
 * @package N98\Magento
23
 */
24
class Modules implements IteratorAggregate, Countable
25
{
26
    /**
27
     * @var array
28
     */
29
    private $list;
30
31
    public function __construct(array $list = null)
32
    {
33
        if (null === $list) {
34
            $list = array();
35
        }
36
37
        $this->list = $list;
38
    }
39
40
    /**
41
     * @return Modules
42
     */
43
    public function findInstalledModules()
44
    {
45
        $list = array();
46
47
        $modules = Mage::app()->getConfig()->getNode('modules')->asArray();
48
        foreach ($modules as $moduleName => $moduleInfo) {
49
            $codePool = isset($moduleInfo['codePool']) ? $moduleInfo['codePool'] : '';
50
            $version = isset($moduleInfo['version']) ? $moduleInfo['version'] : '';
51
            $active = isset($moduleInfo['active']) ? $moduleInfo['active'] : '';
52
53
            $list[] = array(
54
                'Code pool' => trim($codePool),
55
                'Name'      => trim($moduleName),
56
                'Version'   => trim($version),
57
                'Status'    => StringTyped::formatActive($active),
58
            );
59
        }
60
61
        $installed = new Modules();
62
        $installed->list = $list;
63
64
        return $installed;
65
    }
66
67
    /**
68
     * Filter modules by codepool, status and vendor if such options were inputted by user
69
     *
70
     * @param InputInterface $input
71
     * @return Modules
72
     */
73
    public function filterModules(InputInterface $input)
74
    {
75
        $list = $this->list;
76
77
        if ($input->getOption('codepool')) {
78
            $list = ArrayFunctions::matrixFilterByValue($list, "Code pool", $input->getOption('codepool'));
79
        }
80
81
        if ($input->getOption('status')) {
82
            $list = ArrayFunctions::matrixFilterByValue($list, 'Status', $input->getOption('status'));
83
        }
84
85
        if ($input->getOption('vendor')) {
86
            $list = ArrayFunctions::matrixFilterStartswith($list, 'Name', $input->getOption('vendor'));
87
        }
88
89
        $filtered = new self();
90
        $filtered->list = $list;
91
92
        return $filtered;
93
    }
94
95
    ### Traversable Interface ###
96
97
    /**
98
     * Retrieve an external iterator
99
     *
100
     * @return Traversable|array[]
101
     */
102
    public function getIterator()
103
    {
104
        return new ArrayIterator($this->list);
105
    }
106
107
    ### Countable Interface ###
108
109
    /**
110
     * Count elements of an object
111
     *
112
     * @return int The custom count as an integer.
113
     */
114
    public function count()
115
    {
116
        return count($this->list);
117
    }
118
}
119