helper_plugin_swarmwebhook::createNewSwarmSchema()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0185
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
        /** @var helper_plugin_struct $structHelper */
40 3
        $structHelper = plugin_load('helper', 'struct');
41 3
        $access = AccessTable::byTableName('swarm', 0, 0);
42 3
        if (method_exists($structHelper, 'saveLookupData')) {
43 3
            $structHelper->saveLookupData($access, $data);
44
            return;
45
        }
46
47
        // old struct version - we save the data ourselves!
48
49
        dbglog('Please update your struct plugin!');
50
        if (!$access->getSchema()->isEditable()) {
51
            throw new StructException('lookup save error: no permission for schema');
52
        }
53
        $validator = $access->getValidator($data);
54
        if (!$validator->validate()) {
55
            throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors()));
56
        }
57
        if (!$validator->saveData()) {
58
            throw new StructException('No data saved');
59
        }
60
    }
61
62
    /**
63
     * Deletes a checkin from the lookup
64
     *
65
     * @param string $checkinid
66
     */
67 3
    public function deleteCheckinFromLookup($checkinid)
68
    {
69 3
        $tablename = 'swarm';
70
71
        /** @var remote_plugin_struct $remote */
72 3
        $remote = plugin_load('remote', 'struct');
73 3
        $rows = $remote->getAggregationData(
74 3
            [$tablename],
75 3
            ['%rowid%'],
76 3
            [['logic'=> 'and', 'condition' => "checkinid = $checkinid"]]
77
        );
78
79 3
        $pids = array_column($rows, '%rowid%');
80
81 3
        if (empty($pids)) {
82 2
            return;
83
        }
84 1
        foreach ($pids as $pid) { // should only be a single entry
85 1
            $schemadata = AccessTable::byTableName($tablename, $pid);
86 1
            if (!$schemadata->getSchema()->isEditable()) {
87
                throw new StructException('lookup delete error: no permission for schema');
88
            }
89 1
            $schemadata->clearData();
90
        }
91 1
    }
92
93
    /**
94
     * Create a new struct schema from the struct json file in the plugin dir
95
     */
96 2
    public function createNewSwarmSchema()
97
    {
98 2
        $json = file_get_contents(__DIR__ . '/swarm.struct.json');
99 2
        $builder = new SchemaImporter('swarm', $json, true);
100 2
        if (!$builder->build()) {
101
            msg('something went wrong while saving', -1);
102
        }
103 2
        touch(action_plugin_struct_cache::getSchemaRefreshFile());
104 2
    }
105
}
106