Guard::printErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 17
rs 9.4285
c 1
b 0
f 1
1
<?php
2
3
namespace LimeSoda\LiveGuard;
4
use N98\Magento\Command\AbstractMagentoCommand;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\StringInput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use WebDriver\Exception;
10
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
11
use Symfony\Component\Console\Input\InputOption;
12
use LimeSoda\LiveGuard\ErrorCollection;
13
use N98\Magento\Command\System\CheckCommand;
14
15
/**
16
 * Class Guard
17
 * @package LimeSoda\LiveGuard
18
 */
19
class Guard extends AbstractMagentoCommand
20
{
21
22
    protected static $_tableHeaders = array('Message', 'File');
23
    const ERROR_MSG = '%s %s Errors(s) found';
24
    const SUCCESS_MSG = '%s No Errors found';
25
    const CONFIG_MISSING_MSG = '%s No Configuration found. Please add guards to the configuration.';
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('ls:liveguard')
31
            ->setDescription(
32
                'Helps you to protect your live environment (and others)'
33
            );
34
    }
35
36
    /**
37
     * @param \Symfony\Component\Console\Input\InputInterface $input
38
     * @param \Symfony\Component\Console\Output\OutputInterface $output
39
     * @return int|void
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $this->detectMagento($output, true);
44
        if ($this->initMagento()) {
45
46
            $config = \Mage::getModel('limesoda_liveguard/config');
47
48
            if (!$config->configExists()) {
49
                $this->printConfigMissing($output);
50
                return $this;
51
            }
52
53
            $errorCollection = new ErrorCollection();
54
            foreach ($config->getGuards() as $guard) {
55
56
                try {
57
                    $guard->process();
58
                }
59
                catch( \Exception $e ) {
60
                    $errorCollection->addError($e);
61
                }
62
            }
63
64
            if ($errorCollection->countErrors()) {
65
                $this->printErrors($output, $errorCollection);
66
            } else {
67
                $this->printSuccess($output);
68
            }
69
70
        }
71
    }
72
73
    /**
74
     * @param OutputInterface $output
75
     * @param ErrorCollection $errorCollection
76
     */
77
    protected function printErrors(
78
        OutputInterface $output,
79
        ErrorCollection $errorCollection
80
    ) {
81
        $this->writeSection(
82
            $output,
83
            sprintf(
84
                self::ERROR_MSG,
85
                \N98\Util\Unicode\Charset::convertInteger(
86
                    CheckCommand::UNICODE_CROSS_CHAR
87
                ),
88
                $errorCollection->countErrors()
89
            ),
90
            'bg=red;fg=white'
91
        );
92
        $this->printErrorsTable($output, $errorCollection);
93
    }
94
95
    /**
96
     * @param OutputInterface $output
97
     * @param ErrorCollection $errorCollection
98
     */
99
    protected function printErrorsTable(
100
        OutputInterface $output,
101
        ErrorCollection $errorCollection
102
    ) {
103
104
        $table = $this->getHelper('table');
105
        $table->setHeaders(self::$_tableHeaders);
106
107
        foreach ($errorCollection->getErrors() as $error) {
108
109
            $row = array(
110
                $error->getMessage(),
111
                $error->getFirstTraceFile()
112
            );
113
114
            $table->addRow($row);
115
        }
116
117
        $table->render($output);
118
    }
119
120
    /**
121
     * @param OutputInterface $output
122
     */
123
    protected function printConfigMissing(OutputInterface $output)
124
    {
125
        $this->writeSection(
126
            $output,
127
            sprintf(
128
                self::CONFIG_MISSING_MSG,
129
                \N98\Util\Unicode\Charset::convertInteger(
130
                    CheckCommand::UNICODE_CROSS_CHAR
131
                )
132
            ),
133
            'bg=blue;fg=white'
134
        );
135
    }
136
137
    /**
138
     * @param OutputInterface $output
139
     */
140
    protected function printSuccess(OutputInterface $output)
141
    {
142
        $this->writeSection(
143
            $output,
144
            sprintf(
145
                self::SUCCESS_MSG,
146
                \N98\Util\Unicode\Charset::convertInteger(
147
                    CheckCommand::UNICODE_CHECKMARK_CHAR
148
                )
149
            ),
150
            'bg=green;fg=white'
151
        );
152
    }
153
154
}
155