Completed
Branch master (1f9106)
by Nils
02:44
created

StandardCliReporter::renderRuleOutput()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 5
nop 0
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use PhmLabs\Components\Init\Init;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use whm\Smoke\Config\Configuration;
8
use whm\Smoke\Scanner\Result;
9
10
class StandardCliReporter extends CliReporter
11
{
12
    /**
13
     * @var Result[]
14
     */
15
    private $results = array();
16
    private $orderBy;
17
    private $rules = array();
18
    private $maxResults;
19
20
    public function init(OutputInterface $_output, Configuration $_configuration, $orderBy = 'url', $maxResults = 0)
21
    {
22
        $this->setOutputInterface($_output);
23
24
        $this->orderBy = $orderBy;
25
        $this->rules = $_configuration->getRules();
26
27
        if ($maxResults === 0) {
28
            $this->maxResults = 10000000;
29
        } else {
30
            $this->maxResults = $maxResults;
31
        }
32
    }
33
34
    public function processResult(Result $result)
35
    {
36
        $this->results[] = $result;
37
    }
38
39
    public function finish()
40
    {
41
        if ($this->orderBy === 'url') {
42
            $this->renderUrlOutput();
43
        } elseif ($this->orderBy === 'rule') {
44
            $this->renderRuleOutput();
45
        }
46
        $this->output->writeln('');
47
    }
48
49
    private function getFailedUrls($ruleKey)
50
    {
51
        $failedUrls = array();
52
53
        $count = 0;
54
        foreach ($this->results as $result) {
55
            if ($result->isFailure()) {
56
                if (array_key_exists($ruleKey, $result->getMessages())) {
57
                    $messages = $result->getMessages();
58
                    $failedUrls[] = (string) $result->getUrl() . ' - ' . $messages[$ruleKey];
59
                    ++$count;
60
                }
61
                if ($count > $this->maxResults) {
62
                    $failedUrls[] = '... only the first ' . $this->maxResults . ' elements are shown.';
63
                    break;
64
                }
65
            }
66
        }
67
68
        return $failedUrls;
69
    }
70
71
    private function renderRuleOutput()
72
    {
73
        $this->output->writeln("\n\n <comment>Rules and Violations:</comment> \n");
74
75
        foreach ($this->rules as $ruleKey => $rule) {
76
            $info = Init::getInitInformationByClass($rule);
77
            $failedUrls = $this->getFailedUrls($ruleKey);
78
79
            if (count($failedUrls) > 0) {
80
                $this->output->writeln('  <error> ' . get_class($rule) . ' </error>');
81
            } else {
82
                $this->output->writeln('  <info> ' . get_class($rule) . ' </info>');
83
            }
84
85
            $this->output->writeln('   ' . str_replace("\n", "\n   ", $info['documentation']) . "\n");
86
87
            foreach ($failedUrls as $failedUrl) {
88
                $this->output->writeln('   - ' . $failedUrl);
89
            }
90
91
            $this->output->writeln('');
92
        }
93
    }
94
95
    private function renderUrlOutput()
96
    {
97
        $this->output->writeln("\n\n <comment>Passed tests:</comment> \n");
98
99
        foreach ($this->results as $result) {
100
            if ($result->isSuccess()) {
101
                $this->renderSuccess($result);
102
            }
103
        }
104
105
        $this->output->writeln("\n <comment>Failed tests:</comment> \n");
106
107
        foreach ($this->results as $result) {
108
            if ($result->isFailure()) {
109
                $this->renderFailure($result);
110
            }
111
        }
112
    }
113
}
114