Completed
Push — formtime ( a7c473...26356d )
by Richard
06:07
created

MetaWeblogApi   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 249
Duplicated Lines 57.03 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 49
c 1
b 0
f 0
lcom 1
cbo 9
dl 142
loc 249
ccs 6
cts 6
cp 1
rs 8.5454

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
C newPost() 45 55 13
C editPost() 45 53 13
C getPost() 22 41 8
C getRecentPosts() 22 51 10
B getCategories() 0 28 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MetaWeblogApi 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 MetaWeblogApi, 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 (http://xoops.org)
14
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package         class
16
 * @subpackage      xml
17
 * @since           1.0.0
18
 * @author          Kazumi Ono (AKA onokazu)
19
 * @version         $Id $
20
 */
21
class MetaWeblogApi extends XoopsXmlRpcApi
22
{
23
    /**
24
     * @param $params
25
     * @param $response
26
     * @param $module
27
     */
28 1 View Code Duplication
    public function __construct(&$params, &$response, &$module)
29
    {
30 1
        parent::__construct($params, $response, $module);
31 1
        $this->_setXoopsTagMap('storyid', 'postid');
32 1
        $this->_setXoopsTagMap('published', 'dateCreated');
33 1
        $this->_setXoopsTagMap('uid', 'userid');
34
        //$this->_setXoopsTagMap('hometext', 'description');
35 1
    }
36
37
    public function newPost()
38
    {
39
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
40
            $this->response->add(new XoopsXmlRpcFault(104));
41
        } else {
42
            if (!$fields = $this->_getPostFields(null, $this->params[0])) {
43
                $this->response->add(new XoopsXmlRpcFault(106));
44 View Code Duplication
            } else {
45
                $missing = array();
46
                $post = array();
47
                foreach ($fields as $tag => $detail) {
48
                    $maptag = $this->_getXoopsTagMap($tag);
49
                    if (!isset($this->params[3][$maptag])) {
50
                        $data = $this->_getTagCdata($this->params[3]['description'], $maptag, true);
51
                        if (trim($data) == '') {
52
                            if ($detail['required']) {
53
                                $missing[] = $maptag;
54
                            }
55
                        } else {
56
                            $post[$tag] = $data;
57
                        }
58
                    } else {
59
                        $post[$tag] = $this->params[3][$maptag];
60
                    }
61
                }
62
                if (count($missing) > 0) {
63
                    $msg = '';
64
                    foreach ($missing as $m) {
65
                        $msg .= '<' . $m . '> ';
66
                        echo $m;
67
                    }
68
                    $this->response->add(new XoopsXmlRpcFault(109, $msg));
69
                } else {
70
                    $newparams = array();
71
                    $newparams[0] = $this->params[0];
72
                    $newparams[1] = $this->params[1];
73
                    $newparams[2] = $this->params[2];
74
                    foreach ($post as $key => $value) {
75
                        $newparams[3][$key] = $value;
76
                        unset($value);
77
                    }
78
                    $newparams[3]['xoops_text'] = $this->params[3]['description'];
79
                    if (isset($this->params[3]['categories']) && is_array($this->params[3]['categories'])) {
80
                        foreach ($this->params[3]['categories'] as $k => $v) {
81
                            $newparams[3]['categories'][$k] = $v;
82
                        }
83
                    }
84
                    $newparams[4] = $this->params[4];
85
                    $xoopsapi = $this->_getXoopsApi($newparams);
86
                    $xoopsapi->_setUser($this->user, $this->isadmin);
87
                    $xoopsapi->newPost();
88
                }
89
            }
90
        }
91
    }
92
93
    public function editPost()
94
    {
95
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
96
            $this->response->add(new XoopsXmlRpcFault(104));
97
        } else {
98
            if (!$fields = $this->_getPostFields($this->params[0])) {
99 View Code Duplication
            } else {
100
                $missing = array();
101
                $post = array();
102
                foreach ($fields as $tag => $detail) {
103
                    $maptag = $this->_getXoopsTagMap($tag);
104
                    if (!isset($this->params[3][$maptag])) {
105
                        $data = $this->_getTagCdata($this->params[3]['description'], $maptag, true);
106
                        if (trim($data) == ''){
107
                            if ($detail['required']) {
108
                                $missing[] = $tag;
109
                            }
110
                        } else {
111
                            $post[$tag] = $data;
112
                        }
113
                    } else {
114
                        $post[$tag] = $this->params[3][$maptag];
115
                    }
116
                }
117
                if (count($missing) > 0) {
118
                    $msg = '';
119
                    foreach ($missing as $m) {
120
                        $msg .= '<' . $m . '> ';
121
                    }
122
                    $this->response->add(new XoopsXmlRpcFault(109, $msg));
123
                } else {
124
                    $newparams = array();
125
                    $newparams[0] = $this->params[0];
126
                    $newparams[1] = $this->params[1];
127
                    $newparams[2] = $this->params[2];
128
                    foreach ($post as $key => $value) {
129
                        $newparams[3][$key] = $value;
130
                        unset($value);
131
                    }
132
                    if (isset($this->params[3]['categories']) && is_array($this->params[3]['categories'])) {
133
                        foreach ($this->params[3]['categories'] as $k => $v) {
134
                            $newparams[3]['categories'][$k] = $v;
135
                        }
136
                    }
137
                    $newparams[3]['xoops_text'] = $this->params[3]['description'];
138
                    $newparams[4] = $this->params[4];
139
                    $xoopsapi = $this->_getXoopsApi($newparams);
140
                    $xoopsapi->_setUser($this->user, $this->isadmin);
141
                    $xoopsapi->editPost();
142
                }
143
            }
144
        }
145
    }
