GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ModifierTrait::updateMany()   B
last analyzed

Complexity

Conditions 10
Paths 96

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 1
b 0
f 0
nc 96
nop 1
dl 0
loc 38
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Models\NoSql\Traits;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class TraitModifier
20
 *
21
 * @package O2System\Framework\Models\NoSql\Traits
22
 */
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';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
        $this->primaryKeys = [];
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $collection seems to never exist and therefore isset should always be false.
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $collection seems to never exist and therefore isset should always be false.
Loading history...
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';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
166
        $this->primaryKeys = [];
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
245
        $this->primaryKeys = [];
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
246
247
        if (method_exists($this, 'updateRecordSets')) {
248
            $this->setRecordStatus('TRASH');
0 ignored issues
show
Bug introduced by
It seems like setRecordStatus() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
            $this->/** @scrutinizer ignore-call */ 
249
                   setRecordStatus('TRASH');
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
326
     * @return bool
327
     */
328
    public function delete($id, $force = false, $collection = null)
0 ignored issues
show
Unused Code introduced by
The parameter $force is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

328
    public function delete($id, /** @scrutinizer ignore-unused */ $force = false, $collection = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
349
        $this->primaryKeys = [];
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
405
     * @return bool
406
     */
407
    public function restore($id, $collection = null)
408
    {
409
        return $this->publish($id, $collection);
410
    }
411
412
    // ------------------------------------------------------------------------
413
414
    /**
415
     * @param $id
416
     * @param null $collection
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
445
        $this->primaryKeys = [];
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
531
     * @return array
532
     */
533
    public function restoreManyBy(array $ids, $where = [], $collection = null)
534
    {
535
        return $this->publishManyBy($ids, $where, $collection);
536
    }
537
538
    // ------------------------------------------------------------------------
539
540
    /**
541
     * @param array $ids
542
     * @param array $where
543
     * @param null $collection
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collection is correct as it would always require null to be passed?
Loading history...
544
     * @return array
545
     */
546
    public function publishManyBy(array $ids, $where = [], $collection = null)
547
    {
548
        $affectedRows = [];
549
550
        foreach ($ids as $id) {
551
            $affectedRows[ $id ] = $this->publishBy($id, $where, $collection);
552
        }
553
554
        return $affectedRows;
555
    }
556
}