BloggerApi   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 47
eloc 182
dl 0
loc 262
rs 8.64
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUserInfo() 0 13 2
A getUsersBlogs() 0 12 2
B getRecentPosts() 0 44 9
A getTemplate() 0 15 4
B editPost() 0 40 9
B newPost() 0 42 9
A deletePost() 0 10 2
B getPost() 0 34 7
A setTemplate() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like BloggerApi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BloggerApi, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
21
require_once XOOPS_ROOT_PATH . '/class/xml/rpc/xmlrpcapi.php';
22
23
/**
24
 * Class BloggerApi
25
 */
26
class BloggerApi extends XoopsXmlRpcApi
27
{
28
    /**
29
     * @param $params
30
     * @param $response
31
     * @param $module
32
     */
33
    public function __construct(&$params, &$response, &$module)
34
    {
35
        parent::__construct($params, $response, $module);
36
        $this->_setXoopsTagMap('storyid', 'postid');
37
        $this->_setXoopsTagMap('published', 'dateCreated');
38
        $this->_setXoopsTagMap('uid', 'userid');
39
    }
40
41
    public function newPost()
42
    {
43
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
44
            $this->response->add(new XoopsXmlRpcFault(104));
45
        } else {
46
            if (!$fields =& $this->_getPostFields(null, $this->params[1])) {
47
                $this->response->add(new XoopsXmlRpcFault(106));
48
            } else {
49
                $missing = array();
50
                $post    = array();
51
                foreach ($fields as $tag => $detail) {
52
                    $maptag = $this->_getXoopsTagMap($tag);
53
                    $data   = $this->_getTagCdata($this->params[4], $maptag, true);
54
                    if (trim($data) == '') {
55
                        if ($detail['required']) {
56
                            $missing[] = $maptag;
57
                        }
58
                    } else {
59
                        $post[$tag] = $data;
60
                    }
61
                }
62
                if (count($missing) > 0) {
63
                    $msg = '';
64
                    foreach ($missing as $m) {
65
                        $msg .= '<' . $m . '> ';
66
                    }
67
                    $this->response->add(new XoopsXmlRpcFault(109, $msg));
68
                } else {
69
                    $newparams = array();
70
                    // Xoops Api ignores App key
71
                    $newparams[0] = $this->params[1];
72
                    $newparams[1] = $this->params[2];
73
                    $newparams[2] = $this->params[3];
74
                    foreach ($post as $key => $value) {
75
                        $newparams[3][$key] =& $value;
76
                        unset($value);
77
                    }
78
                    $newparams[3]['xoops_text'] = $this->params[4];
79
                    $newparams[4]               = $this->params[5];
80
                    $xoopsapi                   =& $this->_getXoopsApi($newparams);
81
                    $xoopsapi->_setUser($this->user, $this->isadmin);
82
                    $xoopsapi->newPost();
83
                }
84
            }
85
        }
86
    }
87
88
    public function editPost()
89
    {
90
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
91
            $this->response->add(new XoopsXmlRpcFault(104));
92
        } else {
93
            if (!$fields =& $this->_getPostFields($this->params[1])) {
94
            } else {
95
                $missing = array();
96
                $post    = array();
97
                foreach ($fields as $tag => $detail) {
98
                    $data = $this->_getTagCdata($this->params[4], $tag, true);
99
                    if (trim($data) == '') {
100
                        if ($detail['required']) {
101
                            $missing[] = $tag;
102
                        }
103
                    } else {
104
                        $post[$tag] = $data;
105
                    }
106
                }
107
                if (count($missing) > 0) {
108
                    $msg = '';
109
                    foreach ($missing as $m) {
110
                        $msg .= '<' . $m . '> ';
111
                    }
112
                    $this->response->add(new XoopsXmlRpcFault(109, $msg));
113
                } else {
114
                    $newparams = array();
115
                    // XOOPS API ignores App key (index 0 of params)
116
                    $newparams[0] = $this->params[1];
117
                    $newparams[1] = $this->params[2];
118
                    $newparams[2] = $this->params[3];
119
                    foreach ($post as $key => $value) {
120
                        $newparams[3][$key] =& $value;
121
                        unset($value);
122
                    }
123
                    $newparams[3]['xoops_text'] = $this->params[4];
124
                    $newparams[4]               = $this->params[5];
125
                    $xoopsapi                   =& $this->_getXoopsApi($newparams);
126
                    $xoopsapi->_setUser($this->user, $this->isadmin);
127
                    $xoopsapi->editPost();
128
                }
129
            }
130
        }
131
    }
132
133
    public function deletePost()
134
    {
135
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
136
            $this->response->add(new XoopsXmlRpcFault(104));
137
        } else {
138
            // XOOPS API ignores App key (index 0 of params)
139
            array_shift($this->params);
140
            $xoopsapi =& $this->_getXoopsApi($this->params);
141
            $xoopsapi->_setUser($this->user, $this->isadmin);
142
            $xoopsapi->deletePost();
143
        }
144
    }
145
146
    public function getPost()
