Completed
Push — master ( 335f7c...925222 )
by Nils
02:44
created

LeankoalaReporter::sendCollectedResults()   C

Complexity

Conditions 11
Paths 42

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 50
rs 5.4893
cc 11
eloc 30
nc 42
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use Koalamon\Client\Reporter\Event;
6
use Koalamon\Client\Reporter\Event\Attribute;
7
use Koalamon\Client\Reporter\Event\Processor\MongoDBProcessor;
8
use Koalamon\Client\Reporter\Reporter as KoalaReporter;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use whm\Smoke\Config\Configuration;
11
use whm\Smoke\Extensions\Leankoala\LeankoalaExtension;
12
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
13
use whm\Smoke\Rules\CheckResult;
14
use whm\Smoke\Scanner\Result;
15
16
/**
17
 * Class XUnitReporter.
18
 */
19
class LeankoalaReporter implements Reporter
20
{
21
    /**
22
     * @var Result[]
23
     */
24
    private $results = [];
25
26
    /**
27
     * @var Configuration
28
     */
29
    private $config;
30
    private $system;
31
    private $collect;
32
    private $identifier;
33
    private $systemUseRetriever;
34
    private $tool = 'smoke';
35
    private $groupBy;
36
    private $server;
37
    private $addComingFrom;
38
39
    /**
40
     * @var KoalaReporter
41
     */
42
    private $reporter;
43
44
    /**
45
     * @var Retriever
46
     */
47
    private $retriever;
48
49
    private $output;
50
51
    /**
52
     * @var LeankoalaExtension
53
     */
54
    private $leankoalaExtension;
55
56
    const STATUS_SUCCESS = 'success';
57
    const STATUS_FAILURE = 'failure';
58
59
    public function init($apiKey, Configuration $_configuration, OutputInterface $_output, $server = 'https://webhook.koalamon.com', $system = '', $identifier = '', $tool = '', $collect = true, $systemUseRetriever = false, $groupBy = false, $addComingFrom = true)
60
    {
61
        $httpClient = new \GuzzleHttp\Client();
62
63
        $this->reporter = new KoalaReporter('', $apiKey, $httpClient, $server);
64
65
        $this->reporter->setEventProcessor(MongoDBProcessor::createByEnvironmentVars('leankoala'));
66
67
        $this->config = $_configuration;
68
        $this->systemUseRetriever = $systemUseRetriever;
69
70
        $this->system = $system;
71
        $this->collect = $collect;
72
        $this->identifier = $identifier;
73
        $this->groupBy = $groupBy;
74
75
        $this->addComingFrom = $addComingFrom;
76
77
        if ($tool) {
78
            $this->tool = $tool;
79
        }
80
81
        $this->leankoalaExtension = $_configuration->getExtension('Leankoala');
82
83
        $this->server = $server;
84
        $this->output = $_output;
85
    }
86
87
    public function setResponseRetriever(Retriever $retriever)
88
    {
89
        $this->retriever = $retriever;
90
    }
91
92
    public function processResults($results)
93
    {
94
        $this->results[] = $results;
95
    }
96
97
    public function finish()
98
    {
99
        if ($this->collect) {
100
            $this->sendCollectedResults();
101
        } else {
102
            $this->sendSingleResults();
103
        }
104
    }
105
106
    private function getComponent($ruleName)
107
    {
108
        $ruleArray = explode('_', $ruleName);
109
        $component = array_pop($ruleArray);
110
111
        return $component;
112
    }
113
114
    private function sendCollectedResults()
115
    {
116
        $checks = [];
117
118
        foreach ($this->results as $results) {
119
            foreach ($results as $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type object<whm\Smoke\Scanner\Result> is not traversable.
Loading history...
120
                /* @var CheckResult $result */
121
                $tool = 'Smoke' . $result->getRuleName();
122
                $checks[$tool][] = $result;
123
            }
124
        }
125
126
        foreach ($checks as $toolName => $results) {
127
            $attributes = array();
128
129
            if (count($results) === 0) {
130
                continue;
131
            }
132
133
            $message = 'The smoke test for #system_name# failed (Rule: ' . $toolName . ').<ul>';
134
            $status = Event::STATUS_SUCCESS;
135
            $failureCount = 0;
136
            $identifier = $toolName . '_' . $this->system;
137
138
            foreach ($results as $result) {
139
                /** @var CheckResult $result */
140
                if ($result->getStatus() === CheckResult::STATUS_FAILURE) {
141
                    $comingFrom = '';
142
                    if ($this->addComingFrom && $this->retriever->getComingFrom($result->getResponse()->getUri())) {
143
                        $comingFrom = ', coming from: ' . $this->retriever->getComingFrom($result->getResponse()->getUri());
144
                    }
145
                    $message .= '<li>' . $result->getMessage() . ' (url: ' . (string)$result->getResponse()->getUri() . $comingFrom . ')</li>';
146
                    ++$failureCount;
147
                }
148
            }
149
            if ($failureCount > 0) {
150
                $status = Event::STATUS_FAILURE;
151
                $message .= '</ul>';
152
                $firstResult = array_pop($results);
153
                if ($firstResult) {
154
                    $attributes[] = new Attribute('html-content', (string)$firstResult->getResponse()->getBody(), true);
155
                }
156
            } else {
157
                $message = 'All checks for system "#system_name#" succeeded [SmokeBasic:' . $toolName . '].';
158
            }
159
160
161
            $this->send($identifier, $this->system, $message, $status, $failureCount, $this->tool, $this->system, $attributes);
162
        }
163
    }
164
165
    private function sendSingleResults()
166
    {
167
        foreach ($this->results as $results) {
168
            foreach ($results as $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type object<whm\Smoke\Scanner\Result> is not traversable.
Loading history...
169
                /* @var CheckResult $result */
170
171
                $identifier = '_' . $this->getIdentifier($result);
172
                $tool = $this->getPrefix($result->getRuleName());
173
174
                $component = $this->getComponent($result->getRuleName());
175
                $system = $this->leankoalaExtension->getSystem($component);
176
177
                $this->send(
178
                    $identifier,
179
                    $system,
180
                    $result->getMessage() . ' (url: ' . (string)$result->getResponse()->getUri() . ')',
181
                    $result->getStatus(),
182
                    $result->getValue(),
183
                    $tool,
184
                    $component
185
                );
186
            }
187
        }
188
    }
189
190
    private function getIdentifier(CheckResult $result)
191
    {
192
        return $this->tool . '_' . $result->getRuleName();
193
    }
194
195
    private function getPrefix($string)
196
    {
197
        return substr($string, 0, strpos($string, '_'));
198
    }
199
200
    /**
201
     * @param $identifier
202
     * @param $system
203
     * @param $message
204
     * @param $status
205
     * @param $value
206
     * @param $tool
207
     * @param $component
208
     * @param Attribute[] $attributes
209
     */
210
    private function send($identifier, $system, $message, $status, $value, $tool, $component, $attributes = [])
211
    {
212
        if ($status !== CheckResult::STATUS_NONE) {
213
            $event = new Event($identifier, $system, $status, $tool, $message, $value, '', $component);
214
            $event->addAttribute(new Attribute('_config', json_encode($this->config->getConfigArray()), true));
215
            foreach ($attributes as $attribute) {
216
                $event->addAttribute($attribute);
217
            }
218
            $this->reporter->sendEvent($event);
219
        }
220
    }
221
}
222