Passed
Push — develop ( 065463...53f27d )
by Michael
01:33
created

Zapier::extractDataFromPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

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