Completed
Push — develop ( 4811e5...9083e6 )
by Michael
01:49
created

helper_plugin_swarmzapierstructwebhook   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 87.18%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 12
dl 0
loc 81
ccs 34
cts 39
cp 0.8718
rs 10
c 6
b 1
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A helper_plugin_swarmwebhook::saveDataToLookup() 0 12 4
A helper_plugin_swarmwebhook::getDateTimeInstance() 0 8 2
A helper_plugin_swarmwebhook::createNewSwarmSchema() 0 8 2
B helper_plugin_swarmwebhook::deleteCheckinFromLookup() 0 23 4
1
<?php
2
use dokuwiki\plugin\struct\meta\AccessTable;
3
use dokuwiki\plugin\struct\meta\SchemaImporter;
4
use dokuwiki\plugin\struct\meta\StructException;
5
6
/**
7
 * DokuWiki Plugin swarmwebhook (Helper Component)
8
 *
9
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10
 * @author  Michael Große <[email protected]>
11
 */
12
13
class helper_plugin_swarmwebhook extends DokuWiki_Plugin
14
{
15
16
    /**
17
     * Transforms a timestamp and the timezone offset as provided in the payload into an DateTimeInterface instance
18
     *
19
     * @param int $timestamp
20
     * @param int $payloadTimezoneOffset offset to UTC in minutes
21
     *
22
     * @return DateTimeInterface
23
     */
24 4
    public function getDateTimeInstance($timestamp, $payloadTimezoneOffset)
25
    {
26 4
        $tzSign = $payloadTimezoneOffset >= 0 ? '+' : '-';
27 4
        $offsetInHours = $payloadTimezoneOffset / 60;
28 4
        $tz = $tzSign . str_pad($offsetInHours * 100, 4, '0', STR_PAD_LEFT);
29 4
        $dateTime = new DateTime('now', new DateTimeZone($tz));
30 4
        $dateTime->setTimestamp($timestamp);
31 4
        return $dateTime;
32
    }
33
34
    /**
35
     * @param array $data associative array in the form of [columnname => columnvalue]
36
     */
37 3
    public function saveDataToLookup(array $data)
38
    {
39 3
        $access = AccessTable::byTableName('swarm', 0, 0);
40 3
        if (!$access->getSchema()->isEditable()) {
41
            throw new StructException('lookup save error: no permission for schema');
42
        }
43 3
        $validator = $access->getValidator($data);
44 3
        if (!$validator->validate()) {
45
            throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors()));
46
        }
47 3
        if (!$validator->saveData()) {
48
            throw new StructException('No data saved');
49
        }
50 3
    }
51
52
    /**
53
     * Deletes a checkin from the lookup
54
     *
55
     * @param string $checkinid
56
     */
57 3
    public function deleteCheckinFromLookup($checkinid)
58
    {
59 3
        $tablename = 'swarm';
60
61
        /** @var remote_plugin_struct $remote */
62 3
        $remote = plugin_load('remote', 'struct');
63 3
        $rows = $remote->getAggregationData(
64 3
            [$tablename],
65 3
            ['%rowid%'],
66 3
            [['logic'=> 'and', 'condition' => "checkinid = $checkinid"]]
67
        );
68
69 3
        $pids = array_column($rows, '%rowid%');
70
71 3
        if (empty($pids)) {
72 2
            return;
73
        }
74 1
        foreach ($pids as $pid) { // should only be a single entry
75 1
            $schemadata = AccessTable::byTableName($tablename, $pid);
76 1
            if (!$schemadata->getSchema()->isEditable()) {
77
                throw new StructException('lookup delete error: no permission for schema');
78
            }
79 1
            $schemadata->clearData();
80
        }
81 1
    }
82
83
    /**
84
     * Create a new struct schema from the struct json file in the plugin dir
85
     */
86 2
    public function createNewSwarmSchema()
87
    {
88 2
        $json = file_get_contents(__DIR__ . '/swarm.struct.json');
89 2
        $builder = new SchemaImporter('swarm', $json, true);
90 2
        if (!$builder->build()) {
91
            msg('something went wrong while saving', -1);
92
        }
93 2
        touch(action_plugin_struct_cache::getSchemaRefreshFile());
94 2
    }
95
}
96