| Total Complexity | 96 |
| Total Lines | 349 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like XoopsModelWrite 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 XoopsModelWrite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class XoopsModelWrite extends XoopsModelAbstract |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Clean values of all variables of the object for storage. |
||
| 35 | * also add slashes and quote string wherever needed |
||
| 36 | * |
||
| 37 | * CleanVars only contains changed and cleaned variables |
||
| 38 | * Reference is used for PHP4 compliance |
||
| 39 | * |
||
| 40 | * @param $object |
||
| 41 | * |
||
| 42 | * @return bool true if successful |
||
| 43 | * @access public |
||
| 44 | */ |
||
| 45 | public function cleanVars($object) |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * insert an object into the database |
||
| 236 | * |
||
| 237 | * @param object $object {@link XoopsObject} reference to object |
||
| 238 | * @param bool $force flag to force the query execution despite security settings |
||
| 239 | * @return mixed object ID |
||
| 240 | */ |
||
| 241 | public function insert($object, $force = true) |
||
| 242 | { |
||
| 243 | if (!$object->isDirty()) { |
||
| 244 | trigger_error("Data entry is not inserted - the object '" . get_class($object) . "' is not dirty", E_USER_NOTICE); |
||
| 245 | |||
| 246 | return $object->getVar($this->handler->keyName); |
||
| 247 | } |
||
| 248 | if (!$this->cleanVars($object)) { |
||
| 249 | trigger_error("Insert failed in method 'cleanVars' of object '" . get_class($object) . "'", E_USER_WARNING); |
||
| 250 | |||
| 251 | return $object->getVar($this->handler->keyName); |
||
| 252 | } |
||
| 253 | $queryFunc = empty($force) ? 'query' : 'exec'; |
||
|
|
|||
| 254 | |||
| 255 | if ($object->isNew()) { |
||
| 256 | $sql = 'INSERT INTO `' . $this->handler->table . '`'; |
||
| 257 | $queryFunc = 'exec'; |
||
| 258 | if (!empty($object->cleanVars)) { |
||
| 259 | $keys = array_keys($object->cleanVars); |
||
| 260 | $vals = array_values($object->cleanVars); |
||
| 261 | $sql .= ' (`' . implode('`, `', $keys) . '`) VALUES (' . implode(',', $vals) . ')'; |
||
| 262 | } else { |
||
| 263 | trigger_error("Data entry is not inserted - no variable is changed in object of '" . get_class($object) . "'", E_USER_NOTICE); |
||
| 264 | |||
| 265 | return $object->getVar($this->handler->keyName); |
||
| 266 | } |
||
| 267 | if (!$result = $this->handler->db->{$queryFunc}($sql)) { |
||
| 268 | return false; |
||
| 269 | } |
||
| 270 | if (!$object->getVar($this->handler->keyName) && $object_id = $this->handler->db->getInsertId()) { |
||
| 271 | $object->assignVar($this->handler->keyName, $object_id); |
||
| 272 | } |
||
| 273 | } elseif (!empty($object->cleanVars)) { |
||
| 274 | $keys = []; |
||
| 275 | foreach ($object->cleanVars as $k => $v) { |
||
| 276 | $keys[] = " `{$k}` = {$v}"; |
||
| 277 | } |
||
| 278 | $sql = 'UPDATE `' . $this->handler->table . '` SET ' . implode(',', $keys) . ' WHERE `' . $this->handler->keyName . '` = ' . $this->handler->db->quote($object->getVar($this->handler->keyName)); |
||
| 279 | $queryFunc = 'exec'; |
||
| 280 | if (!$result = $this->handler->db->{$queryFunc}($sql)) { |
||
| 281 | return false; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return $object->getVar($this->handler->keyName); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * delete an object from the database |
||
| 290 | * |
||
| 291 | * @param object $object {@link XoopsObject} reference to the object to delete |
||
| 292 | * @param bool $force |
||
| 293 | * @return bool FALSE if failed. |
||
| 294 | */ |
||
| 295 | public function delete($object, $force = false) |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * delete all objects matching the conditions |
||
| 317 | * |
||
| 318 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions to meet |
||
| 319 | * @param bool $force force to delete |
||
| 320 | * @param bool $asObject delete in object way: instantiate all objects and delete one by one |
||
| 321 | * @return bool|int |
||
| 322 | */ |
||
| 323 | public function deleteAll(?CriteriaElement $criteria = null, $force = true, $asObject = false) |
||
| 324 | { |
||
| 325 | if ($asObject) { |
||
| 326 | $objects = $this->handler->getAll($criteria); |
||
| 327 | $num = 0; |
||
| 328 | foreach (array_keys($objects) as $key) { |
||
| 329 | $num += $this->delete($objects[$key], $force) ? 1 : 0; |
||
| 330 | } |
||
| 331 | unset($objects); |
||
| 332 | |||
| 333 | return $num; |
||
| 334 | } |
||
| 335 | // $queryFunc = empty($force) ? 'query' : 'exec'; |
||
| 336 | $queryFunc = 'exec'; |
||
| 337 | $sql = 'DELETE FROM ' . $this->handler->table; |
||
| 338 | if (!empty($criteria)) { |
||
| 339 | if (is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 340 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 341 | } else { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | } |
||
| 345 | if (!$this->handler->db->{$queryFunc}($sql)) { |
||
| 346 | return false; |
||
| 347 | } |
||
| 348 | |||
| 349 | return $this->handler->db->getAffectedRows(); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Change a field for objects with a certain criteria |
||
| 354 | * |
||
| 355 | * @param string $fieldname Name of the field |
||
| 356 | * @param mixed $fieldvalue Value to write |
||
| 357 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} |
||
| 358 | * @param bool $force force to query |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | public function updateAll($fieldname, $fieldvalue, ?CriteriaElement $criteria = null, $force = false) |
||
| 380 | } |
||
| 381 | } |
||
| 382 |