Completed
Push — master ( 8ec94e...9abf36 )
by Nils
02:18
created

LeankoalaReporter::send()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 15

Duplication

Lines 22
Ratio 100 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 22
loc 22
rs 8.6737
cc 5
eloc 15
nc 7
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\KoalamonException;
9
use Koalamon\Client\Reporter\Reporter as KoalaReporter;
10
use phm\HttpWebdriverClient\Http\Response\TimeoutAwareResponse;
11
use Psr\Http\Message\ResponseInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use whm\Smoke\Config\Configuration;
14
use whm\Smoke\Extensions\Leankoala\LeankoalaExtension;
15
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
16
use whm\Smoke\Rules\CheckResult;
17
use whm\Smoke\Scanner\Result;
18
19
/**
20
 * Class XUnitReporter.
21
 */
22
class LeankoalaReporter implements Reporter
23
{
24
    /**
25
     * @var Result[]
26
     */
27
    private $results = [];
28
29
    /**
30
     * @var Configuration
31
     */
32
    private $config;
33
    private $system;
34
    private $collect;
35
    private $identifier;
36
    private $systemUseRetriever;
37
    private $tool = 'smoke';
38
    private $groupBy;
39
    private $server;
40
    private $addComingFrom;
41
    private $useUrlAsIdentifier;
42
43
    /**
44
     * @var KoalaReporter
45
     */
46
    private $reporter;
47
48
    /**
49
     * @var Retriever
50
     */
51
    private $retriever;
52
53
    /**
54
     * @var OutputInterface
55
     */
56
    private $output;
57
58
    /**
59
     * @var LeankoalaExtension
60
     */
61
    private $leankoalaExtension;
62
63
    const STATUS_SUCCESS = 'success';
64
    const STATUS_FAILURE = 'failure';
65
66
    public function init($apiKey, Configuration $_configuration, OutputInterface $_output, $server = 'https://webhook.koalamon.com', $system = '', $identifier = '', $tool = '', $collect = true, $systemUseRetriever = false, $groupBy = false, $addComingFrom = true, $useMongo = true, $useUrlAsIdentifier = false)
67
    {
68
        $httpClient = new \GuzzleHttp\Client();
69
70
        $this->reporter = new KoalaReporter('', $apiKey, $httpClient, $server);
71
72
        if ($useMongo) {
73
            $this->reporter->setEventProcessor(MongoDBProcessor::createByEnvironmentVars('leankoala'));
74
        }
75
76
        $this->config = $_configuration;
77
        $this->systemUseRetriever = $systemUseRetriever;
78
79
        $this->system = $system;
80
        $this->collect = $collect;
81
        $this->identifier = $identifier;
82
        $this->useUrlAsIdentifier = $useUrlAsIdentifier;
83
        $this->groupBy = $groupBy;
84
85
        $this->addComingFrom = $addComingFrom;
86
87
        if ($tool) {
88
            $this->tool = $tool;
89
        }
90
91
        $this->leankoalaExtension = $_configuration->getExtension('Leankoala');
92
93
        $this->server = $server;
94
        $this->output = $_output;
95
    }
96
97
    public function setResponseRetriever(Retriever $retriever)
98
    {
99
        $this->retriever = $retriever;
100
    }
101
102
    public function processResults($results)
103
    {
104
        $this->results[] = $results;
105
    }
106
107
    public function finish()
108
    {
109
        if ($this->collect) {
110
            $this->sendCollectedResults();
111
        } else {
112
            $this->sendSingleResults();
113
        }
114
    }
115
116
    private function getComponent($ruleName)
117
    {
118
        $ruleArray = explode('_', $ruleName);
119
        $component = array_pop($ruleArray);
120
121
        return $component;
122
    }
123
124
    private function sendCollectedResults()
125
    {
126
        $checks = [];
127
128
        foreach ($this->results as $results) {
129
            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...
130
                /* @var CheckResult $result */
131
                $tool = 'Smoke' . $result->getRuleName();
132
                $checks[$tool][] = $result;
133
            }
134
        }
135
136
        foreach ($checks as $toolName => $results) {
137
            $attributes = array();
138
139
            if (count($results) === 0) {
140
                continue;
141
            }
142
143
            $message = 'The smoke test for #system_name# failed (Rule: ' . $toolName . ').<ul>';
144
            $status = Event::STATUS_SUCCESS;
145
            $failureCount = 0;
146
            $identifier = $toolName . '_' . $this->system;
147
148
            foreach ($results as $result) {
149
                /** @var CheckResult $result */
150
                if ($result->getStatus() === CheckResult::STATUS_FAILURE) {
151
                    $comingFrom = '';
152
                    if ($this->addComingFrom && $this->retriever->getComingFrom($result->getResponse()->getUri())) {
153
                        $comingFrom = ', coming from: ' . $this->retriever->getComingFrom($result->getResponse()->getUri());
154
                    }
155
                    $message .= '<li>' . $result->getMessage() . ' (url: ' . (string)$result->getResponse()->getUri() . $comingFrom . ')</li>';
156
                    ++$failureCount;
157
                }
158
            }
159
            if ($failureCount > 0) {
160
                $status = Event::STATUS_FAILURE;
161
                $message .= '</ul>';
162
                $firstResult = array_pop($results);
163
                $response = $firstResult->getResponse();
164
                /** @var ResponseInterface $response */
165
166
                $attributes[] = new Attribute('html-content', (string)$response->getBody(), true);
167
                if ($response instanceof TimeoutAwareResponse) {
0 ignored issues
show
Bug introduced by
The class phm\HttpWebdriverClient\...se\TimeoutAwareResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
168
                    $attributes[] = new Attribute('timeout', $response->isTimeout());
169
                }
170
            } else {
171
                $message = 'All checks for system "#system_name#" succeeded [SmokeBasic:' . $toolName . '].';
172
            }
173
174
            $this->send($identifier, $this->system, $message, $status, $failureCount, $this->tool, $this->system, $attributes);
175
        }
176
    }
