| Total Complexity | 95 |
| Total Lines | 464 |
| Duplicated Lines | 0 % |
| Changes | 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 | * Insert Data |
||
| 27 | * |
||
| 28 | * Method to input data as well as equipping the data in accordance with the fields |
||
| 29 | * in the destination database table. |
||
| 30 | * |
||
| 31 | * @access public |
||
| 32 | * |
||
| 33 | * @param array $sets Array of Input Data |
||
| 34 | * @param string $table Table Name |
||
| 35 | * |
||
| 36 | * @return mixed |
||
| 37 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
| 38 | */ |
||
| 39 | public function insert(array $sets, $table = null) |
||
| 40 | { |
||
| 41 | $table = isset($table) ? $table : $this->table; |
||
| 42 | |||
| 43 | if (method_exists($this, 'insertRecordSets')) { |
||
| 44 | $this->insertRecordSets($sets); |
||
| 45 | } |
||
| 46 | |||
| 47 | if (method_exists($this, 'beforeInsert')) { |
||
| 48 | $this->beforeInsert($sets); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (method_exists($this, 'getRecordOrdering')) { |
||
| 52 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
| 53 | $sets[ 'record_ordering' ] = $this->getRecordOrdering($table); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($this->qb->table($table)->insert($sets)) { |
||
| 58 | if (method_exists($this, 'afterInsert')) { |
||
| 59 | return $this->afterInsert(); |
||
| 60 | } |
||
| 61 | |||
| 62 | return true; |
||
| 63 | } |
||
| 64 | |||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | // ------------------------------------------------------------------------ |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Update Data |
||
| 72 | * |
||
| 73 | * Method to update data as well as equipping the data in accordance with the fields |
||
| 74 | * in the destination database table. |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * |
||
| 78 | * @param string $table Table Name |
||
| 79 | * @param array $sets Array of Update Data |
||
| 80 | * |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | public function update(array $sets, $where = [], $table = null) |
||
| 84 | { |
||
| 85 | $table = isset($table) ? $table : $this->table; |
||
| 86 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 87 | |||
| 88 | if (empty($where)) { |
||
| 89 | if (empty($this->primaryKeys)) { |
||
| 90 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 91 | } else { |
||
| 92 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 93 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // Reset Primary Keys |
||
| 99 | $this->primaryKey = 'id'; |
||
|
|
|||
| 100 | $this->primaryKeys = []; |
||
| 101 | |||
| 102 | if (method_exists($this, 'updateRecordSets')) { |
||
| 103 | $this->updateRecordSets($sets); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (method_exists($this, 'beforeUpdate')) { |
||
| 107 | $sets = $this->beforeUpdate($sets); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (method_exists($this, 'getRecordOrdering')) { |
||
| 111 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
| 112 | $sets[ 'record_ordering' ] = $this->getRecordOrdering($table); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($this->qb->table($table)->update($sets, $where)) { |
||
| 117 | |||
| 118 | if (method_exists($this, 'afterUpdate')) { |
||
| 119 | return $this->afterUpdate(); |
||
| 120 | } |
||
| 121 | |||
| 122 | return true; |
||
| 123 | } |
||
| 124 | |||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function insertMany(array $sets) |
||
| 129 | { |
||
| 130 | $table = isset($table) ? $table : $this->table; |
||
| 131 | |||
| 132 | if (method_exists($this, 'insertRecordSets')) { |
||
| 133 | foreach ($sets as $set) { |
||
| 134 | $this->insertRecordSets($set); |
||
| 135 | |||
| 136 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
| 137 | $set[ 'record_ordering' ] = $this->getRecordOrdering($table); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | if (method_exists($this, 'beforeInsertMany')) { |
||
| 143 | $this->beforeInsertMany($sets); |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($this->qb->table($table)->insertBatch($sets)) { |
||
| 147 | if (method_exists($this, 'afterInsertMany')) { |
||
| 148 | return $this->afterInsertMany(); |
||
| 149 | } |
||
| 150 | |||
| 151 | return true; |
||
| 152 | } |
||
| 153 | |||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | // ------------------------------------------------------------------------ |
||
| 158 | |||
| 159 | public function updateMany(array $sets) |
||
| 160 | { |
||
| 161 | $table = isset($table) ? $table : $this->table; |
||
| 162 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 163 | |||
| 164 | $where = []; |
||
| 165 | if (empty($this->primaryKeys)) { |
||
| 166 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 167 | $this->qb->where($primaryKey, $sets[ $primaryKey ]); |
||
| 168 | } else { |
||
| 169 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 170 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // Reset Primary Keys |
||
| 175 | $this->primaryKey = 'id'; |
||
| 176 | $this->primaryKeys = []; |
||
| 177 | |||
| 178 | if (method_exists($this, 'updateRecordSets')) { |
||
| 179 | foreach ($sets as $set) { |
||
| 180 | $this->updateRecordSets($set); |
||
| 181 | |||
| 182 | if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) { |
||
| 183 | $set[ 'record_ordering' ] = $this->getRecordOrdering($table); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | if (method_exists($this, 'beforeUpdateMany')) { |
||
| 189 | $this->beforeUpdateMany($sets); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ($this->qb->table($table)->updateBatch($sets, $where)) { |
||
| 193 | if (method_exists($this, 'afterUpdateMany')) { |
||
| 194 | return $this->afterUpdateMany(); |
||
| 195 | } |
||
| 196 | |||
| 197 | return true; |
||
| 198 | } |
||
| 199 | |||
| 200 | return false; |
||
| 201 | } |
||
| 202 | |||
| 203 | // ------------------------------------------------------------------------ |
||
| 204 | |||
| 205 | /** |
||
| 206 | * trash |
||
| 207 | * |
||
| 208 | * @param $id |
||
| 209 | * @param null $table |
||
| 210 | * |
||
| 211 | * @return array|bool |
||
| 212 | */ |
||
| 213 | public function trash($id, $table = null) |
||
| 214 | { |
||
| 215 | $table = isset($table) ? $table : $this->table; |
||
| 216 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 217 | |||
| 218 | $sets = []; |
||
| 219 | $where = []; |
||
| 220 | |||
| 221 | if (empty($this->primaryKeys)) { |
||
| 222 | $where[ $primaryKey ] = $id; |
||
| 223 | $sets[ $primaryKey ] = $id; |
||
| 224 | } elseif (is_array($id)) { |
||
| 225 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 226 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 227 | $sets[ $primaryKey ] = $id[ $primaryKey ]; |
||
| 228 | } |
||
| 229 | } else { |
||
| 230 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 231 | $where[ $primaryKey ] = $sets[ $primaryKey ]; |
||
| 232 | } |
||
| 233 | |||
| 234 | $sets[ reset($this->primaryKeys) ] = $id; |
||
| 235 | } |
||
| 236 | |||
| 237 | // Reset Primary Keys |
||
| 238 | $this->primaryKey = 'id'; |
||
| 239 | $this->primaryKeys = []; |
||
| 240 | |||
| 241 | if (method_exists($this, 'updateRecordSets')) { |
||
| 242 | $this->setRecordStatus('TRASH'); |
||
| 243 | $this->updateRecordSets($sets); |
||
| 244 | } |
||
| 245 | |||
| 246 | if (method_exists($this, 'beforeTrash')) { |
||
| 247 | $this->beforeTrash($sets); |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($this->qb->table($table)->update($sets, $where)) { |
||
| 251 | if (method_exists($this, 'afterTrash')) { |
||
| 252 | return $this->afterTrash(); |
||
| 253 | } |
||
| 254 | |||
| 255 | return true; |
||
| 256 | } |
||
| 257 | |||
| 258 | return false; |
||
| 259 | } |
||
| 260 | |||
| 261 | // ------------------------------------------------------------------------ |
||
| 262 | |||
| 263 | public function trashBy($id, array $where = [], $table = null) |
||
| 264 | { |
||
| 265 | $this->qb->where($where); |
||
| 266 | |||
| 267 | return $this->trash($id, $table); |
||
| 268 | } |
||
| 269 | |||
| 270 | // ------------------------------------------------------------------------ |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Trash many rows from the database table based on sets of ids. |
||
| 274 | * |
||
| 275 | * @param array $ids |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | public function trashMany(array $ids) |
||
| 280 | { |
||
| 281 | $affectedRows = []; |
||
| 282 | |||
| 283 | foreach ($ids as $id) { |
||
| 284 | $affectedRows[ $id ] = $this->trash($id); |
||
| 285 | } |
||
| 286 | |||
| 287 | return $affectedRows; |
||
| 288 | } |
||
| 289 | |||
| 290 | // ------------------------------------------------------------------------ |
||
| 291 | |||
| 292 | public function trashManyBy(array $ids, $where = [], $table = null) |
||
| 293 | { |
||
| 294 | $affectedRows = []; |
||
| 295 | |||
| 296 | foreach ($ids as $id) { |
||
| 297 | $affectedRows[ $id ] = $this->trashBy($id, $where, $table); |
||
| 298 | } |
||
| 299 | |||
| 300 | return $affectedRows; |
||
| 301 | } |
||
| 302 | |||
| 303 | // ------------------------------------------------------------------------ |
||
| 304 | |||
| 305 | public function delete($id, $force = false, $table = null) |
||
| 306 | { |
||
| 307 | if ((isset($table) AND is_bool($table)) OR ! isset($table)) { |
||
| 308 | $table = $this->table; |
||
| 309 | } |
||
| 310 | |||
| 311 | $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id'; |
||
| 312 | |||
| 313 | $where = []; |
||
| 314 | if (empty($this->primaryKeys)) { |
||
| 315 | $where[ $primaryKey ] = $id; |
||
| 316 | } elseif (is_array($id)) { |
||
| 317 | foreach ($this->primaryKeys as $primaryKey) { |
||
| 318 | $where[ $primaryKey ] = $id[ $primaryKey ]; |
||
| 319 | } |
||
| 320 | } else { |
||
| 321 | $where[ reset($this->primaryKeys) ] = $id; |
||
| 322 | } |
||
| 323 | |||
| 324 | // Reset Primary Keys |
||
| 325 | $this->primaryKey = 'id'; |
||
| 326 | $this->primaryKeys = []; |
||
| 327 | |||
| 328 | if (method_exists($this, 'beforeDelete')) { |
||
| 329 | $this->beforeDelete(); |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($this->qb->table($table)->delete($where)) { |
||
| 333 | if (method_exists($this, 'afterDelete')) { |
||
| 334 | return $this->afterDelete(); |
||
| 335 | } |
||
| 336 | |||
| 337 | return true; |
||
| 338 | } |
||
| 339 | |||
| 340 | return false; |
||
| 341 | } |
||
| 342 | |||
| 343 | // ------------------------------------------------------------------------ |
||
| 344 | |||
| 345 | public function deleteBy($id, $where = [], $force = false, $table = null) |
||
| 346 | { |
||
| 347 | $this->qb->where($where); |
||
| 348 | |||
| 349 | return $this->delete($id, $force, $table); |
||
| 350 | } |
||
| 351 | |||
| 352 | // ------------------------------------------------------------------------ |
||
| 353 | |||
| 354 | public function deleteMany(array $ids, $force = false, $table = null) |
||
| 355 | { |
||
| 356 | $affectedRows = []; |
||
| 357 | |||
| 358 | foreach ($ids as $id) { |
||
| 359 | $affectedRows[ $id ] = $this->delete($id, $force, $table); |
||
| 360 | } |
||
| 361 | |||
| 362 | return $affectedRows; |
||
| 363 | } |
||
| 364 | |||
| 365 | public function deleteManyBy(array $ids, $where = [], $force = false, $table = null) |
||
| 366 | { |
||
| 367 | $affectedRows = []; |
||
| 368 | |||
| 369 | foreach ($ids as $id) { |
||
| 370 | $affectedRows[ $id ] = $this->deleteBy($id, $where, $force, $table); |
||
| 371 | } |
||
| 372 | |||
| 373 | return $affectedRows; |
||
| 374 | } |
||
| 375 | |||
| 376 | // ------------------------------------------------------------------------ |
||
| 377 | |||
| 378 | public function publish($id, $table = null) |
||
| 424 | } |
||
| 425 | |||
| 426 | // ------------------------------------------------------------------------ |
||
| 427 | |||
| 428 | public function publishBy($id, array $where = [], $table = null) |
||
| 429 | { |
||
| 430 | $this->qb->where($where); |
||
| 431 | |||
| 432 | return $this->publish($id, $table); |
||
| 433 | } |
||
| 434 | |||
| 435 | // ------------------------------------------------------------------------ |
||
| 436 | |||
| 437 | public function publishMany(array $ids, $table = null) |
||
| 438 | { |
||
| 439 | $affectedRows = []; |
||
| 440 | |||
| 441 | foreach ($ids as $id) { |
||
| 442 | $affectedRows[ $id ] = $this->publish($id, $table); |
||
| 443 | } |
||
| 444 | |||
| 445 | return $affectedRows; |
||
| 446 | } |
||
| 447 | |||
| 448 | // ------------------------------------------------------------------------ |
||
| 449 | |||
| 450 | public function publishManyBy(array $ids, $where = [], $table = null) |
||
| 451 | { |
||
| 452 | $affectedRows = []; |
||
| 453 | |||
| 454 | foreach ($ids as $id) { |
||
| 455 | $affectedRows[ $id ] = $this->publishBy($id, $where, $table); |
||
| 456 | } |
||
| 457 | |||
| 458 | return $affectedRows; |
||
| 459 | } |
||
| 460 | |||
| 461 | // ------------------------------------------------------------------------ |
||
| 462 | |||
| 463 | public function restore($id, $table = null) |
||
| 464 | { |
||
| 465 | return $this->publish($id, $table); |
||
| 466 | } |
||
| 467 | |||
| 468 | // ------------------------------------------------------------------------ |
||
| 469 | |||
| 470 | public function restoreBy($id, array $where = [], $table = null) |
||
| 471 | { |
||
| 472 | return $this->publishBy($id, $where, $table); |
||
| 473 | } |
||
| 474 | |||
| 475 | // ------------------------------------------------------------------------ |
||
| 476 | |||
| 477 | public function restoreMany(array $ids, $table = null) |
||
| 478 | { |
||
| 479 | return $this->publishMany($ids, $table); |
||
| 480 | } |
||
| 481 | |||
| 482 | // ------------------------------------------------------------------------ |
||
| 483 | |||
| 484 | public function restoreManyBy(array $ids, $where = [], $table = null) |
||
| 487 | } |
||
| 488 | } |