Webhook::run()   B
last analyzed

Complexity

Conditions 7
Paths 19

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 34
c 1
b 0
f 0
nc 19
nop 0
dl 0
loc 49
ccs 0
cts 42
cp 0
crap 56
rs 8.4426
1
<?php
2
3
namespace dokuwiki\plugin\issuelinks;
4
5
class Webhook extends \DokuWiki_Plugin
6
{
7
8
    public function run()
9
    {
10
        /** @var \helper_plugin_issuelinks_util $util */
11
        $util = plugin_load('helper', 'issuelinks_util');
12
        if (!$util) {
0 ignored issues
show
introduced by
$util is of type helper_plugin_issuelinks_util, thus it always evaluated to true.
Loading history...
13
            http_status(424);
14
            echo 'Plugin is deactived at server. Aborting.';
15
            return;
16
        }
17
        $body = file_get_contents('php://input');
18
19
        global $INPUT;
20
        $userAgent = $INPUT->server->str('HTTP_USER_AGENT');
21
        dbglog($userAgent);
22
        dbglog($INPUT->server);
23
24
        $serviceProvider = classes\ServiceProvider::getInstance();
25
        $services = $serviceProvider->getServices();
26
        $handlingService = null;
27
        foreach ($services as $service) {
28
            if (!$service::isOurWebhook()) {
29
                continue;
30
            }
31
            $handlingService = $service::getInstance();
32
            break;
33
        }
34
35
36
        if ($handlingService === null) {
37
            dbglog('webhook could not be indentified', __FILE__ . ': ' . __LINE__);
38
            dbglog('user agent: ' . $userAgent);
39
            dbglog(json_decode($body, true));
40
            $util->sendResponse(400, 'unknown webhook');
41
            return;
42
        }
43
44
        try {
45
            $validationResult = $handlingService->validateWebhook($body);
46
            if ($validationResult !== true) {
47
                $util->sendResponse($validationResult->code, $validationResult->body);
48
                return;
49
            }
50
            $result = $handlingService->handleWebhook($body);
51
        } catch (\Throwable $e) {
52
            $util->sendResponse(500, $e->getMessage());
53
            return;
54
        }
55
56
        $util->sendResponse($result->code, $result->body);
57
    }
58
}
59