| Total Complexity | 72 |
| Total Lines | 433 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EntrydataHandler 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 EntrydataHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class EntrydataHandler extends \XoopsModules\Soapbox\EntrygetHandler |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * constructor |
||
| 44 | * @param \XoopsDatabase|null $db |
||
| 45 | */ |
||
| 46 | public function __construct(\XoopsDatabase $db = null) |
||
| 47 | { |
||
| 48 | parent::__construct($db); |
||
| 49 | global $moduleDirName; |
||
| 50 | $this->sbArticleHandler = new ArticlesHandler($db); |
||
| 51 | $this->sbColumnHandler = new ColumnsHandler($db); |
||
| 52 | $this->sbVoteHandler = new VotedataHandler($db); |
||
| 53 | $_mymoduleHandler = xoops_getHandler('module'); |
||
| 54 | $_mymodule = $_mymoduleHandler->getByDirname('soapbox'); |
||
| 55 | if (!is_object($_mymodule)) { |
||
| 56 | exit('not found dirname'); |
||
|
|
|||
| 57 | } |
||
| 58 | $this->moduleDirName = $moduleDirName; |
||
| 59 | $this->moduleId = $_mymodule->getVar('mid'); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * create a new Article |
||
| 64 | * |
||
| 65 | * @param bool $isNew |
||
| 66 | * @return object Articles reference to the new Article |
||
| 67 | */ |
||
| 68 | public function createArticle($isNew = true) |
||
| 69 | { |
||
| 70 | $ret = $this->sbArticleHandler->create($isNew); |
||
| 71 | |||
| 72 | return $ret; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * create a new Column |
||
| 77 | * |
||
| 78 | * @param bool $isNew |
||
| 79 | * @return object Columns reference to the new Column |
||
| 80 | */ |
||
| 81 | public function createColumn($isNew = true) |
||
| 82 | { |
||
| 83 | $ret = $this->sbColumnHandler->create($isNew); |
||
| 84 | |||
| 85 | return $ret; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * create a new Votedata |
||
| 90 | * |
||
| 91 | * @param bool $isNew |
||
| 92 | * @return object Votedata reference to the new Votedata |
||
| 93 | */ |
||
| 94 | public function createVotedata($isNew = true) |
||
| 95 | { |
||
| 96 | $ret = $this->sbVoteHandler->create($isNew); |
||
| 97 | |||
| 98 | return $ret; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * insert a new entry in the database |
||
| 103 | * |
||
| 104 | * @param Articles $sbarticle reference to the {@link Articles} object |
||
| 105 | * @param bool $force |
||
| 106 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 107 | */ |
||
| 108 | public function insertArticle(Articles $sbarticle, $force = false) |
||
| 109 | { |
||
| 110 | if (!$this->sbArticleHandler->insert($sbarticle, $force)) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | // re count ---------------------------------- |
||
| 114 | $this->updateTotalByColumnID($sbarticle->getVar('columnID')); |
||
| 115 | |||
| 116 | // re count ---------------------------------- |
||
| 117 | return true; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * insert a new Column in the database |
||
| 122 | * |
||
| 123 | * @param object $sbcolumn reference to the {@link Columns} object |
||
| 124 | * @param bool $force |
||
| 125 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 126 | */ |
||
| 127 | public function insertColumn($sbcolumn, $force = false) |
||
| 128 | { |
||
| 129 | if (!$this->sbColumnHandler->insert($sbcolumn, $force)) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * insert a new Votedata in the database |
||
| 138 | * |
||
| 139 | * @param object $sbvotedata reference to the {@link Votedata} object |
||
| 140 | * @param bool $force |
||
| 141 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 142 | */ |
||
| 143 | public function insertVotedata($sbvotedata, $force = false) |
||
| 144 | { |
||
| 145 | if (!$this->sbVoteHandler->insert($sbvotedata, $force)) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | return true; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * delete a Article from the database |
||
| 154 | * |
||
| 155 | * @param object $sbarticle reference to the Article to delete |
||
| 156 | * @param bool $force |
||
| 157 | * @param bool $re_count |
||
| 158 | * @return bool FALSE if failed. |
||
| 159 | */ |
||
| 160 | public function deleteArticle($sbarticle, $force = false, $re_count = true) |
||
| 161 | { |
||
| 162 | // delete Article |
||
| 163 | if (!$this->sbArticleHandler->delete($sbarticle, $force)) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | // delete Votedata |
||
| 167 | $criteria = new \CriteriaCompo(); |
||
| 168 | $criteria->add(new \Criteria('lid', $sbarticle->getVar('articleID'))); |
||
| 169 | $this->sbVoteHandler->deleteEntrys($criteria, $force); |
||
| 170 | unset($criteria); |
||
| 171 | // delete comments |
||
| 172 | xoops_comment_delete($this->moduleId, $sbarticle->getVar('articleID')); |
||
| 173 | // re count ---------------------------------- |
||
| 174 | if ($re_count) { |
||
| 175 | $this->updateTotalByColumnID($sbarticle->getVar('columnID')); |
||
| 176 | } |
||
| 177 | |||
| 178 | // re count ---------------------------------- |
||
| 179 | return true; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * delete a Column from the database |
||
| 184 | * |
||
| 185 | * @param object $sbcolumn reference to the Column to delete |
||
| 186 | * @param bool $force |
||
| 187 | * @return bool FALSE if failed. |
||
| 188 | */ |
||
| 189 | public function deleteColumn($sbcolumn, $force = false) |
||
| 190 | { |
||
| 191 | // delete Column |
||
| 192 | if (!$this->sbColumnHandler->delete($sbcolumn, $force)) { |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | $criteria = new \CriteriaCompo(); |
||
| 196 | $criteria->add(new \Criteria('columnID', $sbcolumn->getVar('columnID'))); |
||
| 197 | $this->deleteArticlesEntrys($criteria, $force, false); |
||
| 198 | unset($criteria); |
||
| 199 | |||
| 200 | return true; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * delete a Votedata from the database |
||
| 205 | * |
||
| 206 | * @param Votedata $sbvotedata reference to the Votedata to delete |
||
| 207 | * @param bool $force |
||
| 208 | * @return bool FALSE if failed. |
||
| 209 | */ |
||
| 210 | public function deleteVotedata($sbvotedata, $force = false) |
||
| 211 | { |
||
| 212 | // delete Votedata |
||
| 213 | if (!$this->sbVoteHandler->delete($sbvotedata, $force)) { |
||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | return true; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * delete entrys from the database |
||
| 222 | * |
||
| 223 | * @param \CriteriaElement $criteria {@link CriteriaElement} conditions to be match |
||
| 224 | * @param bool $force |
||
| 225 | * @param bool $re_count |
||
| 226 | * @return bool FALSE if failed. |
||
| 227 | */ |
||
| 228 | public function deleteArticlesEntrys(\CriteriaElement $criteria = null, $force = false, $re_count = false) |
||
| 229 | { |
||
| 230 | $_sbarticles_arr = $this->getArticles($criteria); |
||
| 231 | if (empty($_sbarticles_arr) || 0 === count($_sbarticles_arr)) { |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | foreach ($_sbarticles_arr as $sbarticle) { |
||
| 235 | $this->deleteArticle($sbarticle, $force, false); |
||
| 236 | } |
||
| 237 | |||
| 238 | return true; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $sbarticle |
||
| 243 | * @param bool $force |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function updateRating(&$sbarticle, $force = false) // updates rating data in itemtable for a given item |
||
| 247 | { |
||
| 248 | if (mb_strtolower(get_class($sbarticle)) !== mb_strtolower(Articles::class)) { |
||
| 249 | return false; |
||
| 250 | } |
||
| 251 | $totalrating = 0.00; |
||
| 252 | $votesDB = 0; |
||
| 253 | $finalrating = 0; |
||
| 254 | |||
| 255 | $sbvotedata_arr = $this->getVotedatasByArticleID($sbarticle->getVar('articleID'), true, 0, 0); |
||
| 256 | $votesDB = count($sbvotedata_arr); |
||
| 257 | if (empty($sbvotedata_arr) || 0 === $votesDB) { |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | foreach ($sbvotedata_arr as $sbvotedata) { |
||
| 261 | if (is_object($sbvotedata)) { |
||
| 262 | $totalrating += $sbvotedata->getVar('rating'); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | if (0 !== $totalrating && 0 !== $votesDB) { |
||
| 266 | $finalrating = ($totalrating / $votesDB) + 0.00005; |
||
| 267 | $finalrating = number_format($finalrating, 4); |
||
| 268 | } |
||
| 269 | |||
| 270 | $sbarticle->setVar('rating', $finalrating); |
||
| 271 | $sbarticle->setVar('votes', $votesDB); |
||
| 272 | if (!$this->insertArticle($sbarticle, $force)) { |
||
| 273 | return false; |
||
| 274 | } |
||
| 275 | |||
| 276 | return true; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $columnID |
||
| 281 | * @param bool $force |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function updateTotalByColumnID($columnID, $force = false) |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * updates a single field in a Article record |
||
| 305 | * |
||
| 306 | * @param Articles $sbarticle reference to the {@link Articles} object |
||
| 307 | * @param string $fieldName name of the field to update |
||
| 308 | * @param string $fieldValue updated value for the field |
||
| 309 | * @return bool TRUE if success or unchanged, FALSE on failure |
||
| 310 | */ |
||
| 311 | public function updateArticleByField($sbarticle, $fieldName, $fieldValue) |
||
| 312 | { |
||
| 313 | return $this->sbArticleHandler->updateByField($sbarticle, $fieldName, $fieldValue); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * updates a single field in a Column record |
||
| 318 | * |
||
| 319 | * @param Columns $sbcolumns reference to the {@link Columns} object |
||
| 320 | * @param string $fieldName name of the field to update |
||
| 321 | * @param string $fieldValue updated value for the field |
||
| 322 | * @return bool TRUE if success or unchanged, FALSE on failure |
||
| 323 | */ |
||
| 324 | public function updateColumnByField($sbcolumns, $fieldName, $fieldValue) |
||
| 325 | { |
||
| 326 | return $this->sbColumnHandler->updateByField($sbcolumns, $fieldName, $fieldValue); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * updates a single field in a Votedata record |
||
| 331 | * |
||
| 332 | * @param $sbvotedata |
||
| 333 | * @param string $fieldName name of the field to update |
||
| 334 | * @param string $fieldValue updated value for the field |
||
| 335 | * @return bool TRUE if success or unchanged, FALSE on failure |
||
| 336 | * @internal param object $user reference to the {@link Columns} object object |
||
| 337 | */ |
||
| 338 | public function updateVotedataByField(&$sbvotedata, $fieldName, $fieldValue) |
||
| 339 | { |
||
| 340 | return $this->sbVoteHandler->updateByField($sbvotedata, $fieldName, $fieldValue); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * weight update |
||
| 345 | * |
||
| 346 | * @param array $weight to match |
||
| 347 | * @return bool FALSE if failed. |
||
| 348 | */ |
||
| 349 | public function reorderColumnsUpdate($weight) |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * weight update |
||
| 375 | * |
||
| 376 | * @param array $weight to match |
||
| 377 | * @return bool FALSE if failed. |
||
| 378 | */ |
||
| 379 | public function reorderArticlesUpdate($weight) |
||
| 380 | { |
||
| 381 | if (!isset($weight) || empty($weight) || !is_array($weight)) { |
||
| 382 | return false; |
||
| 383 | } |
||
| 384 | foreach ($weight as $articleID => $order) { |
||
| 385 | $weight[$articleID] = (int)$order; |
||
| 386 | } |
||
| 387 | array_unique($weight); |
||
| 388 | foreach ($weight as $articleID => $order) { |
||
| 389 | if (isset($articleID) && is_numeric($articleID) && isset($order)) { |
||
| 390 | $sbarticle = $this->getArticle($articleID); |
||
| 391 | if (is_object($sbarticle)) { |
||
| 392 | if (!is_numeric($order) || empty($order)) { |
||
| 393 | $order = 0; |
||
| 394 | } |
||
| 395 | $this->updateArticleByField($sbarticle, 'weight', $order); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | return true; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * newarticleTriggerEvent a category of this columnID from the database |
||
| 405 | * |
||
| 406 | * @param object $sbcolumn reference to the category to delete |
||
| 407 | * @param string $events |
||
| 408 | * @return bool FALSE if failed. |
||
| 409 | * @internal param bool $force |
||
| 410 | */ |
||
| 411 | public function newColumnTriggerEvent($sbcolumn, $events = 'new_column') |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * new articleTriggerEvent counter a new entry in the database |
||
| 432 | * |
||
| 433 | * @param $sbarticle |
||
| 434 | * @param string $events |
||
| 435 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 436 | * @internal param object $criteria {@link CriteriaElement} conditions to be match conditions to be match |
||
| 437 | */ |
||
| 438 | public function newArticleTriggerEvent(&$sbarticle, $events = 'new_article') |
||
| 473 | } |
||
| 474 | } |
||
| 475 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.