Completed
Push — master ( 66e16b...7e3e50 )
by Michael
06:17
created

helper_plugin_prosemirror   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 50
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSyntaxFromProsemirrorData() 0 11 2
A tryToLogErrorToSentry() 0 19 2
1
<?php
2
3
use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder;
4
use dokuwiki\plugin\sentry\Event;
1 ignored issue
show
Bug introduced by
The type dokuwiki\plugin\sentry\Event 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 prosemirror (Helper Component)
8
 *
9
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10
 * @author  Andreas Gohr <[email protected]>
11
 */
12
class helper_plugin_prosemirror extends DokuWiki_Plugin
1 ignored issue
show
Bug introduced by
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...
13
{
14
15
    /**
16
     * Decode json and parse the data back into DokuWiki Syntax
17
     *
18
     * @param string $unparsedJSON the json produced by Prosemirror
19
     *
20
     * @return null|string DokuWiki syntax or null on error
21
     */
22
    public function getSyntaxFromProsemirrorData($unparsedJSON)
23
    {
24
        $prosemirrorData = json_decode($unparsedJSON, true);
25
        if ($prosemirrorData === null) {
26
            $errorMsg = 'Error decoding prosemirror json ' . json_last_error_msg();
27
            throw new RuntimeException($errorMsg);
28
        }
29
30
        $rootNode = SyntaxTreeBuilder::parseDataIntoTree($prosemirrorData);
31
        $syntax = $rootNode->toSyntax();
32
        return $syntax;
33
    }
34
35
    /**
36
     * Try to log an error to sentry if the sentry plugin exists
37
     *
38
     * @param Throwable $exception
39
     * @param array     $extraData associative array for sentries `extra` field
40
     *
41
     * @return bool true if the exception has been logged to sentry, false otherwise
42
     */
43
    public function tryToLogErrorToSentry(Throwable $exception, array $extraData = [])
44
    {
45
        global $ID;
46
47
        /** @var helper_plugin_sentry $sentry */
48
        $sentry = plugin_load('helper', 'sentry');
1 ignored issue
show
Bug introduced by
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

48
        $sentry = /** @scrutinizer ignore-call */ plugin_load('helper', 'sentry');
Loading history...
49
        if (!$sentry) {
0 ignored issues
show
introduced by
$sentry is of type helper_plugin_sentry, thus it always evaluated to true. If $sentry can have other possible types, add them to helper.php:47
Loading history...
50
            return false;
51
        }
52
        $sentryEvent = new Event([
53
            'extra' => $extraData,
54
            'tags' => [
55
                'plugin' => 'prosemirror',
56
                'id' => $ID,
57
            ],
58
        ]);
59
        $sentryEvent->addException($exception);
60
        $sentry->logEvent($sentryEvent);
61
        return true;
62
    }
63
}
64
// vim:ts=4:sw=4:et:
65