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

handle_preprocess()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 7
nc 5
nop 2
dl 0
loc 31
rs 8.4906
c 4
b 1
f 0
1
<?php
2
/**
3
 * DokuWiki Plugin prosemirror (Action Component)
4
 *
5
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6
 * @author  Andreas Gohr <[email protected]>
7
 */
8
9
// must be run within Dokuwiki
10
use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder;
11
12
if (!defined('DOKU_INC')) {
13
    die();
14
}
15
16
class action_plugin_prosemirror_parser extends DokuWiki_Action_Plugin
1 ignored issue
show
Bug introduced by
The type DokuWiki_Action_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...
17
{
18
19
    /**
20
     * Registers a callback function for a given event
21
     *
22
     * @param Doku_Event_Handler $controller DokuWiki's event controller object
23
     *
24
     * @return void
25
     */
26
    public function register(Doku_Event_Handler $controller)
1 ignored issue
show
Bug introduced by
The type Doku_Event_Handler 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...
27
    {
28
        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_preprocess');
29
        $controller->register_hook('DRAFT_SAVE', 'BEFORE', $this, 'handle_draft');
30
    }
31
32
    /**
33
     * Triggered by: COMMON_DRAFT_SAVE
34
     *
35
     * @param Doku_Event $event
36
     * @param            $param
37
     */
38
    public function handle_draft(Doku_Event $event, $param)
1 ignored issue
show
Bug introduced by
The type Doku_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...
39
    {
40
        global $INPUT;
41
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
42
        if (empty($unparsedJSON)) {
43
            return;
44
        }
45
46
47
        /** @var \helper_plugin_prosemirror $helper */
48
        $helper = plugin_load('helper', 'prosemirror');
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
        $helper = /** @scrutinizer ignore-call */ plugin_load('helper', 'prosemirror');
Loading history...
49
50
        try {
51
            $syntax = $helper->getSyntaxFromProsemirrorData($unparsedJSON);
52
        } catch (\Throwable $e) {
53
            $event->preventDefault();
54
            $event->stopPropagation();
55
56
            $errorMsg = $e->getMessage();
57
58
            if ($helper->tryToLogErrorToSentry($e, ['json' => $unparsedJSON])) {
59
                $errorMsg .= ' -- The error has been logged to Sentry.';
60
            }
61
62
            $event->data['errors'][] = $errorMsg;
63
            return;
64
        }
65
66
        $event->data['text'] = $syntax;
67
    }
68
69
    /**
70
     * [Custom event handler which performs action]
71
     *
72
     * Triggered by: ACTION_ACT_PREPROCESS
73
     *
74
     * @param Doku_Event $event  event object by reference
75
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
76
     *                           handler was registered]
77
     *
78
     * @return void
79
     */
80
    public function handle_preprocess(Doku_Event $event, $param)
81
    {
82
        global $TEXT, $INPUT;
83
        if ($INPUT->server->str('REQUEST_METHOD') !== 'POST'
84
            || !in_array($event->data, ['save', 'preview'])
85
            || !$INPUT->post->has('prosemirror_json')
86
        ) {
87
            return;
88
        }
89
90
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
91
92
        /** @var \helper_plugin_prosemirror $helper */
93
        $helper = plugin_load('helper', 'prosemirror');
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

93
        $helper = /** @scrutinizer ignore-call */ plugin_load('helper', 'prosemirror');
Loading history...
94
        try {
95
            $syntax = $helper->getSyntaxFromProsemirrorData($unparsedJSON);
96
        } catch (\Throwable $e) {
97
            $event->preventDefault();
98
            $event->stopPropagation();
99
100
            $errorMsg = $e->getMessage();
101
102
            if ($helper->tryToLogErrorToSentry($e, ['json' => $unparsedJSON])) {
103
                $errorMsg .= ' -- The error has been logged to Sentry.';
104
            }
105
106
            msg($errorMsg, -1);
1 ignored issue
show
Bug introduced by
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

106
            /** @scrutinizer ignore-call */ 
107
            msg($errorMsg, -1);
Loading history...
107
            return;
108
        }
109
        if (!empty($syntax)) {
110
            $TEXT = $syntax;
111
        }
112
    }
113
114
}
115
116
// vim:ts=4:sw=4:et:
117