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

CheckCommand::_invokeCheckClass()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 15
nc 4
nop 2
1
<?php
2
3
namespace N98\Magento\Command\System;
4
5
use LogicException;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use N98\Magento\Command\CommandAware;
8
use N98\Magento\Command\CommandConfigAware;
9
use N98\Magento\Command\System\Check\Result;
10
use N98\Magento\Command\System\Check\ResultCollection;
11
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
12
use N98\Util\Unicode\Charset;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class CheckCommand
19
 *
20
 * @package N98\Magento\Command\System
21
 */
22
class CheckCommand extends AbstractMagentoCommand
23
{
24
    /**
25
     * Command config
26
     *
27
     * @var array
28
     */
29
    private $config;
30
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('sys:check')
35
            ->setDescription('Checks Magento System')
36
            ->addOption(
37
                'format',
38
                null,
39
                InputOption::VALUE_OPTIONAL,
40
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
41
            )
42
        ;
43
44
        $help = <<<HELP
45
- Checks missing files and folders
46
- Security
47
- PHP Extensions (Required and Bytecode Cache)
48
- MySQL InnoDB Engine
49
HELP;
50
        $this->setHelp($help);
51
    }
52
53
    /**
54
     * @param InputInterface  $input
55
     * @param OutputInterface $output
56
     *
57
     * @return int|void
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $this->config = $this->getCommandConfig();
62
63
        $this->detectMagento($output);
64
        if ($this->initMagento()) {
65
66
            $results = new ResultCollection();
67
68
            foreach ($this->config['checks'] as $checkGroup => $checkGroupClasses) {
69
                $results->setResultGroup($checkGroup);
70
                foreach ($checkGroupClasses as $checkGroupClass) {
71
                    $this->_invokeCheckClass($results, $checkGroupClass);
72
                }
73
            }
74
75
            if ($input->getOption('format')) {
76
                $this->_printTable($input, $output, $results);
77
            } else {
78
                $this->_printResults($output, $results);
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param ResultCollection $results
85
     * @param string           $checkGroupClass name
86
     */
87
    protected function _invokeCheckClass(ResultCollection $results, $checkGroupClass)
88
    {
89
        $check = $this->_createCheck($checkGroupClass);
90
91
        switch (true) {
92
            case $check instanceof Check\SimpleCheck:
93
                $check->check($results);
94
                break;
95
96
            case $check instanceof Check\StoreCheck:
97
                $this->checkStores($results, $checkGroupClass, $check);
98
                break;
99
100
            case $check instanceof Check\WebsiteCheck:
101
                $this->checkWebsites($results, $checkGroupClass, $check);
102
                break;
103
104
            default:
105
                throw new LogicException(
106
                    sprintf('Unhandled check-class "%s"', $checkGroupClass)
107
                );
108
        }
109
    }
110
111
    /**
112
     * @param OutputInterface  $output
113
     * @param ResultCollection $results
114
     */
115
    protected function _printResults(OutputInterface $output, ResultCollection $results)
116
    {
117
        $lastResultGroup = null;
118
        foreach ($results as $result) {
119
            if ($result->getResultGroup() != $lastResultGroup) {
120
                $this->writeSection($output, str_pad(strtoupper($result->getResultGroup()), 60, ' ', STR_PAD_BOTH));
121
            }
122
            if ($result->getMessage()) {
123
                switch ($result->getStatus()) {
124
                    case Result::STATUS_WARNING:
125
                    case Result::STATUS_ERROR:
126
                        $output->write('<error>' . Charset::convertInteger(Charset::UNICODE_CROSS_CHAR) . '</error> ');
127
                        break;
128
129
                    case Result::STATUS_OK:
130
                    default:
131
                        $output->write('<info>' . Charset::convertInteger(Charset::UNICODE_CHECKMARK_CHAR) . '</info> ');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
132
                        break;
133
                }
134
                $output->writeln($result->getMessage());
135
            }
136
137
            $lastResultGroup = $result->getResultGroup();
138
        }
139
    }
140
141
    /**
142
     * @param InputInterface   $input
143
     * @param OutputInterface  $output
144
     * @param ResultCollection $results
145
     */
146
    protected function _printTable(InputInterface $input, OutputInterface $output, ResultCollection $results)
147
    {
148
        $table = array();
149
        foreach ($results as $result) {
150
            /* @var $result Result */
151
            $table[] = array(
152
                $result->getResultGroup(),
153
                strip_tags($result->getMessage()),
154
                $result->getStatus()
155
            );
156
        }
157
158
        $this->getHelper('table')
159
            ->setHeaders(array('Group', 'Message', 'Result'))
160
            ->renderByFormat($output, $table, $input->getOption('format'));
161
    }
162
163
    /**
164
     * @param string $checkGroupClass
165
     *
166
     * @return object
167
     */
168
    private function _createCheck($checkGroupClass)
169
    {
170
        $check = new $checkGroupClass();
171
172
        if ($check instanceof CommandAware) {
173
            $check->setCommand($this);
174
        }
175
        if ($check instanceof CommandConfigAware) {
176
            $check->setCommandConfig($this->config);
177
178
            return $check;
179
        }
180
181
        return $check;
182
    }
183
184
    /**
185
     * @param ResultCollection $results
186
     * @param string           $context
187
     * @param string           $checkGroupClass
188
     */
189
    private function _markCheckWarning(ResultCollection $results, $context, $checkGroupClass)
190
    {
191
        $result = $results->createResult();
192
        $result->setMessage('<error>No ' . $context . ' configured to run store check:</error> <comment>' . basename($checkGroupClass) . '</comment>');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 151 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
193
        $result->setStatus($result::STATUS_WARNING);
194
        $results->addResult($result);
195
    }
196
197
    /**
198
     * @param ResultCollection $results
199
     * @param string $checkGroupClass name
200
     * @param Check\StoreCheck $check
201
     */
202 View Code Duplication
    private function checkStores(ResultCollection $results, $checkGroupClass, Check\StoreCheck $check)
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...
203
    {
204
        if (!$stores = \Mage::app()->getStores()) {
205
            $this->_markCheckWarning($results, 'stores', $checkGroupClass);
206
        }
207
        foreach ($stores as $store) {
208
            $check->check($results, $store);
209
        }
210
    }
211
212
    /**
213
     * @param ResultCollection $results
214
     * @param string $checkGroupClass name
215
     * @param Check\WebsiteCheck $check
216
     */
217 View Code Duplication
    private function checkWebsites(ResultCollection $results, $checkGroupClass, Check\WebsiteCheck $check)
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...
218
    {
219
        if (!$websites = \Mage::app()->getWebsites()) {
220
            $this->_markCheckWarning($results, 'websites', $checkGroupClass);
221
        }
222
        foreach ($websites as $website) {
223
            $check->check($results, $website);
224
        }
225
    }
226
}
227