| Total Complexity | 60 |
| Total Lines | 192 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SonglistAlbumsHandler 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 SonglistAlbumsHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 120 | class SonglistAlbumsHandler extends XoopsPersistableObjectHandler |
||
| 121 | { |
||
| 122 | function __construct(&$db) |
||
| 123 | { |
||
| 124 | parent::__construct($db, "songlist_albums", 'SonglistAlbums', "abid", "title"); |
||
| 125 | } |
||
| 126 | |||
| 127 | function filterFields() { |
||
| 128 | return array('abid', 'cid', 'aids', 'sids', 'title', 'image', 'path', 'artists', 'songs', 'hits', 'rank', 'votes', 'created', 'updated'); |
||
| 129 | } |
||
| 130 | |||
| 131 | function getFilterCriteria($filter) { |
||
| 132 | $parts = explode('|', $filter); |
||
| 133 | $criteria = new CriteriaCompo(); |
||
| 134 | foreach($parts as $part) { |
||
| 135 | $var = explode(',', $part); |
||
| 136 | if (!empty($var[1])&&!is_numeric($var[0])) { |
||
| 137 | $object = $this->create(); |
||
| 138 | if ( $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_TXTBOX || |
||
| 139 | $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_TXTAREA) { |
||
| 140 | $criteria->add(new Criteria('`'.$var[0].'`', '%'.$var[1].'%', (isset($var[2])?$var[2]:'LIKE'))); |
||
| 141 | } elseif ( $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_INT || |
||
| 142 | $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_DECIMAL || |
||
| 143 | $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_FLOAT ) { |
||
| 144 | $criteria->add(new Criteria('`'.$var[0].'`', $var[1], (isset($var[2])?$var[2]:'='))); |
||
| 145 | } elseif ( $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_ENUM ) { |
||
| 146 | $criteria->add(new Criteria('`'.$var[0].'`', $var[1], (isset($var[2])?$var[2]:'='))); |
||
| 147 | } elseif ( $object->vars[$var[0]]['data_type']==XOBJ_DTYPE_ARRAY ) { |
||
| 148 | $criteria->add(new Criteria('`'.$var[0].'`', '%"'.$var[1].'";%', (isset($var[2])?$var[2]:'LIKE'))); |
||
| 149 | } |
||
| 150 | } elseif (!empty($var[1])&&is_numeric($var[0])) { |
||
| 151 | $criteria->add(new Criteria($var[0], $var[1])); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | return $criteria; |
||
| 155 | } |
||
| 156 | |||
| 157 | function getFilterForm($filter, $field, $sort='created', $op = 'dashboard', $fct='list') { |
||
| 163 | } |
||
| 164 | |||
| 165 | function insert($obj, $force=true, $object = null) { |
||
| 166 | if ($obj->isNew()) { |
||
| 167 | $new = true; |
||
| 168 | $old = $this->create(); |
||
| 169 | $obj->setVar('created', time()); |
||
| 170 | } else { |
||
| 171 | $new = false; |
||
| 172 | $old = $this->get($obj->getVar('abid')); |
||
| 173 | $obj->setVar('updated', time()); |
||
| 174 | } |
||
| 175 | |||
| 176 | $artists_handler = xoops_getmodulehandler('artists', 'songlist'); |
||
| 177 | $genre_handler = xoops_getmodulehandler('genre', 'songlist'); |
||
| 178 | $voice_handler = xoops_getmodulehandler('voice', 'songlist'); |
||
| 179 | $category_handler = xoops_getmodulehandler('category', 'songlist'); |
||
| 180 | |||
| 181 | if (is_a($object, 'SonglistSongs')) { |
||
| 182 | if ($obj->vars['cid']['changed']==true) { |
||
| 183 | if ($obj->vars['cid']['value'] != $old->vars['cid']['value']) { |
||
| 184 | $category = $category_handler->get($obj->vars['cid']['value']); |
||
| 185 | if (is_object($category)) { |
||
| 186 | $category->setVar('albums', $category->getVar('albums')+1); |
||
| 187 | $category_handler->insert($category, true, $obj); |
||
| 188 | if (!$old->isNew()&&$old->vars['cid']['value']>0) { |
||
| 189 | $category = $category_handler->get($old->vars['cid']['value']); |
||
| 190 | if (is_object($category)) { |
||
| 191 | $category->setVar('albums', $category->getVar('albums')-1); |
||
| 192 | $category_handler->insert($category, true, $obj); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | if (count($obj->vars['aids']['value'])!=0&&$obj->vars['aids']['changed']==true) { |
||
| 200 | foreach($obj->vars['aids']['value'] as $aid) { |
||
| 201 | if (!is_array($aid, $old->getVar('aids'))&&$aid!=0) { |
||
| 202 | $artists = $artists_handler->get($aid); |
||
| 203 | if (is_object($artists)) { |
||
| 204 | $artists->setVar('albums', $artists->getVar('albums')+1); |
||
| 205 | $artists_handler->insert($artists, true, $obj); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | if (!$old->isNew()) { |
||
| 210 | foreach($old->getVar('aids') as $aid) { |
||
| 211 | if (!is_array($aid, $obj->vars['aids']['value'])&&$aid!=0) { |
||
| 212 | $artists = $artists_handler->get($aid); |
||
| 213 | if (is_object($artists)) { |
||
| 214 | $artists->setVar('albums', $artists->getVar('albums')-1); |
||
| 215 | $artists_handler->insert($artists, true, $obj); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | if ($object->vars['gid']['value']!=0&&$object->vars['gid']['changed']==true) { |
||
| 223 | $genre = $genre_handler->get($object->vars['gid']['value']); |
||
| 224 | if (is_object($genre)) { |
||
| 225 | $genre->setVar('albums', $genre->getVar('albums')+1); |
||
| 226 | $genre_handler->insert($genre, true, $obj); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | if ($object->vars['vid']['value']!=0&&$object->vars['vid']['changed']==true) { |
||
| 230 | $voice = $voice_handler->get($object->vars['vid']['value']); |
||
| 231 | if (is_object($voice)) { |
||
| 232 | $voice->setVar('albums', $voice->getVar('albums')+1); |
||
| 233 | $voice_handler->insert($voice, true, $obj); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | } |
||
| 238 | if (strlen($obj->getVar('title'))==0) |
||
| 239 | return false; |
||
| 240 | |||
| 241 | return parent::insert($obj, $force); |
||
| 242 | } |
||
| 243 | |||
| 244 | var $_objects = array('object'=>array(), 'array'=>array()); |
||
| 245 | |||
| 246 | function get($id, $fields = '*') { |
||
| 247 | if (!isset($this->_objects['object'][$id])) { |
||
| 248 | $this->_objects['object'][$id] = parent::get($id, $fields); |
||
| 249 | if (!isset($GLOBALS['songlistAdmin'])&&is_object($this->_objects['object'][$id])) { |
||
| 250 | $sql = 'UPDATE `'.$this->table.'` set hits=hits+1 where `'.$this->keyName.'` = '.$this->_objects['object'][$id]->getVar($this->keyName); |
||
| 251 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | return $this->_objects['object'][$id]; |
||
| 255 | } |
||
| 256 | |||
| 257 | function getObjects($criteria = NULL, $id_as_key = false, $as_object = true) { |
||
| 280 | } |
||
| 281 | |||
| 282 | function getURL() { |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | function getTop($limit=1) { |
||
| 312 | } |
||
| 313 | } |
||
| 314 | ?> |
||
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.