| Total Complexity | 85 |
| Total Lines | 532 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ModifierTrait 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 ModifierTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | trait ModifierTrait |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * ModifierTrait::insert |
||
| 27 | * |
||
| 28 | * Method to input data as well as equipping the data in accordance with the fields |
||
| 29 | * in the destination database collection. |
||
| 30 | * |
||
| 31 | * |
||
| 32 | * @param array $sets Array of Input Data |
||
| 33 | * @param string $collection Table Name |
||
| 34 | * |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | public function insert(array $sets, $collection = null) |
||
| 38 | { |
||
| 39 | $collection = isset($collection) ? $collection : $this->collection; |
||
| 40 | |||
| 41 | if (method_exists($this, 'insertRecordSets')) { |
||
| 42 | $this->insertRecordSets($sets); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (method_exists($this, 'beforeInsert')) { |
||
| 46 | $this->beforeInsert($sets); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($this->qb->collection($collection)->insert($sets)) { |
||
| 50 | if (method_exists($this, 'afterInsert')) { |
||
| 51 | return $this->afterInsert(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return true; |
||
| 55 | } |
||
| 56 | |||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | // ------------------------------------------------------------------------ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * ModifierTrait::update |
||
| 64 | * |
||
| 65 | * Method to update data as well as equipping the data in accordance with the fields |
||
| 66 | * in the destination database collection. |
||
| 67 | * |
||
| 68 | * @param string $collection Table Name |
||
| 69 | * @param array $sets Array of Update Data |
||
| 70 | * |
||
| 71 | * @return bool|\O2System\Database\DataObjects\Result |
||
| 72 | */ |
||
| 73 | public function update(array $sets, $where = [], $collection = null) |
||
| 74 | { |
||
| 75 | $collection = isset($collection) ? $collection : $this->collection; |
||
| 76 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 77 | |||
| 78 | if (empty($where)) { |
||
| 79 | if (empty($this->primaryKeys)) { |
||
| 80 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 81 | } else { |
||
| 82 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 83 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | // Reset Primary Keys |
||
| 89 | $this->primaryKey = 'id'; |
||
|
|
|||
| 90 | $this->primaryKeys = []; |
||
| 91 | |||
| 92 | if (method_exists($this, 'updateRecordSets')) { |
||
| 93 | $this->updateRecordSets($sets); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (method_exists($this, 'beforeUpdate')) { |
||
| 97 | $sets = $this->beforeUpdate($sets); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($this->qb->collection($collection)->update($sets, $where)) { |
||
| 101 | |||
| 102 | if (method_exists($this, 'afterUpdate')) { |
||
| 103 | return $this->afterUpdate(); |
||
| 104 | } |
||
| 105 | |||
| 106 | return true; |
||
| 107 | } |
||
| 108 | |||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | // ------------------------------------------------------------------------ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param array $sets |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function insertMany(array $sets) |
||
| 119 | { |
||
| 120 | $collection = isset($collection) ? $collection : $this->collection; |
||
| 121 | |||
| 122 | if (method_exists($this, 'insertRecordSets')) { |
||
| 123 | foreach ($sets as $set) { |
||
| 124 | $this->insertRecordSets($set); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if (method_exists($this, 'beforeInsertMany')) { |
||
| 129 | $this->beforeInsertMany($sets); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($this->qb->collection($collection)->insertBatch($sets)) { |
||
| 133 | if (method_exists($this, 'afterInsertMany')) { |
||
| 134 | return $this->afterInsertMany(); |
||
| 135 | } |
||
| 136 | |||
| 137 | return true; |
||
| 138 | } |
||
| 139 | |||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | // ------------------------------------------------------------------------ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param array $sets |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public function updateMany(array $sets) |
||
| 150 | { |
||
| 151 | $collection = isset($collection) ? $collection : $this->collection; |
||
| 152 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 153 | |||
| 154 | $where = []; |
||
| 155 | if (empty($this->primaryKeys)) { |
||
| 156 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 157 | $this->qb->where($primaryKey, $sets[ $primaryKey ]); |
||
| 158 | } else { |
||
| 159 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 160 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // Reset Primary Keys |
||
| 165 | $this->primaryKey = 'id'; |
||
| 166 | $this->primaryKeys = []; |
||
| 167 | |||
| 168 | if (method_exists($this, 'updateRecordSets')) { |
||
| 169 | foreach ($sets as $set) { |
||
| 170 | $this->updateRecordSets($set); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if (method_exists($this, 'beforeUpdateMany')) { |
||
| 175 | $this->beforeUpdateMany($sets); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($this->qb->collection($collection)->updateBatch($sets, $where)) { |
||
| 179 | if (method_exists($this, 'afterUpdateMany')) { |
||
| 180 | return $this->afterUpdateMany(); |
||
| 181 | } |
||
| 182 | |||
| 183 | return true; |
||
| 184 | } |
||
| 185 | |||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | // ------------------------------------------------------------------------ |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Trash many rows from the database collection based on sets of ids. |
||
| 193 | * |
||
| 194 | * @param array $ids |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function trashMany(array $ids) |
||
| 199 | { |
||
| 200 | $affectedRows = []; |
||
| 201 | |||
| 202 | foreach ($ids as $id) { |
||
| 203 | $affectedRows[ $id ] = $this->trash($id); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $affectedRows; |
||
| 207 | } |
||
| 208 | |||
| 209 | // ------------------------------------------------------------------------ |
||
| 210 | |||
| 211 | /** |
||
| 212 | * trash |
||
| 213 | * |
||
| 214 | * @param $id |
||
| 215 | * @param null $collection |
||
| 216 | * |
||
| 217 | * @return array|bool |
||
| 218 | */ |
||
| 219 | public function trash($id, $collection = null) |
||
| 220 | { |
||
| 221 | $collection = isset($collection) ? $collection : $this->collection; |
||
| 222 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 223 | |||
| 224 | $sets = []; |
||
| 225 | $where = []; |
||
| 226 | |||
| 227 | if (empty($this->primaryKeys)) { |
||
| 228 | $where[ $primaryKey ] = $id; |
||
| 229 | $sets[ $primaryKey ] = $id; |
||
| 230 | } elseif (is_array($id)) { |
||
| 231 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 232 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 233 | $sets[ $primaryKey ] = $id[ $primaryKey ]; |
||
| 234 | } |
||
| 235 | } else { |
||
| 236 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 237 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 238 | } |
||
| 239 | |||
| 240 | $sets[ reset($this->primaryKeys) ] = $id; |
||
| 241 | } |
||
| 242 | |||
| 243 | // Reset Primary Keys |
||
| 244 | $this->primaryKey = 'id'; |
||
| 245 | $this->primaryKeys = []; |
||
| 246 | |||
| 247 | if (method_exists($this, 'updateRecordSets')) { |
||
| 248 | $this->setRecordStatus('TRASH'); |
||
| 249 | $this->updateRecordSets($sets); |
||
| 250 | } |
||
| 251 | |||
| 252 | if (method_exists($this, 'beforeTrash')) { |
||
| 253 | $this->beforeTrash($sets); |
||
| 254 | } |
||
| 255 | |||
| 256 | if ($this->qb->collection($collection)->update($sets, $where)) { |
||
| 257 | if (method_exists($this, 'afterTrash')) { |
||
| 258 | return $this->afterTrash(); |
||
| 259 | } |
||
| 260 | |||
| 261 | return true; |
||
| 262 | } |
||
| 263 | |||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | // ------------------------------------------------------------------------ |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $ids |
||
| 271 | * @param array $where |
||
| 272 | * @param null $collection |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function trashManyBy(array $ids, $where = [], $collection = null) |
||
| 276 | { |
||
| 277 | $affectedRows = []; |
||
| 278 | |||
| 279 | foreach ($ids as $id) { |
||
| 280 | $affectedRows[ $id ] = $this->trashBy($id, $where, $collection); |
||
| 281 | } |
||
| 282 | |||
| 283 | return $affectedRows; |
||
| 284 | } |
||
| 285 | |||
| 286 | // ------------------------------------------------------------------------ |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param $id |
||
| 290 | * @param array $where |
||
| 291 | * @param null $collection |
||
| 292 | * @return array|bool |
||
| 293 | */ |
||
| 294 | public function trashBy($id, array $where = [], $collection = null) |
||
| 295 | { |
||
| 296 | $this->qb->where($where); |
||
| 297 | |||
| 298 | return $this->trash($id, $collection); |
||
| 299 | } |
||
| 300 | |||
| 301 | // ------------------------------------------------------------------------ |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param array $ids |
||
| 305 | * @param bool $force |
||
| 306 | * @param null $collection |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function deleteMany(array $ids, $force = false, $collection = null) |
||
| 310 | { |
||
| 311 | $affectedRows = []; |
||
| 312 | |||
| 313 | foreach ($ids as $id) { |
||
| 314 | $affectedRows[ $id ] = $this->delete($id, $force, $collection); |
||
| 315 | } |
||
| 316 | |||
| 317 | return $affectedRows; |
||
| 318 | } |
||
| 319 | |||
| 320 | // ------------------------------------------------------------------------ |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param $id |
||
| 324 | * @param bool $force |
||
| 325 | * @param null $collection |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function delete($id, $force = false, $collection = null) |
||
| 329 | { |
||
| 330 | if ((isset($collection) AND is_bool($collection)) OR ! isset($collection)) { |
||
| 331 | $collection = $this->collection; |
||
| 332 | } |
||
| 333 | |||
| 334 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 335 | |||
| 336 | $where = []; |
||
| 337 | if (empty($this->primaryKeys)) { |
||
| 338 | $where[ $primaryKey ] = $id; |
||
| 339 | } elseif (is_array($id)) { |
||
| 340 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 341 | $where[ $primaryKey ] = $id[ $primaryKey ]; |
||
| 342 | } |
||
| 343 | } else { |
||
| 344 | $where[ reset($this->primaryKeys) ] = $id; |
||
| 345 | } |
||
| 346 | |||
| 347 | // Reset Primary Keys |
||
| 348 | $this->primaryKey = 'id'; |
||
| 349 | $this->primaryKeys = []; |
||
| 350 | |||
| 351 | if (method_exists($this, 'beforeDelete')) { |
||
| 352 | $this->beforeDelete(); |
||
| 353 | } |
||
| 354 | |||
| 355 | if ($this->qb->collection($collection)->delete($where)) { |
||
| 356 | if (method_exists($this, 'afterDelete')) { |
||
| 357 | return $this->afterDelete(); |
||
| 358 | } |
||
| 359 | |||
| 360 | return true; |
||
| 361 | } |
||
| 362 | |||
| 363 | return false; |
||
| 364 | } |
||
| 365 | |||
| 366 | // ------------------------------------------------------------------------ |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param array $ids |
||
| 370 | * @param array $where |
||
| 371 | * @param bool $force |
||
| 372 | * @param null $collection |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function deleteManyBy(array $ids, $where = [], $force = false, $collection = null) |
||
| 376 | { |
||
| 377 | $affectedRows = []; |
||
| 378 | |||
| 379 | foreach ($ids as $id) { |
||
| 380 | $affectedRows[ $id ] = $this->deleteBy($id, $where, $force, $collection); |
||
| 381 | } |
||
| 382 | |||
| 383 | return $affectedRows; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param $id |
||
| 388 | * @param array $where |
||
| 389 | * @param bool $force |
||
| 390 | * @param null $collection |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function deleteBy($id, $where = [], $force = false, $collection = null) |
||
| 394 | { |
||
| 395 | $this->qb->where($where); |
||
| 396 | |||
| 397 | return $this->delete($id, $force, $collection); |
||
| 398 | } |
||
| 399 | |||
| 400 | // ------------------------------------------------------------------------ |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param $id |
||
| 404 | * @param null $collection |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function restore($id, $collection = null) |
||
| 410 | } |
||
| 411 | |||
| 412 | // ------------------------------------------------------------------------ |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param $id |
||
| 416 | * @param null $collection |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function publish($id, $collection = null) |
||
| 420 | { |
||
| 421 | $collection = isset($collection) ? $collection : $this->collection; |
||
| 422 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 423 | |||
| 424 | $sets = []; |
||
| 425 | $where = []; |
||
| 426 | |||
| 427 | if (empty($this->primaryKeys)) { |
||
| 428 | $where[ $primaryKey ] = $id; |
||
| 429 | $sets[ $primaryKey ] = $id; |
||
| 430 | } elseif (is_array($id)) { |
||
| 431 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 432 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 433 | $sets[ $primaryKey ] = $id[ $primaryKey ]; |
||
| 434 | } |
||
| 435 | } else { |
||
| 436 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 437 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 438 | } |
||
| 439 | |||
| 440 | $sets[ reset($this->primaryKeys) ] = $id; |
||
| 441 | } |
||
| 442 | |||
| 443 | // Reset Primary Keys |
||
| 444 | $this->primaryKey = 'id'; |
||
| 445 | $this->primaryKeys = []; |
||
| 446 | |||
| 447 | if (method_exists($this, 'updateRecordSets')) { |
||
| 448 | $this->setRecordStatus('PUBLISH'); |
||
| 449 | $this->updateRecordSets($sets); |
||
| 450 | } |
||
| 451 | |||
| 452 | if (method_exists($this, 'beforePublish')) { |
||
| 453 | $this->beforePublish($sets); |
||
| 454 | } |
||
| 455 | |||
| 456 | if ($this->qb->collection($collection)->update($sets, $where)) { |
||
| 457 | if (method_exists($this, 'afterPublish')) { |
||
| 458 | return $this->afterPublish(); |
||
| 459 | } |
||
| 460 | |||
| 461 | return true; |
||
| 462 | } |
||
| 463 | |||
| 464 | return false; |
||
| 465 | } |
||
| 466 | |||
| 467 | // ------------------------------------------------------------------------ |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param $id |
||
| 471 | * @param array $where |
||
| 472 | * @param null $collection |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | public function restoreBy($id, array $where = [], $collection = null) |
||
| 476 | { |
||
| 477 | return $this->publishBy($id, $where, $collection); |
||
| 478 | } |
||
| 479 | |||
| 480 | // ------------------------------------------------------------------------ |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param $id |
||
| 484 | * @param array $where |
||
| 485 | * @param null $collection |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | public function publishBy($id, array $where = [], $collection = null) |
||
| 489 | { |
||
| 490 | $this->qb->where($where); |
||
| 491 | |||
| 492 | return $this->publish($id, $collection); |
||
| 493 | } |
||
| 494 | |||
| 495 | // ------------------------------------------------------------------------ |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param array $ids |
||
| 499 | * @param null $collection |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | public function restoreMany(array $ids, $collection = null) |
||
| 503 | { |
||
| 504 | return $this->publishMany($ids, $collection); |
||
| 505 | } |
||
| 506 | |||
| 507 | // ------------------------------------------------------------------------ |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param array $ids |
||
| 511 | * @param null $collection |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | public function publishMany(array $ids, $collection = null) |
||
| 515 | { |
||
| 516 | $affectedRows = []; |
||
| 517 | |||
| 518 | foreach ($ids as $id) { |
||
| 519 | $affectedRows[ $id ] = $this->publish($id, $collection); |
||
| 520 | } |
||
| 521 | |||
| 522 | return $affectedRows; |
||
| 523 | } |
||
| 524 | |||
| 525 | // ------------------------------------------------------------------------ |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param array $ids |
||
| 529 | * @param array $where |
||
| 530 | * @param null $collection |
||
| 531 | * @return array |
||
| 532 | */ |
||
| 533 | public function restoreManyBy(array $ids, $where = [], $collection = null) |
||
| 536 | } |
||
| 537 | |||
| 538 | // ------------------------------------------------------------------------ |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param array $ids |
||
| 542 | * @param array $where |
||
| 543 | * @param null $collection |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | public function publishManyBy(array $ids, $where = [], $collection = null) |
||
| 555 | } |
||
| 556 | } |