| Total Complexity | 103 |
| Total Lines | 314 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SonglistSongsHandler 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 SonglistSongsHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 150 | class SonglistSongsHandler extends XoopsPersistableObjectHandler |
||
| 151 | { |
||
| 152 | function __construct(&$db) |
||
| 153 | { |
||
| 154 | $module_handler = xoops_gethandler('module'); |
||
| 155 | $config_handler = xoops_gethandler('config'); |
||
| 156 | $GLOBALS['songlistModule'] = $module_handler->getByDirname('songlist'); |
||
| 157 | $GLOBALS['songlistModuleConfig'] = $config_handler->getConfigList($GLOBALS['songlistModule']->getVar('mid')); |
||
| 158 | |||
| 159 | parent::__construct($db, "songlist_songs", 'SonglistSongs', "sid", "title"); |
||
| 160 | } |
||
| 161 | |||
| 162 | function filterFields() { |
||
| 163 | return array('sid', 'cid', 'mp3', 'gid', 'vcid', 'aids', 'abid', 'songid', 'title', 'lyrics', 'hits', 'rank', 'votes', 'tags', 'created', 'updated'); |
||
| 164 | } |
||
| 165 | |||
| 166 | function getFilterCriteria($filter) { |
||
| 190 | } |
||
| 191 | |||
| 192 | function getFilterForm($filter, $field, $sort='created', $op = 'dashboard', $fct='list') { |
||
| 198 | } |
||
| 199 | |||
| 200 | function insert($obj, $force=true, $object = null) { |
||
| 201 | if ($obj->isNew()) { |
||
| 202 | $new = true; |
||
| 203 | $old = $this->create(); |
||
| 204 | $obj->setVar('created', time()); |
||
| 205 | } else { |
||
| 206 | $new = false; |
||
| 207 | $old = $this->get($obj->getVar('sid')); |
||
| 208 | $obj->setVar('updated', time()); |
||
| 209 | } |
||
| 210 | |||
| 211 | $albums_handler = xoops_getmodulehandler('albums', 'songlist'); |
||
| 212 | $artists_handler = xoops_getmodulehandler('artists', 'songlist'); |
||
| 213 | $genre_handler = xoops_getmodulehandler('genre', 'songlist'); |
||
| 214 | $voice_handler = xoops_getmodulehandler('voice', 'songlist'); |
||
| 215 | $category_handler = xoops_getmodulehandler('category', 'songlist'); |
||
| 216 | |||
| 217 | if ($obj->vars['gid']['changed']==true) { |
||
| 218 | if ($new==true||($obj->vars['gid']['value']!=0)) { |
||
| 219 | $genre = $genre_handler->get($obj->vars['gid']['value']); |
||
| 220 | if (is_object($genre)) { |
||
| 221 | $genre->setVar('songs', $genre->getVar('songs')+1); |
||
| 222 | $genre_handler->insert($genre, true, $obj); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | if (!$old->isNew()&&$old->getVar('gid')>0) { |
||
| 226 | $genre = $genre_handler->get($old->vars['gid']['value']); |
||
| 227 | if (is_object($genre)) { |
||
| 228 | $genre->setVar('songs', $genre->getVar('songs')-1); |
||
| 229 | $genre_handler->insert($genre, true, null); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($obj->vars['vcid']['changed']==true) { |
||
| 235 | if ($new==true||($obj->vars['vcid']['value']!=0)) { |
||
| 236 | $voice = $voice_handler->get($obj->vars['vcid']['value']); |
||
| 237 | if (is_object($voice)) { |
||
| 238 | $voice->setVar('songs', $voice->getVar('songs')+1); |
||
| 239 | $voice_handler->insert($voice, true, $obj); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | if (!$old->isNew()&&$old->getVar('vcid')>0) { |
||
| 243 | $voice = $voice_handler->get($old->vars['vcid']['value']); |
||
| 244 | if (is_object($voice)) { |
||
| 245 | $voice->setVar('songs', $voice->getVar('songs')-1); |
||
| 246 | $voice_handler->insert($voice, true, null); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($obj->vars['cid']['changed']==true) { |
||
| 252 | if ($new==true||($obj->vars['cid']['value']!=0)) { |
||
| 253 | $category = $category_handler->get($obj->vars['cid']['value']); |
||
| 254 | if (is_object($category)) { |
||
| 255 | $category->setVar('songs', $category->getVar('songs')+1); |
||
| 256 | $category_handler->insert($category, true, $obj); |
||
| 257 | foreach($obj->getVar('aids') as $aid) { |
||
| 258 | $artists = $artists_handler->get($aid); |
||
| 259 | $cids = $artists->getVar('cids'); |
||
| 260 | $cids[$obj->getVar('cid')] = $obj->getVar('cid'); |
||
| 261 | if (is_object($artists)) { |
||
| 262 | $artists->setVar('cids', $cids); |
||
| 263 | $artists_handler->insert($artists, true, null); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | if (!$old->isNew()&&$old->getVar('cid')>0) { |
||
| 269 | $category = $category_handler->get($old->vars['cid']['value']); |
||
| 270 | if (is_object($category)) { |
||
| 271 | $category->setVar('songs', $category->getVar('songs')-1); |
||
| 272 | $category_handler->insert($category, true, null); |
||
| 273 | foreach($obj->getVar('aids') as $aid) { |
||
| 274 | $artists = $artists_handler->get($aid); |
||
| 275 | $cids=array(); |
||
| 276 | foreach($artists->getVar('cids') as $cid) { |
||
| 277 | if($cid!=$old->getVar('cid')||$cid==$obj->getVar('cid')) |
||
| 278 | $cids[$cid] = $cid; |
||
| 279 | } |
||
| 280 | if (is_object($artists)) { |
||
| 281 | $artists->setVar('cids', $cids); |
||
| 282 | $artists_handler->insert($artists, true, null); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | if ($obj->vars['aids']['changed']==true&&count($obj->vars['aids']['value'])!=0) { |
||
| 290 | if ($new==true||count($obj->vars['aids']['value'])!=0) { |
||
| 291 | foreach($obj->vars['aids']['value'] as $aid) { |
||
| 292 | if (!in_array($aid, $old->vars['aids']['value'])) { |
||
| 293 | $artists = $artists_handler->get($aid); |
||
| 294 | if (is_object($artists)) { |
||
| 295 | $artists->setVar('songs', $artists->getVar('songs')+1); |
||
| 296 | $artists_handler->insert($artists, true, $obj); |
||
| 297 | if ($new==true||($obj->vars['vcid']['value']!=0)) { |
||
| 298 | $voice = $voice_handler->get($obj->vars['vcid']['value']); |
||
| 299 | if (is_object($voice)) { |
||
| 300 | $voice->setVar('artists', $voice->getVar('artists')+1); |
||
| 301 | $voice_handler->insert($voice, true, $obj); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | if (!$old->isNew()&&count($old->getVar('aids'))==0) { |
||
| 309 | foreach($old->getVar('aids') as $aid) { |
||
| 310 | if (!in_array($aid, $obj->vars['aids']['value'])) { |
||
| 311 | $artists = $artists_handler->get($aid); |
||
| 312 | if (is_object($artists)) { |
||
| 313 | $artists->setVar('songs', $artists->getVar('songs')-1); |
||
| 314 | $artists_handler->insert($artists, true, null); |
||
| 315 | if (!$old->isNew()&&$old->getVar('vcid')>0) { |
||
| 316 | $voice = $voice_handler->get($old->vars['vcid']['value']); |
||
| 317 | if (is_object($voice)) { |
||
| 318 | $voice->setVar('artists', $voice->getVar('artists')-1); |
||
| 319 | $voice_handler->insert($voice, true, null); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | if ($obj->vars['abid']['changed']==true) { |
||
| 329 | if ($new==true||($obj->vars['abid']['value']!=0)) { |
||
| 330 | $album = $albums_handler->get($obj->vars['abid']['value']); |
||
| 331 | if (is_object($album)) { |
||
| 332 | $album->setVar('songs', $album->getVar('songs')+1); |
||
| 333 | $albums_handler->insert($album, true, $obj); |
||
| 334 | if ($new==true||($obj->vars['vcid']['value']!=0)) { |
||
| 335 | $voice = $voice_handler->get($obj->vars['vcid']['value']); |
||
| 336 | if (is_object($voice)) { |
||
| 337 | $voice->setVar('albums', $voice->getVar('albums')+1); |
||
| 338 | $voice_handler->insert($voice, true, $obj); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |
||
| 343 | if (!$old->isNew()&&$old->getVar('abid')>0) { |
||
| 344 | $album = $albums_handler->get($obj->vars['abid']['value']); |
||
| 345 | if (is_object($album)) { |
||
| 346 | $album->setVar('songs', $album->getVar('songs')-1); |
||
| 347 | $albums_handler->insert($album, true, null); |
||
| 348 | if (!$old->isNew()&&$old->getVar('vcid')>0) { |
||
| 349 | $voice = $voice_handler->get($old->vars['vcid']['value']); |
||
| 350 | if (is_object($voice)) { |
||
| 351 | $voice->setVar('albums', $voice->getVar('albums')-1); |
||
| 352 | $voice_handler->insert($voice, true, null); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | if (strlen($obj->getVar('title'))==0) |
||
| 360 | return false; |
||
| 361 | |||
| 362 | $sid = parent::insert($obj, $force); |
||
| 363 | if ($obj->vars['abid']['value']>0) { |
||
| 364 | $album = $albums_handler->get($obj->vars['abid']['value']); |
||
| 365 | $arry = $album->getVar('sids'); |
||
| 366 | $arry[$sid] = $sid; |
||
| 367 | if (is_object($album)) { |
||
| 368 | $album->setVar('sids', $arry); |
||
| 369 | $albums_handler->insert($album); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | if (count($obj->getVar('aids'))>0) { |
||
| 373 | foreach($obj->getVar('aids') as $aid) { |
||
| 374 | $artist = $artists_handler->get($aid); |
||
| 375 | $arry = $artist->getVar('sids'); |
||
| 376 | $arry[$sid] = $sid; |
||
| 377 | if (is_object($artists)) { |
||
| 378 | $artist->setVar('sids', $arry); |
||
| 379 | $artists_handler->insert($artist); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 | return $sid; |
||
| 384 | } |
||
| 385 | |||
| 386 | var $_objects = array('object'=>array(), 'array'=>array()); |
||
| 387 | |||
| 388 | function get($id, $fields = '*') { |
||
| 389 | if (!isset($this->_objects['object'][$id])) { |
||
| 390 | $this->_objects['object'][$id] = parent::get($id, $fields); |
||
| 391 | if (!isset($GLOBALS['songlistAdmin'])&&is_object($this->_objects['object'][$id])) { |
||
| 392 | $sql = 'UPDATE `'.$this->table.'` set hits=hits+1 where `'.$this->keyName.'` = '.$this->_objects['object'][$id]->getVar($this->keyName); |
||
| 393 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | return $this->_objects['object'][$id]; |
||
| 397 | } |
||
| 398 | |||
| 399 | function getObjects($criteria = NULL, $id_as_key = false, $as_object = true) { |
||
| 422 | } |
||
| 423 | |||
| 424 | function getURL() { |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | |||
| 444 | function getSearchURL() { |
||
| 445 | global $file, $op, $fct, $id, $value, $gid, $vcid, $cid, $start, $limit; |
||
| 446 | if ($GLOBALS['songlistModuleConfig']['htaccess']) { |
||
| 447 | return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/'.$file.'/'.$start.'-'.$op.'-'.$fct.'-'.$gid.'-'.(isset($_GET['cid'])?($_GET['cid']):$cid).'-'.$vcid.'/'.urlencode($value).$GLOBALS['songlistModuleConfig']['endofurl']; |
||
| 448 | } else { |
||
| 449 | return XOOPS_URL.'/modules/songlist/'.$file.'.php?op='.$op.'&fct='.$fct.'&value='.urlencode($value).'&cid='.$cid.'&gid='.$gid.'&vcid='.$vcid.'&start='.$start; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | function getTop($limit=1) { |
||
| 464 | } |
||
| 465 | } |
||
| 466 | ?> |
||
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.