KoalamonSummaryReporter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 122
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 21 2
A processResults() 0 10 3
A finish() 0 14 2
A send() 0 22 5
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\WebhookReporter 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;
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
77
    /**
78
     * @param CheckResult[] $results
79
     */
80
    public function processResults($results)
81
    {
82
        foreach ($results as $result) {
83
            if($result->getStatus() == CheckResult::STATUS_SUCCESS) {
84
                $this->success++;
85
            } else {
86
                $this->failed++;
87
            }
88
        }
89
    }
90
91
    public function finish()
92
    {
93
        $message = 'Checks: ';
94
        $message .= $this->success . ' succeeded, ';
95
        $message .= $this->failed . ' failed. ';
96
97
        if ($this->failed > 0) {
98
            $status = Event::STATUS_FAILURE;
99
        } else {
100
            $status = Event::STATUS_SUCCESS;
101
        }
102
103
        $this->send($this->identifier, $this->system, $message, $status, $this->failed, $this->tool, $this->system, [], $this->url);
104
    }
105
106
    /**
107
     * @param $identifier
108
     * @param $system
109
     * @param $message
110
     * @param $status
111
     * @param $value
112
     * @param $tool
113
     * @param $component
114
     * @param Attribute[] $attributes
115
     */
116
    private function send($identifier, $system, $message, $status, $value, $tool, $component, $attributes = [], $url = "")
117
    {
118
        if ($status !== CheckResult::STATUS_NONE) {
119
            $event = new Event($identifier, $system, $status, $tool, $message, $value, $url, $component);
120
            $event->addAttribute(new Attribute('_config', json_encode($this->config->getConfigArray()), true));
121
122
            foreach ($attributes as $attribute) {
123
                $event->addAttribute($attribute);
124
            }
125
126
            try {
127
                $this->reporter->sendEvent($event);
128
            } catch (KoalamonException $e) {
129
                $this->output->writeln("\n  <error> Error sending result to leankoala. </error>");
130
                $this->output->writeln('   Url: ' . $e->getUrl());
131
                $this->output->writeln('   Payload: ' . $e->getPayload());
132
                $this->output->writeln("");
133
            } catch (\Exception $e) {
134
                $this->output->writeln($e->getMessage());
135
            }
136
        }
137
    }
138
}
139