147
    {
148
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
149
            $this->response->add(new XoopsXmlRpcFault(104));
150
        } else {
151
            // XOOPS API ignores App key (index 0 of params)
152
            array_shift($this->params);
153
            $xoopsapi =& $this->_getXoopsApi($this->params);
154
            $xoopsapi->_setUser($this->user, $this->isadmin);
155
            $ret =& $xoopsapi->getPost(false);
156
            if (is_array($ret)) {
0 ignored issues
show
introduced by
The condition is_array($ret) is always true.
Loading history...
157
                $struct  = new XoopsXmlRpcStruct();
158
                $content = '';
159
                foreach ($ret as $key => $value) {
160
                    $maptag = $this->_getXoopsTagMap($key);
161
                    switch ($maptag) {
162
                        case 'userid':
163
                            $struct->add('userid', new XoopsXmlRpcString($value));
164
                            break;
165
                        case 'dateCreated':
166
                            $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
167
                            break;
168
                        case 'postid':
169
                            $struct->add('postid', new XoopsXmlRpcString($value));
170
                            break;
171
                        default :
172
                            $content .= '<' . $key . '>' . trim($value) . '</' . $key . '>';
173
                            break;
174
                    }
175
                }
176
                $struct->add('content', new XoopsXmlRpcString($content));
177
                $this->response->add($struct);
178
            } else {
179
                $this->response->add(new XoopsXmlRpcFault(106));
180
            }
181
        }
182
    }
183
184
    public function getRecentPosts()
185
    {
186
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
187
            $this->response->add(new XoopsXmlRpcFault(104));
188
        } else {
189
            // XOOPS API ignores App key (index 0 of params)
190
            array_shift($this->params);
191
            $xoopsapi =& $this->_getXoopsApi($this->params);
192
            $xoopsapi->_setUser($this->user, $this->isadmin);
193
            $ret =& $xoopsapi->getRecentPosts(false);
194
            if (is_array($ret)) {
0 ignored issues
show
introduced by
The condition is_array($ret) is always true.
Loading history...
195
                $arr   = new XoopsXmlRpcArray();
196
                $count = count($ret);
197
                if ($count == 0) {
198
                    $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
199
                } else {
200
                    for ($i = 0; $i < $count; ++$i) {
201
                        $struct  = new XoopsXmlRpcStruct();
202
                        $content = '';
203
                        foreach ($ret[$i] as $key => $value) {
204
                            $maptag = $this->_getXoopsTagMap($key);
205
                            switch ($maptag) {
206
                                case 'userid':
207
                                    $struct->add('userid', new XoopsXmlRpcString($value));
208
                                    break;
209
                                case 'dateCreated':
210
                                    $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
211
                                    break;
212
                                case 'postid':
213
                                    $struct->add('postid', new XoopsXmlRpcString($value));
214
                                    break;
215
                                default :
216
                                    $content .= '<' . $key . '>' . trim($value) . '</' . $key . '>';
217
                                    break;
218
                            }
219
                        }
220
                        $struct->add('content', new XoopsXmlRpcString($content));
221
                        $arr->add($struct);
222
                        unset($struct);
223
                    }
224
                    $this->response->add($arr);
225
                }
226
            } else {
227
                $this->response->add(new XoopsXmlRpcFault(106));
228
            }
229
        }
230
    }
231
232
    public function getUsersBlogs()
233
    {
234
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
235
            $this->response->add(new XoopsXmlRpcFault(104));
236
        } else {
237
            $arr    = new XoopsXmlRpcArray();
238
            $struct = new XoopsXmlRpcStruct();
239
            $struct->add('url', new XoopsXmlRpcString(XOOPS_URL . '/modules/' . $this->module->getVar('dirname') . '/'));
240
            $struct->add('blogid', new XoopsXmlRpcString($this->module->getVar('mid')));
241
            $struct->add('blogName', new XoopsXmlRpcString('XOOPS Blog'));
242
            $arr->add($struct);
243
            $this->response->add($arr);
244
        }
245
    }
246
247
    public function getUserInfo()
248
    {
249
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
250
            $this->response->add(new XoopsXmlRpcFault(104));
251
        } else {
252
            $struct = new XoopsXmlRpcStruct();
253
            $struct->add('nickname', new XoopsXmlRpcString($this->user->getVar('uname')));
254
            $struct->add('userid', new XoopsXmlRpcString($this->user->getVar('uid')));
255
            $struct->add('url', new XoopsXmlRpcString($this->user->getVar('url')));
256
            $struct->add('email', new XoopsXmlRpcString($this->user->getVar('email')));
257
            $struct->add('lastname', new XoopsXmlRpcString(''));
258
            $struct->add('firstname', new XoopsXmlRpcString($this->user->getVar('name')));
259
            $this->response->add($struct);
260
        }
261
    }
262
263
    public function getTemplate()
264
    {
265
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
266
            $this->response->add(new XoopsXmlRpcFault(104));
267
        } else {
268
            switch ($this->params[5]) {
269
                case 'main':
270
                    $this->response->add(new XoopsXmlRpcFault(107));
271
                    break;
272
                case 'archiveIndex':
273
                    $this->response->add(new XoopsXmlRpcFault(107));
274
                    break;
275
                default:
276
                    $this->response->add(new XoopsXmlRpcFault(107));
277
                    break;
278
            }
279
        }
280
    }
281
282
    public function setTemplate()
283
    {
284
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
285
            $this->response->add(new XoopsXmlRpcFault(104));
286
        } else {
287
            $this->response->add(new XoopsXmlRpcFault(107));
288
        }
289
    }
290
}
291