Completed
Push — master ( f02e4f...202a7d )
by
unknown
02:00
created

KoalamonSummaryReporter::finish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
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 Symfony\Component\Console\Output\OutputInterface;
11
use whm\Smoke\Config\Configuration;
12
use whm\Smoke\Extensions\Leankoala\LeankoalaExtension;
13
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
14
use whm\Smoke\Rules\CheckResult;
15
use whm\Smoke\Scanner\Result;
16
17
class KoalamonSummaryReporter implements Reporter
18
{
19
    private $failed = 0;
20
    private $success = 0;
21
22
    /**
23
     * @var Configuration
24
     */
25
    private $config;
26
    private $system;
27
    private $identifier;
28
    private $tool = 'smoke';
29
    private $server;
30
    private $url;
31
32
    /**
33
     * @var KoalaReporter
34
     */
35
    private $reporter;
36
37
    /**
38
     * @var Retriever
39
     */
40
    private $retriever;
0 ignored issues
show
Unused Code introduced by
The property $retriever is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
42
    /**
43
     * @var OutputInterface
44
     */
45
    private $output;
46
47
    /**
48
     * @var LeankoalaExtension
49
     */
50
    private $leankoalaExtension;
51
52
    const STATUS_SUCCESS = 'success';
53
    const STATUS_FAILURE = 'failure';
54
55
    public function init($apiKey, Configuration $_configuration, OutputInterface $_output, $server = 'https://webhook.koalamon.com', $system = '', $identifier = '', $tool = '', $url ='')
56
    {
57
        $httpClient = new \GuzzleHttp\Client();
58
59
        $this->reporter = new KoalaReporter('', $apiKey, $httpClient, $server);
60
61
        $this->config = $_configuration;
62
63
        $this->system = $system;
64
        $this->identifier = $identifier;
65
66
        if ($tool) {
67
            $this->tool = $tool;
68
        }
69
70
        $this->leankoalaExtension = $_configuration->getExtension('Leankoala');
71
72
        $this->url = $url;
73
        $this->server = $server;
74
        $this->output = $_output;
75
76
        var_dump($this->url);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->url); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
77
    }
78
79
    /**
80
     * @param CheckResult[] $results
81
     */
82
    public function processResults($results)
83
    {
84
        foreach ($results as $result) {
85
            if($result->getStatus() == CheckResult::STATUS_SUCCESS) {
86
                $this->success++;
87
            } else {
88
                $this->failed++;
89
            }
90
        }
91
    }
92
93
    public function finish()
94
    {
95
        $message = 'Checks: ';
96
        $message .= $this->success . ' succeeded, ';
97
        $message .= $this->failed . ' failed. ';
98
99
        if ($this->failed > 0) {
100
            $status = Event::STATUS_FAILURE;
101
        } else {
102
            $status = Event::STATUS_SUCCESS;
103
        }
104
105
        $this->send($this->identifier, $this->system, $message, $status, $this->failed, $this->tool, $this->system, [], $this->url);
106
    }
107
108
    /**
109
     * @param $identifier
110
     * @param $system
111
     * @param $message
112
     * @param $status
113
     * @param $value
114
     * @param $tool
115
     * @param $component
116
     * @param Attribute[] $attributes
117
     */
118 View Code Duplication
    private function send($identifier, $system, $message, $status, $value, $tool, $component, $attributes = [], $url = "")
119
    {
120
        if ($status !== CheckResult::STATUS_NONE) {
121
            $event = new Event($identifier, $system, $status, $tool, $message, $value, $url, $component);
122
            $event->addAttribute(new Attribute('_config', json_encode($this->config->getConfigArray()), true));
123
124
            foreach ($attributes as $attribute) {
125
                $event->addAttribute($attribute);
126
            }
127
128
            try {
129
                $this->reporter->sendEvent($event);
130
            } catch (KoalamonException $e) {
131
                $this->output->writeln("\n  <error> Error sending result to leankoala. </error>");
132
                $this->output->writeln('   Url: ' . $e->getUrl());
133
                $this->output->writeln('   Payload: ' . $e->getPayload());
134
                $this->output->writeln("");
135
            } catch (\Exception $e) {
136
                $this->output->writeln($e->getMessage());
137
            }
138
        }
139
    }
140
}
141