Completed
Push — develop ( e816f7...c23763 )
by Michael
01:42
created

deleteCheckinFromLookup()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

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