146
147
    public function getPost()
148
    {
149
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
150
            $this->response->add(new XoopsXmlRpcFault(104));
151
        } else {
152
            $xoops_url = \XoopsBaseConfig::get('url');
153
            $xoopsapi = $this->_getXoopsApi($this->params);
154
            $xoopsapi->_setUser($this->user, $this->isadmin);
155
            $ret = $xoopsapi->getPost(false);
156
            if (is_array($ret)) {
157
                $struct = new XoopsXmlRpcStruct();
158
                $content = '';
159 View Code Duplication
                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
                            $struct->add('link', new XoopsXmlRpcString($xoops_url . '/modules/xoopssections/item.php?item=' . $value));
171
                            $struct->add('permaLink', new XoopsXmlRpcString($xoops_url . '/modules/xoopssections/item.php?item=' . $value));
172
                            break;
173
                        case 'title':
174
                            $struct->add('title', new XoopsXmlRpcString($value));
175
                            break;
176
                        default :
177
                            $content .= '<' . $key . '>' . trim($value) . '</' . $key . '>';
178
                            break;
179
                    }
180
                }
181
                $struct->add('description', new XoopsXmlRpcString($content));
182
                $this->response->add($struct);
183
            } else {
184
                $this->response->add(new XoopsXmlRpcFault(106));
185
            }
186
        }
187
    }
188
189
    public function getRecentPosts()
190
    {
191
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
192
            $this->response->add(new XoopsXmlRpcFault(104));
193
        } else {
194
            $xoops_url = \XoopsBaseConfig::get('url');
195
            $xoopsapi = $this->_getXoopsApi($this->params);
196
            $xoopsapi->_setUser($this->user, $this->isadmin);
197
            $ret = $xoopsapi->getRecentPosts(false);
198
            if (is_array($ret)) {
199
                $arr = new XoopsXmlRpcArray();
200
                $count = count($ret);
201
                if ($count == 0) {
202
                    $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
203
                } else {
204
                    for ($i = 0; $i < $count; ++$i) {
205
                        $struct = new XoopsXmlRpcStruct();
206
                        $content = '';
207 View Code Duplication
                        foreach ($ret[$i] as $key => $value) {
208
                            $maptag = $this->_getXoopsTagMap($key);
209
                            switch($maptag) {
210
                            case 'userid':
211
                                $struct->add('userid', new XoopsXmlRpcString($value));
212
                                break;
213
                            case 'dateCreated':
214
                                $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
215
                                break;
216
                            case 'postid':
217
                                $struct->add('postid', new XoopsXmlRpcString($value));
218
                                $struct->add('link', new XoopsXmlRpcString($xoops_url.'/modules/news/article.php?item_id='.$value));
219
                                $struct->add('permaLink', new XoopsXmlRpcString($xoops_url.'/modules/news/article.php?item_id='.$value));
220
                                break;
221
                            case 'title':
222
                                $struct->add('title', new XoopsXmlRpcString($value));
223
                                break;
224
                            default :
225
                                $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
226
                                break;
227
                            }
228
                        }
229
                        $struct->add('description', new XoopsXmlRpcString($content));
230
                        $arr->add($struct);
231
                        unset($struct);
232
                    }
233
                    $this->response->add($arr);
234
                }
235
            } else {
236
                $this->response->add(new XoopsXmlRpcFault(106));
237
            }
238
        }
239
    }
240
241
    public function getCategories()
242
    {
243
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
244
            $this->response->add(new XoopsXmlRpcFault(104));
245
        } else {
246
            $xoops_url = \XoopsBaseConfig::get('url');
247
            $xoopsapi = $this->_getXoopsApi($this->params);
248
            $xoopsapi->_setUser($this->user, $this->isadmin);
249
            $ret = $xoopsapi->getCategories(false);
250
            if (is_array($ret)) {
251
                $arr = new XoopsXmlRpcArray();
252
                foreach ($ret as $id => $detail) {
253
                    $struct = new XoopsXmlRpcStruct();
254
                    $struct->add('description', new XoopsXmlRpcString($detail));
255
                    $struct->add('htmlUrl', new XoopsXmlRpcString($xoops_url.'/modules/news/index.php?storytopic='.$id));
256
                    $struct->add('rssUrl', new XoopsXmlRpcString(''));
257
                    $catstruct = new XoopsXmlRpcStruct();
258
                    $catstruct->add($detail['title'], $struct);
259
                    $arr->add($catstruct);
260
                    unset($struct);
261
                    unset($catstruct);
262
                }
263
                $this->response->add($arr);
264
            } else {
265
                $this->response->add(new XoopsXmlRpcFault(106));
266
            }
267
        }
268
    }
269
}
270