Completed
Push — master ( d985d2...b62276 )
by Christian
02:00
created

InfoCommand::magentoVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace N98\Magento\Command\System;
4
5
use Exception;
6
use InvalidArgumentException;
7
use N98\Magento\Command\AbstractMagentoCommand;
8
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
9
use N98\Util\Console\Helper\TableHelper;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Finder\Finder;
15
16
class InfoCommand extends AbstractMagentoCommand
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $infos;
22
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('sys:info')
27
            ->addArgument(
28
                'key',
29
                InputArgument::OPTIONAL,
30
                'Only output value of named param like "version". Key is case insensitive.'
31
            )->setDescription('Prints infos about the current magento system.')
32
            ->addOption(
33
                'format',
34
                null,
35
                InputOption::VALUE_OPTIONAL,
36
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
37
            )
38
        ;
39
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @return int|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->detectMagento($output);
50
51
        $softInitMode = in_array($input->getArgument('key'), array(
52
            'version',
53
            'edition',
54
        ));
55
56
        if ($input->getOption('format') == null && $input->getArgument('key') == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $input->getArgument('key') of type string|string[]|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
57
            $this->writeSection($output, 'Magento System Information');
58
        }
59
60
        $this->initMagento($softInitMode);
61
62
        $this->infos['Version'] = $this->magentoVersion();
63
        $this->infos['Edition'] = ($this->_magentoEnterprise ? 'Enterprise' : 'Community');
64
        $this->infos['Root'] = $this->_magentoRootFolder;
65
66
        if ($softInitMode === false) {
67
            $config = \Mage::app()->getConfig();
68
            $this->addCacheInfos();
69
70
            $this->infos['Session'] = $config->getNode('global/session_save');
71
72
            $this->infos['Crypt Key'] = $config->getNode('global/crypt/key');
73
            $this->infos['Install Date'] = $config->getNode('global/install/date');
74
            try {
75
                $this->findCoreOverwrites();
76
                $this->findVendors();
77
                $this->attributeCount();
78
                $this->customerCount();
79
                $this->categoryCount();
80
                $this->productCount();
81
            } catch (Exception $e) {
82
                $output->writeln('<error>' . $e->getMessage() . '</error>');
83
            }
84
        }
85
86
        $table = array();
87
        foreach ($this->infos as $key => $value) {
88
            $table[] = array($key, $value);
89
        }
90
91
        if (($settingArgument = $input->getArgument('key')) !== null) {
92
            $settingArgument = strtolower($settingArgument);
93
            $this->infos = array_change_key_case($this->infos, CASE_LOWER);
94
            if (!isset($this->infos[$settingArgument])) {
95
                throw new InvalidArgumentException('Unknown key: ' . $settingArgument);
96
            }
97
            $output->writeln((string) $this->infos[$settingArgument]);
98 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99
            /* @var $tableHelper TableHelper */
100
            $tableHelper = $this->getHelper('table');
101
            $tableHelper
102
                ->setHeaders(array('name', 'value'))
103
                ->renderByFormat($output, $table, $input->getOption('format'));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('format') targeting Symfony\Component\Consol...tInterface::getOption() can also be of type boolean; however, N98\Util\Console\Helper\...elper::renderByFormat() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
        }
105
    }
106
107
    protected function magentoVersion()
108
    {
109
        if (method_exists('Mage', 'getOpenMageVersion')) {
110
            return 'OpenMage LTS ' . \Mage::getOpenMageVersion();
111
        }
112
113
        return \Mage::getVersion();
114
    }
115
116
    protected function addCacheInfos()
117
    {
118
        $this->infos['Cache Backend'] = get_class(\Mage::app()->getCache()->getBackend());
119
120
        switch (get_class(\Mage::app()->getCache()->getBackend())) {
121
            case 'Zend_Cache_Backend_File':
122
                $cacheDir = \Mage::app()->getConfig()->getOptions()->getCacheDir();
123
                $this->infos['Cache Directory'] = $cacheDir;
124
                break;
125
126
            default:
127
        }
128
    }
129
130
    /**
131
     * @return int|void
132
     */
133
    protected function findCoreOverwrites()
134
    {
135
        $folders = array(
136
            $this->_magentoRootFolder . '/app/code/local/Mage',
137
            $this->_magentoRootFolder . '/app/code/local/Enterprise',
138
            $this->_magentoRootFolder . '/app/code/community/Mage',
139
            $this->_magentoRootFolder . '/app/code/community/Enterprise',
140
        );
141
        foreach ($folders as $key => $folder) {
142
            if (!is_dir($folder)) {
143
                unset($folders[$key]);
144
            }
145
        }
146
147
        if (count($folders) > 0) {
148
            $finder = Finder::create();
149
            $finder
150
                ->files()
151
                ->ignoreUnreadableDirs(true)
152
                ->in($folders);
153
            $this->infos['Core Autoloader Overwrites'] = $finder->count();
154
        }
155
    }
156
157
    /**
158
     * @return int|void
159
     */
160
    protected function findVendors()
161
    {
162
        $codePools = array(
163
            'core'      => $this->_magentoRootFolder . '/app/code/core/',
164
            'community' => $this->_magentoRootFolder . '/app/code/community/',
165
        );
166
167
        if (is_dir($this->_magentoRootFolder . '/app/code/local/')) {
168
            $codePools['local'] = $this->_magentoRootFolder . '/app/code/local/';
169
        }
170
171
        foreach ($codePools as $codePool => $codePoolDir) {
172
            $finder = Finder::create();
173
            $finder
174
                ->directories()
175
                ->ignoreUnreadableDirs(true)
176
                ->in($codePoolDir)
177
                ->depth(0)
178
                ->sortByName();
179
180
            $vendors = iterator_to_array($finder);
181
            $vendors = array_map(
182
                function ($value) use ($codePoolDir) {
183
                    return str_replace($codePoolDir, '', $value);
184
                },
185
                $vendors
186
            );
187
188
            $this->infos['Vendors (' . $codePool . ')'] = implode(', ', $vendors);
189
        }
190
    }
191
192
    protected function categoryCount()
193
    {
194
        $this->infos['Category Count'] = \Mage::getModel('catalog/category')->getCollection()->getSize();
195
    }
196
197
    protected function productCount()
198
    {
199
        $this->infos['Product Count'] = \Mage::getModel('catalog/product')->getCollection()->getSize();
200
    }
201
202
    protected function customerCount()
203
    {
204
        $this->infos['Customer Count'] = \Mage::getModel('customer/customer')->getCollection()->getSize();
205
    }
206
207
    protected function attributeCount()
208
    {
209
        $this->infos['Attribute Count'] = \Mage::getModel('eav/entity_attribute')->getCollection()->getSize();
210
    }
211
}
212