Passed
Push — develop ( fcfb7f...7a8492 )
by Michael
01:17
created

helper.php (7 issues)

Labels
Severity
1
<?php
2
use dokuwiki\plugin\struct\meta\AccessTable;
1 ignored issue
show
The type dokuwiki\plugin\struct\meta\AccessTable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
3
use dokuwiki\plugin\struct\meta\SchemaImporter;
1 ignored issue
show
The type dokuwiki\plugin\struct\meta\SchemaImporter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use dokuwiki\plugin\struct\meta\StructException;
1 ignored issue
show
The type dokuwiki\plugin\struct\meta\StructException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
6
/**
7
 * DokuWiki Plugin swarmzapierstructwebhook (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_swarmzapierstructwebhook extends DokuWiki_Plugin
1 ignored issue
show
The type DokuWiki_Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
{
15
16
    /**
17
     * Extract the data to be saved from the payload
18
     *
19
     * @param array $data
20
     *
21
     * @return array
22
     */
23 2
    public function extractDataFromPayload(array $data)
24
    {
25 2
        $checkinID = $data['id'];
26 2
        $locationName = $data['venue']['name'];
27
28 2
        $dateTime = $this->getDateTimeInstance($data['createdAt'], $data['timeZoneOffset']);
29
30
        $lookupData = [
31 2
            'date' => $dateTime->format('Y-m-d'),
32 2
            'time' => $dateTime->format(DateTime::ATOM),
33 2
            'checkinid' => $checkinID,
34 2
            'locname' => $locationName,
35
        ];
36 2
        if (!empty($data['shout'])) {
37 1
            $lookupData['shout'] = $data['shout'];
38
        }
39 2
        return $lookupData;
40
    }
41
42
    /**
43
     * Transforms a timestamp and the timezone offset as provided in the payload into an DateTimeInterface instance
44
     *
45
     * @param int $timestamp
46
     * @param int $payloadTimezoneOffset offset to UTC in minutes
47
     *
48
     * @return DateTimeInterface
49
     */
50 2
    protected function getDateTimeInstance($timestamp, $payloadTimezoneOffset)
51
    {
52 2
        $tzSign = $payloadTimezoneOffset >= 0 ? '+' : '-';
53 2
        $offsetInHours = $payloadTimezoneOffset / 60;
54 2
        $tz = $tzSign . str_pad($offsetInHours * 100, 4, '0', STR_PAD_LEFT);
55 2
        $dateTime = new DateTime('now', new DateTimeZone($tz));
56 2
        $dateTime->setTimestamp($timestamp);
57 2
        return $dateTime;
58
    }
59
60
    /**
61
     * @param array $data associative array in the form of [columnname => columnvalue]
62
     */
63
    public function saveDataToLookup(array $data)
64
    {
65
        $access = AccessTable::byTableName('swarm', 0, 0);
66
        if (!$access->getSchema()->isEditable()) {
67
            throw new StructException('lookup save error: no permission for schema');
68
        }
69
        $validator = $access->getValidator($data);
70
        if (!$validator->validate()) {
71
            throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors()));
72
        }
73
        if (!$validator->saveData()) {
74
            throw new StructException('No data saved');
75
        }
76
    }
77
78
    /**
79
     * Deletes a checkin from the lookup
80
     *
81
     * @param string $checkinid
82
     */
83
    public function deleteCheckinFromLookup($checkinid)
84
    {
85
        $tablename = 'swarm';
86
87
        /** @var remote_plugin_struct $remote */
88
        $remote = plugin_load('remote', 'struct');
1 ignored issue
show
The function plugin_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $remote = /** @scrutinizer ignore-call */ plugin_load('remote', 'struct');
Loading history...
89
        $rows = $remote->getAggregationData(
90
            [$tablename],
91
            ['%rowid%'],
92
            [['logic'=> 'and', 'condition' => "checkinid = $checkinid"]]
93
        );
94
95
        $pids = array_column($rows, '%rowid%');
96
97
        if (empty($pids)) {
98
            return;
99
        }
100
        foreach ($pids as $pid) { // should only be a single entry
101
            $schemadata = AccessTable::byTableName($tablename, $pid);
102
            if (!$schemadata->getSchema()->isEditable()) {
103
                throw new StructException('lookup delete error: no permission for schema');
104
            }
105
            $schemadata->clearData();
106
        }
107
    }
108
109
    /**
110
     * Create a new struct schema from the struct json file in the plugin dir
111
     */
112
    public function createNewSwarmSchema()
113
    {
114
        $json = file_get_contents(__DIR__ . '/swarm.struct.json');
115
        $builder = new SchemaImporter('swarm', $json, true);
116
        if (!$builder->build()) {
117
            msg('something went wrong while saving', -1);
1 ignored issue
show
The function msg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
            /** @scrutinizer ignore-call */ 
118
            msg('something went wrong while saving', -1);
Loading history...
118
        }
119
        touch(action_plugin_struct_cache::getSchemaRefreshFile());
1 ignored issue
show
The type action_plugin_struct_cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
    }
121
}
122