Completed
Push — master ( 2454a8...540363 )
by Tom
15:51
created

Modules::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
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
                'codePool' => trim($codePool),
55
                'Name'     => trim($moduleName),
56
                'Version'  => trim($version),
57
                'Status'   => StringTyped::formatActive($active),
58
            );
59
        }
60
61
        return new Modules($list);
62
    }
63
64
    /**
65
     * Filter modules by codepool, status and vendor if such options were inputted by user
66
     *
67
     * @param InputInterface $input
68
     * @return Modules
69
     */
70
    public function filterModules(InputInterface $input)
71
    {
72
        $filtered = $this->list;
73
74
        if ($input->getOption('codepool')) {
75
            $filtered = ArrayFunctions::matrixFilterByValue($filtered, "codePool", $input->getOption('codepool'));
76
        }
77
78
        if ($input->getOption('status')) {
79
            $filtered = ArrayFunctions::matrixFilterByValue($filtered, 'Status', $input->getOption('status'));
80
        }
81
82
        if ($input->getOption('vendor')) {
83
            $filtered = ArrayFunctions::matrixFilterStartswith($filtered, 'Name', $input->getOption('vendor'));
84
        }
85
86
        return new self($filtered);
87
    }
88
89
    ### Traversable Interface ###
90
91
    /**
92
     * Retrieve an external iterator
93
     *
94
     * @return Traversable|array[]
95
     */
96
    public function getIterator()
97
    {
98
        return new ArrayIterator($this->list);
99
    }
100
101
    ### Countable Interface ###
102
103
    /**
104
     * Count elements of an object
105
     *
106
     * @return int The custom count as an integer.
107
     */
108
    public function count()
109
    {
110
        return count($this->list);
111
    }
112
}
113