Completed
Push — master ( 9b8114...2ed294 )
by Nils
02:31
created

LeankoalaReporter::init()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 11

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\Reporter as KoalaReporter;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use whm\Smoke\Config\Configuration;
9
use whm\Smoke\Extensions\Leankoala\LeankoalaExtension;
10
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
11
use whm\Smoke\Rules\CheckResult;
12
use whm\Smoke\Scanner\Result;
13
14
/**
15
 * Class XUnitReporter.
16
 */
17
class LeankoalaReporter implements Reporter
18
{
19
    /**
20
     * @var Result[]
21
     */
22
    private $results = [];
23
24
    private $config;
25
    private $system;
26
    private $collect;
27
    private $identifier;
28
    private $systemUseRetriever;
29
    private $tool = 'smoke';
30
    private $groupBy;
31
    private $server;
32
    private $addComingFrom;
33
34
    /**
35
     * @var KoalaReporter
36
     */
37
    private $reporter;
38
39
    /**
40
     * @var Retriever
41
     */
42
    private $retriever;
43
44
    private $output;
45
46
    /**
47
     * @var LeankoalaExtension
48
     */
49
    private $leankoalaExtension;
50
51
    const STATUS_SUCCESS = 'success';
52
    const STATUS_FAILURE = 'failure';
53
54 View Code Duplication
    public function init($apiKey, Configuration $_configuration, OutputInterface $_output, $server = 'https://webhook.koalamon.com', $system = '', $identifier = '', $tool = '', $collect = true, $systemUseRetriever = false, $groupBy = false, $addComingFrom = true)
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...
55
    {
56
        $httpClient = new \GuzzleHttp\Client();
57
        $this->reporter = new KoalaReporter('', $apiKey, $httpClient, $server);
58
59
        $this->config = $_configuration;
60
        $this->systemUseRetriever = $systemUseRetriever;
61
62
        $this->system = $system;
63
        $this->collect = $collect;
64
        $this->identifier = $identifier;
65
        $this->groupBy = $groupBy;
66
67
        $this->addComingFrom = $addComingFrom;
68
69
        if ($tool) {
70
            $this->tool = $tool;
71
        }
72
73
        $this->leankoalaExtension = $_configuration->getExtension('Leankoala');
74
75
        $this->server = $server;
76
        $this->output = $_output;
77
    }
78
79
    public function setResponseRetriever(Retriever $retriever)
80
    {
81
        $this->retriever = $retriever;
82
    }
83
84
    public function processResults($results)
85
    {
86
        $this->results[] = $results;
87
    }
88
89
    public function finish()
90
    {
91
        if ($this->collect) {
92
            $this->sendCollectedResults();
93
        } else {
94
            $this->sendSingleResults();
95
        }
96
    }
97
98
    private function getComponent($ruleName)
99
    {
100
        $ruleArray = explode('_', $ruleName);
101
        $component = array_pop($ruleArray);
102
103
        return $component;
104
    }
105
106
    private function sendCollectedResults()
107
    {
108
        $checks = [];
109
110
        foreach ($this->results as $results) {
111
            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...
112
                /* @var CheckResult $result */
113
                $tool = 'Smoke' . $result->getRuleName();
114
                $checks[$tool][] = $result;
115
            }
116
        }
117
118
        foreach ($checks as $toolName => $results) {
119
            if (count($results) === 0) {
120
                continue;
121
            }
122
123
            $message = 'The smoke test for #system_name# failed (Rule: ' . $toolName . ').<ul>';
124
            $status = Event::STATUS_SUCCESS;
125
            $failureCount = 0;
126
            $identifier = $toolName . '_' . $this->system;
127
128
            foreach ($results as $result) {
129
                /** @var CheckResult $result */
130
                if ($result->getStatus() === CheckResult::STATUS_FAILURE) {
131
                    $comingFrom = '';
132
                    if ($this->addComingFrom && $this->retriever->getComingFrom($result->getResponse()->getUri())) {
133
                        $comingFrom = ', coming from: ' . $this->retriever->getComingFrom($result->getResponse()->getUri());
134
                    }
135
                    $message .= '<li>' . $result->getMessage() . ' (url: ' . (string) $result->getResponse()->getUri() . $comingFrom . ')</li>';
136
                    ++$failureCount;
137
                }
138
            }
139
            if ($failureCount > 0) {
140
                $status = Event::STATUS_FAILURE;
141
                $message .= '</ul>';
142
            } else {
143
                $message = 'All checks for system "#system_name#" succeeded [SmokeBasic:' . $toolName . '].';
144
            }
145
146
            $this->send($identifier, $this->system, $message, $status, $failureCount, $this->tool, $this->system);
147
        }
148
    }
149
150
    private function sendSingleResults()
151
    {
152
        foreach ($this->results as $results) {
153
            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...
154
                /* @var CheckResult $result */
155
156
                $identifier = '_' . $this->getIdentifier($result);
157
                $tool = $this->getPrefix($result->getRuleName());
158
159
                $component = $this->getComponent($result->getRuleName());
160
                $system = $this->leankoalaExtension->getSystem($component);
161
162
                $this->send(
163
                    $identifier,
164
                    $system,
165
                    $result->getMessage() . ' (url: ' . (string) $result->getResponse()->getUri() . ')',
166
                    $result->getStatus(),
167
                    $result->getValue(),
168
                    $tool,
169
                    $component
170
                );
171
            }
172
        }
173
    }
174
175
    private function getIdentifier(CheckResult $result)
176
    {
177
        return $this->tool . '_' . $result->getRuleName();
178
    }
179
180
    private function getPrefix($string)
181
    {
182
        return substr($string, 0, strpos($string, '_'));
183
    }
184
185
    private function send($identifier, $system, $message, $status, $value, $tool, $component)
186
    {
187
        $event = new Event($identifier, $system, $status, $tool, $message, $value, '', $component);
188
        $this->reporter->sendEvent($event);
189
    }
190
}
191