Completed
Push — develop ( 53f27d...7115f3 )
by Michael
01:50
created

Zapier::handleWebhookPayload()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.432

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 14
cts 20
cp 0.7
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 10
nop 1
crap 4.432
1
<?php
2
3
namespace dokuwiki\plugin\swarmzapierstructwebhook\webhooks;
4
5
use dokuwiki\plugin\struct\meta\Schema;
6
7
class Zapier extends AbstractWebhook
8
{
9 1
    public function run($json)
10
    {
11 1
        global $conf, $INPUT;
12
13 1
        if ($conf['allowdebug']) {
14
            dbglog($_SERVER);
15
        }
16
17
        /** @var null|\helper_plugin_swarmzapierstructwebhook $helper */
18 1
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
19 1
        if (!$helper) {
20
            http_status(422, 'swarmzapierstructwebhook plugin not active at this server');
21
            return;
22
        }
23 1
        $storedSecret = $helper->getConf('hook secret');
24 1
        if (!empty($storedSecret)) {
25
            $requestSecret = $INPUT->server->str('X_HOOK_SECRET');
26
            if (empty($requestSecret)) {
27
                http_status(401, 'Header X_HOOK_SECRET missing!');
28
                return;
29
            }
30
31
            if ($requestSecret !== $storedSecret) {
32
                http_status(403, 'Header X_HOOK_SECRET not identical with configured secret!');
33
                return;
34
            }
35
        }
36
37 1
        $ok = $this->handleWebhookPayload($json);
38
39 1
        if ($ok) {
40 1
            http_status(202);
41 1
        }
42 1
    }
43
44
    /**
45
     * Stores the webhook's payload to the struct table
46
     *
47
     * FIXME: don't set http status here
48
     *
49
     * @param string $json the original webhooks payload as json
50
     *
51
     * @return bool false if there was an error, http status has already been set, true if everything was ok
52
     */
53 2
    protected function handleWebhookPayload($json)
54
    {
55
        /** @var null|\helper_plugin_struct $struct */
56 2
        $struct = plugin_load('helper', 'struct');
57 2
        if (!$struct) {
58
            http_status(422, 'struct plugin not active at this server');
59
            return false;
60
        }
61
62
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
63 2
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
64
65 2
        $lookupData = $this->extractDataFromPayload(json_decode($json, true));
66 2
        $lookupData['json'] = $json;
67
68
        try {
69 2
            $schemas = Schema::getAll('lookup');
70 2
            if (!in_array('swarm', $schemas)) {
71 1
                $helper->createNewSwarmSchema();
72 1
            }
73
74 2
            $helper->deleteCheckinFromLookup($lookupData['checkinid']);
75 2
            $helper->saveDataToLookup($lookupData);
76 2
        } catch (\Exception $e) { // FIXME: catch more specific exceptions!
77
            $errorMessage = $e->getMessage();
78
            dbglog($errorMessage);
79
            http_status(500, $errorMessage);
80
            return false;
81
        }
82
83 2
        return true;
84
    }
85
86
87
88
    /**
89
     * Extract the data to be saved from the payload
90
     *
91
     * @param array $data
92
     *
93
     * @return array
94
     */
95 4
    protected function extractDataFromPayload(array $data)
96
    {
97 4
        $checkinID = $data['id'];
98 4
        $locationName = $data['venue']['name'];
99
100
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
101 4
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
102 4
        $dateTime = $helper->getDateTimeInstance($data['createdAt'], $data['timeZoneOffset']);
103
104
        $lookupData = [
105 4
            'date' => $dateTime->format('Y-m-d'),
106 4
            'time' => $dateTime->format(\DateTime::ATOM),
107 4
            'checkinid' => $checkinID,
108 4
            'locname' => $locationName,
109 4
            'service' => 'Zapier',
110 4
        ];
111 4
        if (!empty($data['shout'])) {
112 1
            $lookupData['shout'] = $data['shout'];
113 1
        }
114 4
        return $lookupData;
115
    }
116
}
117