Completed
Push — master ( 8034c6...a22340 )
by Nils
04:00
created

KoalamonReporter::sendSingle()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 30
Code Lines 21

Duplication

Lines 10
Ratio 33.33 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 10
loc 30
rs 5.3846
cc 8
eloc 21
nc 11
nop 0
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use whm\Smoke\Config\Configuration;
7
use whm\Smoke\Scanner\Result;
8
9
/**
10
 * Class XUnitReporter.
11
 */
12
class KoalamonReporter implements Reporter
13
{
14
    /**
15
     * @var Result[]
16
     */
17
    private $results;
18
19
    private $koaloMon = 'http://www.koalamon.com/app_dev.php/webhook/';
20
    private $apiKey;
21
    private $config;
22
    private $system;
23
    private $collect;
24
    private $identifier;
25
26
    private $output;
27
28
    const STATUS_SUCCESS = 'success';
29
    const STATUS_FAILURE = 'failure';
30
31
    public function init($apiKey, $system = "", $identifier = "", $collect = true, Configuration $_configuration, OutputInterface $_output)
32
    {
33
        $this->config = $_configuration;
34
        $this->apiKey = $apiKey;
35
        $this->system = $system;
36
        $this->collect = $collect;
37
        $this->identifier = $identifier;
38
39
        $this->output = $_output;
40
    }
41
42
    /**
43
     * @param Rule [];
44
     */
45
    private function getRuleKeys()
46
    {
47
        $keys = array();
48
        foreach ($this->config->getRules() as $key => $rule) {
49
            $keys[] = $key;
50
        }
51
52
        return $keys;
53
    }
54
55
    public function processResult(Result $result)
56
    {
57
        $this->results[] = $result;
58
    }
59
60
    public function finish()
61
    {
62
        $this->output->writeln("Sending results to www.koalamon.com ... \n");
63
64
        if ($this->collect) {
65
            $this->sendCollected();
66
        } else {
67
            $this->sendSingle();
68
        }
69
    }
70
71
    private function sendSingle()
72
    {
73
        $rules = $this->getRuleKeys();
74
        foreach ($this->results as $result) {
75
            $failedTests = array();
76
            if ($result->isFailure()) {
77
                foreach ($result->getMessages() as $ruleLKey => $message) {
78
                    $identifier = 'smoke_' . $ruleLKey . '_' . $result->getUrl();
79 View Code Duplication
                    if ($this->system == "") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
                        $system = str_replace("http://", "", $result->getUrl());
81
                    } else {
82
                        $system = $this->system;
83
                    }
84
                    $this->send($identifier, $system, 'smoke', $message, self::STATUS_FAILURE, (string)$result->getUrl());
85
                    $failedTests[] = $ruleLKey;
86
                }
87
            }
88
            foreach ($rules as $rule) {
89
                if (!in_array($rule, $failedTests, true)) {
90
                    $identifier = 'smoke_' . $rule . '_' . $result->getUrl();
91 View Code Duplication
                    if ($this->system == "") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
                        $system = str_replace("http://", "", $result->getUrl());
93
                    } else {
94
                        $system = $this->system;
95
                    }
96
                    $this->send($identifier, $system, 'smoke_' . $rule, '', self::STATUS_SUCCESS, (string)$result->getUrl());
97
                }
98
            }
99
        }
100
    }
101
102
    private function sendCollected()
103
    {
104
        $failureMessages = array();
105
106
        foreach ($this->getRuleKeys() as $rule) {
107
            $failureMessages[$rule] = "";
108
        }
109
110
        foreach ($this->results as $result) {
111
            if ($result->isFailure()) {
112
                foreach ($result->getMessages() as $ruleLKey => $message) {
113
                    if ($failureMessages[$ruleLKey] == "") {
114
                        $failureMessages[$ruleLKey] = "    The smoke test for " . $this->system . " failed (Rule: " . $ruleLKey . ").<ul>";
115
                    }
116
                    $failureMessages[$ruleLKey] .= "<li>" . $message . "(url: " . $result->getUrl() . ")</li>";
117
                }
118
            }
119
        }
120
121
        foreach ($failureMessages as $key => $failureMessage) {
122
            if ($failureMessage != "") {
123
                $this->send($this->identifier . '_' . $key, $this->system, 'smoke', $failureMessage . '</ul>', self::STATUS_FAILURE, "");
124
            } else {
125
                $this->send($this->identifier . '_' . $key, $this->system, 'smoke', "", self::STATUS_SUCCESS, "");
126
            }
127
        }
128
    }
129
130
    public function send($identifier, $system, $tool, $message, $status, $url = '')
131
    {
132
        $curl = curl_init();
133
        $responseBody = array(
134
            'system' => $system,
135
            'status' => $status,
136
            'message' => $message,
137
            'identifier' => $identifier,
138
            'type' => $tool,
139
            'url' => $url,
140
        );
141
142
        $koalamonUrl = $this->koaloMon . '?api_key=' . $this->apiKey;
143
        curl_setopt_array($curl, array(
144
            CURLOPT_URL => $koalamonUrl,
145
            CURLOPT_RETURNTRANSFER => true,
146
            CURLOPT_ENCODING => '',
147
            CURLOPT_MAXREDIRS => 10,
148
            CURLOPT_TIMEOUT => 30,
149
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
150
            CURLOPT_CUSTOMREQUEST => 'POST',
151
            CURLOPT_POSTFIELDS => json_encode($responseBody),
152
        ));
153
        $response = curl_exec($curl);
154
155
        $err = curl_error($curl);
156
        curl_close($curl);
157
158
        return $err;
159
    }
160
}
161