177
178
    private function sendSingleResults()
179
    {
180
        foreach ($this->results as $results) {
181
            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...
182
                /* @var CheckResult $result */
183
184
                $identifier = '_' . $this->getIdentifier($result);
185
                $tool = $this->getPrefix($result->getRuleName());
186
187
                $component = $this->getComponent($result->getRuleName());
188
                $system = $this->leankoalaExtension->getSystem($component);
189
190
                $attributes = array();
191
                if ($result->getStatus() == CheckResult::STATUS_FAILURE) {
192
                    $body = (string)$result->getResponse()->getBody();
193
                    if ($body == "") {
194
                        $attributes[] = new Attribute('html content', '<empty>');
195
                    } else {
196
                        $attributes[] = new Attribute('html content', $body, true);
197
                    }
198
                    $attributes[] = new Attribute('http header', json_encode($result->getResponse()->getHeaders()), true);
199
                    $attributes[] = new Attribute('http status code', $result->getResponse()->getStatusCode());
200
201
                    if ($result->getResponse() instanceof TimeoutAwareResponse) {
0 ignored issues
show
Bug introduced by
The class phm\HttpWebdriverClient\...se\TimeoutAwareResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
202
                        $attributes[] = new Attribute('timeout', $result->getResponse()->isTimeout());
203
                    }
204
                }
205
206
                $checkResultAttributes = $result->getAttributes();
207
                foreach ($checkResultAttributes as $checkResultAttribute) {
208
                    $attributes[] = new Attribute($checkResultAttribute->getKey(), $checkResultAttribute->getValue(), $checkResultAttribute->isIsStorable());
209
                }
210
211
                if ($this->system) {
212
                    $currentSystem = $this->system;
213
                } else {
214
                    $currentSystem = $system;
215
                }
216
217
                // do not send skipped check results to Leankoala
218
                if ($result->getStatus() != CheckResult::STATUS_SKIPPED) {
219
                    $this->send(
220
                        $identifier,
221
                        $currentSystem,
222
                        $result->getMessage() . ' (url: ' . (string)$result->getResponse()->getUri() . ')',
223
                        $result->getStatus(),
224
                        $result->getValue(),
225
                        $tool,
226
                        $component,
227
                        $attributes,
228
                        $result->getUrl()
229
                    );
230
                }
231
            }
232
        }
233
    }
234
235
    private function getIdentifier(CheckResult $result)
236
    {
237
        if ($this->useUrlAsIdentifier) {
238
            $suffix = '_' . md5((string)$result->getResponse()->getUri());
239
        } else {
240
            $suffix = '';
241
        }
242
243
        return $this->tool . '_' . $result->getRuleName() . $suffix;
244
    }
245
246
    private function getPrefix($string)
247
    {
248
        return substr($string, 0, strpos($string, '_'));
249
    }
250
251
    /**
252
     * @param $identifier
253
     * @param $system
254
     * @param $message
255
     * @param $status
256
     * @param $value
257
     * @param $tool
258
     * @param $component
259
     * @param Attribute[] $attributes
260
     */
261 View Code Duplication
    private function send($identifier, $system, $message, $status, $value, $tool, $component, $attributes = [], $url = "")
262
    {
263
        if ($status !== CheckResult::STATUS_NONE) {
264
            $event = new Event($identifier, $system, $status, $tool, $message, $value, $url, $component);
265
            $event->addAttribute(new Attribute('_config', json_encode($this->config->getConfigArray()), true));
266
267
            foreach ($attributes as $attribute) {
268
                $event->addAttribute($attribute);
269
            }
270
271
            try {
272
                $this->reporter->sendEvent($event);
273
            } catch (KoalamonException $e) {
274
                $this->output->writeln("\n  <error> Error sending result to leankoala. </error>");
275
                $this->output->writeln('   Url: ' . $e->getUrl());
276
                $this->output->writeln('   Payload: ' . $e->getPayload());
277
                $this->output->writeln("");
278
            } catch (\Exception $e) {
279
                $this->output->writeln($e->getMessage());
280
            }
281
        }
282
    }
283
}
284