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

action_plugin_prosemirror_ajax::switchEditors()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 2
dl 0
loc 59
rs 7.9612
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
11
use dokuwiki\plugin\prosemirror\parser\LinkNode;
12
13
if (!defined('DOKU_INC')) {
14
    die();
15
}
16
17
class action_plugin_prosemirror_ajax 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...
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('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
29
        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'switchEditors');
30
    }
31
32
    /**
33
     * [Custom event handler which performs action]
34
     *
35
     * Event: AJAX_CALL_UNKNOWN
36
     *
37
     * @param Doku_Event $event  event object by reference
38
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
39
     *                           handler was registered]
40
     *
41
     * @return void
42
     */
43
    public function handleAjax(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...
44
    {
45
        if ($event->data !== 'plugin_prosemirror') {
46
            return;
47
        }
48
        $event->preventDefault();
49
        $event->stopPropagation();
50
51
        global $INPUT, $ID;
52
        $ID = $INPUT->str('id');
53
        $responseData = [];
54
        foreach ($INPUT->arr('actions') as $action) {
55
            switch ($action) {
56
                case 'resolveInternalLink':
57
                    {
58
                        $inner = $INPUT->str('inner');
59
                        $responseData[$action] = $this->resolveInternalLink($inner, $ID);
60
                        break;
61
                    }
62
                case 'resolveInterWikiLink':
63
                    {
64
                        $inner = $INPUT->str('inner');
65
                        list($shortcut, $reference) = explode('>', $inner);
66
                        $responseData[$action] = $this->resolveInterWikiLink($shortcut, $reference);
67
                        break;
68
                    }
69
                case 'resolveMedia':
70
                    {
71
                        $attrs = $INPUT->arr('attrs');
72
                        $responseData[$action] = [
73
                            'data-resolvedHtml' => \dokuwiki\plugin\prosemirror\parser\ImageNode::resolveMedia(
74
                                $attrs['id'],
75
                                $attrs['title'],
76
                                $attrs['align'],
77
                                $attrs['width'],
78
                                $attrs['height'],
79
                                $attrs['cache'],
80
                                $attrs['linking']
81
                            )
82
                        ];
83
                        break;
84
                    }
85
                case 'resolveImageTitle':
86
                    {
87
                        $image = $INPUT->arr('image');
88
                        $responseData[$action] = [];
89
                        $responseData[$action]['data-resolvedImage'] = LinkNode::resolveImageTitle(
90
                            $ID,
91
                            $image['id'],
92
                            $image['title'],
93
                            $image['align'],
94
                            $image['width'],
95
                            $image['height'],
96
                            $image['cache']
97
                        );
98
                        break;
99
                    }
100
                default:
101
                    {
102
                        dbglog('Unknown action: ' . $INPUT->str('action'), __FILE__ . ': ' . __LINE__);
1 ignored issue
show
Bug introduced by
The function dbglog 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

102
                        /** @scrutinizer ignore-call */ 
103
                        dbglog('Unknown action: ' . $INPUT->str('action'), __FILE__ . ': ' . __LINE__);
Loading history...
103
                        http_status(400, 'unknown action');
1 ignored issue
show
Bug introduced by
The function http_status 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

103
                        /** @scrutinizer ignore-call */ 
104
                        http_status(400, 'unknown action');
Loading history...
104
                        return;
105
                    }
106
            }
107
        }
108
109
        echo json_encode($responseData);
110
    }
111
112
    protected function resolveInterWikiLink($shortcut, $reference)
113
    {
114
        $xhtml_renderer = p_get_renderer('xhtml');
1 ignored issue
show
Bug introduced by
The function p_get_renderer 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

114
        $xhtml_renderer = /** @scrutinizer ignore-call */ p_get_renderer('xhtml');
Loading history...
115
        $xhtml_renderer->interwiki = getInterwiki();
1 ignored issue
show
Bug introduced by
The function getInterwiki 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

115
        $xhtml_renderer->interwiki = /** @scrutinizer ignore-call */ getInterwiki();
Loading history...
116
        $url = $xhtml_renderer->_resolveInterWiki($shortcut, $reference, $exits);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $exits seems to be never defined.
Loading history...
117
        return [
118
            'url' => $url,
119
            'resolvedClass' => 'interwikilink interwiki iw_' . $shortcut,
120
        ];
121
    }
122
123
    protected function resolveInternalLink($inner, $curId)
124
    {
125
        if ($inner[0] === '#') {
126
            return dokuwiki\plugin\prosemirror\parser\LocalLinkNode::resolveLocalLink($inner, $curId);
127
        }
128
       return \dokuwiki\plugin\prosemirror\parser\InternalLinkNode::resolveLink($inner, $curId);
129
    }
130
131
    /**
132
     * [Custom event handler which performs action]
133
     *
134
     * Event: AJAX_CALL_UNKNOWN
135
     *
136
     * @param Doku_Event $event  event object by reference
137
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
138
     *                           handler was registered]
139
     *
140
     * @return void
141
     */
142
    public function switchEditors(Doku_Event $event, $param)
143
    {
144
        if ($event->data !== 'plugin_prosemirror_switch_editors') {
145
            return;
146
        }
147
        $event->preventDefault();
148
        $event->stopPropagation();
149
150
        global $INPUT;
151
152
        if ($INPUT->bool('getJSON')) {
153
            $text = $INPUT->str('data');
154
            $instructions = p_get_instructions($text);
1 ignored issue
show
Bug introduced by
The function p_get_instructions 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

154
            $instructions = /** @scrutinizer ignore-call */ p_get_instructions($text);
Loading history...
155
            try {
156
                $prosemirrorJSON = p_render('prosemirror', $instructions, $info);
1 ignored issue
show
Bug introduced by
The function p_render 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

156
                $prosemirrorJSON = /** @scrutinizer ignore-call */ p_render('prosemirror', $instructions, $info);
Loading history...
Comprehensibility Best Practice introduced by
The variable $info seems to be never defined.
Loading history...
157
            } catch (Throwable $e) {
158
                $errorMsg = 'Rendering the page\'s syntax for the WYSIWYG editor failed';
159
160
                /** @var \helper_plugin_prosemirror $helper */
161
                $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

161
                $helper = /** @scrutinizer ignore-call */ plugin_load('helper', 'prosemirror');
Loading history...
162
                if ($helper->tryToLogErrorToSentry($e, ['text' => $text])) {
163
                    $errorMsg .= ' -- The error has been logged to Sentry.';
164
                }
165
166
                http_status(500);
1 ignored issue
show
Bug introduced by
The function http_status 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

166
                /** @scrutinizer ignore-call */ 
167
                http_status(500);
Loading history...
167
                header('Content-Type: application/json');
168
                echo json_encode(['error' => $errorMsg]);
169
                return;
170
            }
171
            $responseData = [
172
                'json' => $prosemirrorJSON,
173
            ];
174
        } else {
175
            /** @var \helper_plugin_prosemirror $helper */
176
            $helper = plugin_load('helper', 'prosemirror');
177
            $json = $INPUT->str('data');
178
            try {
179
                $syntax = $helper->getSyntaxFromProsemirrorData($json);
180
            } catch (Throwable $e) {
181
                $errorMsg = 'Parsing the data generated by Prosemirror failed with message: "';
182
                $errorMsg .= $e->getMessage();
183
                $errorMsg .= '"';
184
185
                if ($helper->tryToLogErrorToSentry($e, ['json' => $json])) {
186
                    $errorMsg .= ' -- The error has been logged to Sentry.';
187
                }
188
189
                http_status(500);
190
                header('Content-Type: application/json');
191
                echo json_encode(['error' => $errorMsg]);
192
                return;
193
            }
194
            $responseData = [
195
                'text' => $syntax,
196
            ];
197
        }
198
199
        header('Content-Type: application/json');
200
        echo json_encode($responseData);
201
    }
202
}
203