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::lockManyBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\Sql\Traits;
15
16
// ------------------------------------------------------------------------
17
use O2System\Framework\Libraries\Ui\Contents\Lists\Unordered;
18
use O2System\Framework\Models\Sql\DataObjects\Result;
19
use O2System\Framework\Models\Sql\DataObjects\Result\Row;
20
use O2System\Image\Uploader;
21
22
/**
23
 * Class TraitModifier
24
 *
25
 * @package O2System\Framework\Models\Sql\Traits
26
 */
27
trait ModifierTrait
28
{
29
    /**
30
     * ModifierTrait::insert
31
     *
32
     * @param array $sets
33
     *
34
     * @return bool
35
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException
36
     */
37
    public function insert(array $sets)
38
    {
39
        if (count($sets)) {
40
            if (count($this->fillableColumns)) {
41
                foreach ($sets as $key => $value) {
42
                    if ( ! in_array($key, $this->fillableColumns)) {
43
                        unset($sets[ $key ]);
44
                    }
45
                }
46
            }
47
48
            if (method_exists($this, 'insertRecordSets')) {
49
                $this->insertRecordSets($sets);
50
            }
51
52
            if (method_exists($this, 'beforeInsert')) {
53
                $this->beforeInsert($sets);
54
            }
55
56
            if (method_exists($this, 'getRecordOrdering')) {
57
                if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) {
58
                    $sets[ 'record_ordering' ] = $this->getRecordOrdering();
59
                }
60
            }
61
62
            if (isset($this->uploadedImageFilePath)) {
63
                if ( ! file_exists($this->uploadedImageFilePath)) {
64
                    mkdir($this->uploadedImageFilePath, 0777, true);
65
                }
66
67
                $upload = new Uploader();
68
                $upload->setPath($this->uploadedImageFilePath);
69
70
                if ($files = input()->files()) {
0 ignored issues
show
Bug introduced by
The method files() does not exist on O2System\Kernel\Cli\Input. ( Ignorable by Annotation )

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

70
                if ($files = input()->/** @scrutinizer ignore-call */ files()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
                    // Uploaded Image Process
72
                    if (isset($this->uploadedImageKey)) {
73
                        if (isset($files[ $this->uploadedImageKey ])) {
74
                            $upload->process($this->uploadedImageKey);
75
76
                            if ($upload->getErrors()) {
77
                                $errors = new Unordered();
78
79
                                foreach ($upload->getErrors() as $code => $error) {
80
                                    $errors->createList($error);
81
                                }
82
83
                                if (services()->has('session')) {
84
                                    session()->setFlash('danger', $errors);
85
                                }
86
87
                                return false;
88
                            }
89
                        }
90
                    } elseif (count($this->uploadedImageKeys)) {
91
                        foreach ($this->uploadedImageKeys as $uploadedImageKey) {
92
                            if (isset($files[ $uploadedImageKey ])) {
93
                                $upload->process($uploadedImageKey);
94
95
                                if ($upload->getErrors()) {
96
                                    $errors = new Unordered();
97
98
                                    foreach ($upload->getErrors() as $code => $error) {
99
                                        $errors->createList($error);
100
                                    }
101
102
                                    if (services()->has('session')) {
103
                                        session()->setFlash('danger', $errors);
104
                                    }
105
106
                                    return false;
107
                                }
108
                            }
109
                        }
110
                    }
111
112
                    // Uploaded File Process
113
                    if (isset($this->uploadedFileFilepath)) {
114
                        if (isset($files[ $this->uploadedFileKey ])) {
115
                            $upload->process($this->uploadedFileKey);
116
117
                            if ($upload->getErrors()) {
118
                                $errors = new Unordered();
119
120
                                foreach ($upload->getErrors() as $code => $error) {
121
                                    $errors->createList($error);
122
                                }
123
124
                                if (services()->has('session')) {
125
                                    session()->setFlash('danger', $errors);
126
                                }
127
128
                                return false;
129
                            }
130
                        }
131
                    } elseif (count($this->uploadedFileKeys)) {
132
                        foreach ($this->uploadedFileKeys as $uploadedFileKey) {
133
                            if (isset($files[ $uploadedFileKey ])) {
134
                                $upload->process($uploadedFileKey);
135
136
                                if ($upload->getErrors()) {
137
                                    $errors = new Unordered();
138
139
                                    foreach ($upload->getErrors() as $code => $error) {
140
                                        $errors->createList($error);
141
                                    }
142
143
                                    if (services()->has('session')) {
144
                                        session()->setFlash('danger', $errors);
145
                                    }
146
147
                                    return false;
148
                                }
149
                            }
150
                        }
151
                    }
152
                }
153
            }
154
155
            if ($this->qb->table($this->table)->insert($sets)) {
156
                if (method_exists($this, 'afterInsert')) {
157
                    $this->afterInsert();
158
                }
159
160
                if (method_exists($this, 'rebuildTree')) {
161
                    $this->rebuildTree();
162
                }
163
164
                $label = false;
165
                foreach (['name', 'label', 'title', 'code'] as $labelField) {
166
                    if (isset($sets[ $labelField ])) {
167
                        if(services()->has('session')) {
168
                            session()->setFlash('success', language('SUCCESS_INSERT_WITH_LABEL', [$sets[ $labelField ]]));
169
                        }
170
171
                        $label = true;
172
                        break;
173
                    }
174
                }
175
176
                if ($label === false) {
177
                    if(services()->has('session')) {
178
                        session()->setFlash('success', language('SUCCESS_INSERT'));
179
                    }
180
                }
181
182
                return true;
183
            }
184
        }
185
186
        $label = false;
187
        foreach (['name', 'label', 'title', 'code'] as $labelField) {
188
            if (isset($sets[ $labelField ])) {
189
                if(services()->has('session')) {
190
                    session()->setFlash('danger', language('FAILED_INSERT_WITH_LABEL', [$sets[ $labelField ]]));
191
                }
192
193
                $label = true;
194
                break;
195
            }
196
        }
197
198
        if ($label === false) {
199
            if(services()->has('session')) {
200
                session()->setFlash('danger', language('FAILED_INSERT'));
201
            }
202
        }
203
204
        // Sets Global $_POST Variable
205
        $_POST = $sets;
206
207
        return false;
208
    }
209
210
    // ------------------------------------------------------------------------
211
212
    /**
213
     * ModifierTrait::insertOrUpdate
214
     *
215
     * @param array $sets
216
     * @param array $conditions
217
     *
218
     * @return bool
219
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException
220
     */
221
    public function insertOrUpdate(array $sets, array $conditions = [])
222
    {
223
        if (count($sets)) {
224
            $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';
225
226
            if (empty($conditions)) {
227
                if (isset($sets[ $primaryKey ])) {
228
                    $conditions = [$primaryKey => $sets[ $primaryKey ]];
229
                } else {
230
                    $conditions = $sets;
231
                }
232
            }
233
234
            // Try to find
235
            if ($result = $this->qb->from($this->table)->getWhere($conditions)) {
236
                if ($result->count() > 0) {
237
                    return $this->update($sets, $conditions);
238
                } else {
239
                    return $this->insert($sets);
240
                }
241
            }
242
        }
243
244
        return false;
245
    }
246
247
    // ------------------------------------------------------------------------
248
249
    /**
250
     * ModifierTrait::insertMany
251
     *
252
     * @param array $sets
253
     *
254
     * @return bool|int
255
     */
256
    public function insertMany(array $sets)
257
    {
258
        if (count($sets)) {
259
            if (method_exists($this, 'insertRecordSets')) {
260
                foreach ($sets as $set) {
261
                    $this->insertRecordSets($set);
262
                    if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) {
263
                        $set[ 'record_ordering' ] = $this->getRecordOrdering();
0 ignored issues
show
Bug introduced by
It seems like getRecordOrdering() 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

263
                        /** @scrutinizer ignore-call */ 
264
                        $set[ 'record_ordering' ] = $this->getRecordOrdering();
Loading history...
264
                    }
265
                }
266
            }
267
268
            if (method_exists($this, 'beforeInsertMany')) {
269
                $this->beforeInsertMany($sets);
270
            }
271
272
            if ($this->qb->table($this->table)->insertBatch($sets)) {
273
                if (method_exists($this, 'afterInsertMany')) {
274
                    $this->afterInsertMany();
275
                }
276
277
                $affectedRows = $this->db->getAffectedRows();
278
279
                if (method_exists($this, 'rebuildTree')) {
280
                    $this->rebuildTree();
281
                }
282
283
                return $affectedRows;
284
            }
285
        }
286
287
        return false;
288
    }
289
290
    // ------------------------------------------------------------------------
291
292
    /**
293
     * ModifierTrait::insertIfNotExists
294
     *
295
     * @param array $sets
296
     * @param array $conditions
297
     *
298
     * @return bool
299
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException
300
     */
301
    public function insertIfNotExists(array $sets, array $conditions = [])
302
    {
303
        if (empty($conditions)) {
304
            $conditions = $sets;
305
        }
306
307
        if (count($sets)) {
308
            if ($result = $this->qb->from($this->table)->getWhere($conditions)) {
309
                if ($result->count() == 0) {
310
                    return $this->insert($sets);
311
                }
312
            }
313
        }
314
315
        return false;
316
    }
317
318
    // ------------------------------------------------------------------------
319
320
    /**
321
     * ModifierTrait::update
322
     *
323
     * @param array $sets
324
     * @param array $conditions
325
     *
326
     * @return bool
327
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException
328
     */
329
    public function update(array $sets, array $conditions = [])
330
    {
331
        if (count($sets)) {
332
            $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';
333
334
            if (empty($conditions)) {
335
                if (isset($sets[ $primaryKey ])) {
336
                    $conditions = [$primaryKey => $sets[ $primaryKey ]];
337
                }
338
            }
339
340
            if (count($this->fillableColumns)) {
341
                foreach ($sets as $key => $value) {
342
                    if ( ! in_array($key, $this->fillableColumns)) {
343
                        unset($sets[ $key ]);
344
                    }
345
                }
346
            }
347
348
            if (method_exists($this, 'updateRecordSets')) {
349
                $this->updateRecordSets($sets);
350
            }
351
352
            if (method_exists($this, 'beforeUpdate')) {
353
                $sets = $this->beforeUpdate($sets);
354
            }
355
356
            if (method_exists($this, 'getRecordOrdering')) {
357
                if ($this->recordOrdering === true && empty($sets[ 'record_ordering' ])) {
358
                    $sets[ 'record_ordering' ] = $this->getRecordOrdering();
359
                }
360
            }
361
362
            if ($row = $this->findWhere($conditions)) {
0 ignored issues
show
Bug introduced by
It seems like findWhere() 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

362
            if ($row = $this->/** @scrutinizer ignore-call */ findWhere($conditions)) {
Loading history...
363
                if (isset($this->uploadedImageFilePath)) {
364
                    if ( ! file_exists($this->uploadedImageFilePath)) {
365
                        mkdir($this->uploadedImageFilePath, 0777, true);
366
                    }
367
368
                    $upload = new Uploader();
369
                    $upload->setPath($this->uploadedImageFilePath);
370
371
                    if ($files = input()->files()) {
372
                        // Uploaded Image Process
373
                        if (isset($this->uploadedImageKey)) {
374
                            if (isset($files[ $this->uploadedImageKey ])) {
375
                                $upload->process($this->uploadedImageKey);
376
377
                                if ($upload->getErrors()) {
378
                                    $errors = new Unordered();
379
380
                                    foreach ($upload->getErrors() as $code => $error) {
381
                                        $errors->createList($error);
382
                                    }
383
384
                                    if (services()->has('session')) {
385
                                        session()->setFlash('danger', $errors);
386
                                    }
387
388
                                    return false;
389
                                } elseif ($row->offsetGet($this->uploadedImageKey) !== $upload->getUploadedFiles()->first()[ 'name' ]) {
390
                                    $sets[ $this->uploadedImageKey ] = $upload->getUploadedFiles()->first()[ 'name' ];
391
392
                                    if (is_file($filePath = $this->uploadedImageFilePath . $row->offsetGet($this->uploadedImageKey))) {
393
                                        unlink($filePath);
394
                                    }
395
                                }
396
                            }
397
                        } elseif (count($this->uploadedImageKeys)) {
398
                            foreach ($this->uploadedImageKeys as $uploadedImageKey) {
399
                                if (isset($files[ $uploadedImageKey ])) {
400
                                    $upload->process($uploadedImageKey);
401
402
                                    if ($upload->getErrors()) {
403
                                        $errors = new Unordered();
404
405
                                        foreach ($upload->getErrors() as $code => $error) {
406
                                            $errors->createList($error);
407
                                        }
408
409
                                        if (services()->has('session')) {
410
                                            session()->setFlash('danger', $errors);
411
                                        }
412
413
                                        return false;
414
                                    } elseif ($row->offsetGet($uploadedImageKey) !== $upload->getUploadedFiles()->first()[ 'name' ]) {
415
                                        $sets[ $uploadedImageKey ] = $upload->getUploadedFiles()->first()[ 'name' ];
416
417
                                        if (is_file($filePath = $this->uploadedImageFilePath . $row->offsetGet($uploadedImageKey))) {
418
                                            unlink($filePath);
419
                                        }
420
                                    }
421
                                }
422
                            }
423
                        }
424
425
                        // Uploaded File Process
426
                        if (isset($this->uploadedFileFilepath)) {
427
                            if (isset($files[ $this->uploadedFileKey ])) {
428
                                $upload->process($this->uploadedFileKey);
429
430
                                if ($upload->getErrors()) {
431
                                    $errors = new Unordered();
432
433
                                    foreach ($upload->getErrors() as $code => $error) {
434
                                        $errors->createList($error);
435
                                    }
436
437
                                    if (services()->has('session')) {
438
                                        session()->setFlash('danger', $errors);
439
                                    }
440
441
                                    return false;
442
                                } elseif ($row->offsetGet($this->uploadedFileKey) !== $upload->getUploadedFiles()->first()[ 'name' ]) {
443
                                    $sets[ $this->uploadedFileKey ] = $upload->getUploadedFiles()->first()[ 'name' ];
444
445
                                    if (is_file($filePath = $this->uploadedFileFilepath . $row->offsetGet($this->uploadedFileKey))) {
446
                                        unlink($filePath);
447
                                    }
448
                                }
449
                            }
450
                        } elseif (count($this->uploadedFileKeys)) {
451
                            foreach ($this->uploadedFileKeys as $uploadedFileKey) {
452
                                if (isset($files[ $uploadedFileKey ])) {
453
                                    $upload->process($uploadedFileKey);
454
455
                                    if ($upload->getErrors()) {
456
                                        $errors = new Unordered();
457
458
                                        foreach ($upload->getErrors() as $code => $error) {
459
                                            $errors->createList($error);
460
                                        }
461
462
                                        if (services()->has('session')) {
463
                                            session()->setFlash('danger', $errors);
464
                                        }
465
466
                                        return false;
467
                                    } elseif ($row->offsetGet($uploadedFileKey) !== $upload->getUploadedFiles()->first()[ 'name' ]) {
468
                                        $sets[ $uploadedFileKey ] = $upload->getUploadedFiles()->first()[ 'name' ];
469
470
                                        if (is_file($filePath = $this->uploadedFileFilepath . $row->offsetGet($uploadedFileKey))) {
471
                                            unlink($filePath);
472
                                        }
473
                                    }
474
                                }
475
                            }
476
                        }
477
                    }
478
                }
479
480
                if ($this->qb->table($this->table)->update($sets, $conditions)) {
481
482
                    if (method_exists($this, 'afterUpdate')) {
483
                        $this->afterUpdate();
484
                    }
485
486
                    $label = false;
487
                    foreach (['name', 'label', 'title', 'code'] as $labelField) {
488
                        if (isset($sets[ $labelField ])) {
489
                            if(services()->has('session')) {
490
                                session()->setFlash('success',
491
                                    language('SUCCESS_UPDATE_WITH_LABEL', [$sets[ $labelField ]]));
492
                            }
493
494
                            $label = true;
495
                            break;
496
                        }
497
                    }
498
499
                    if ($label === false) {
500
                        if(services()->has('session')) {
501
                            session()->setFlash('success', language('SUCCESS_UPDATE'));
502
                        }
503
                    }
504
505
                    return true;
506
                }
507
            }
508
        }
509
510
        $label = false;
511
        foreach (['name', 'label', 'title', 'code'] as $labelField) {
512
            if (isset($sets[ $labelField ])) {
513
                if(services()->has('session')) {
514
                    session()->setFlash('danger', language('FAILED_UPDATE_WITH_LABEL', [$sets[ $labelField ]]));
515
                }
516
517
                $label = true;
518
                break;
519
            }
520
        }
521
522
        if ($label === false) {
523
            if(services()->has('session')) {
524
                session()->setFlash('danger', language('FAILED_UPDATE'));
525
            }
526
        }
527
528
        // Sets Global $_POST Variable
529
        $_POST = $sets;
530
531
        return false;
532
    }
533
534
    // ------------------------------------------------------------------------
535
536
    /**
537
     * ModifierTrait::updateOrInsert
538
     *
539
     * @param array $sets
540
     * @param array $conditions
541
     *
542
     * @return bool
543
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException
544
     */
545
    public function updateOrInsert(array $sets, array $conditions = [])
546
    {
547
        return $this->insertOrUpdate($sets, $conditions);
548
    }
549
550
    // ------------------------------------------------------------------------
551
552
    /**
553
     * ModifierTrait::updateMany
554
     *
555
     * @param array $sets
556
     *
557
     * @return bool|array
558
     */
559
    public function updateMany(array $sets)
560
    {
561
        $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';
562
563
        if (method_exists($this, 'updateRecordSets')) {
564
            foreach ($sets as $key => $set) {
565
                $this->updateRecordSets($sets[ $key ]);
566
            }
567
        }
568
569
        if (method_exists($this, 'beforeUpdateMany')) {
570
            $this->beforeUpdateMany($sets);
571
        }
572
573
        if ($this->qb->table($this->table)->updateBatch($sets, $primaryKey)) {
574
            if (method_exists($this, 'afterUpdateMany')) {
575
                return $this->afterUpdateMany();
576
            }
577
578
            $affectedRows = $this->db->getAffectedRows();
579
580
            if (method_exists($this, 'rebuildTree')) {
581
                $this->rebuildTree();
582
            }
583
584
            return $affectedRows;
585
        }
586
587
        return false;
588
    }
589
590
    // ------------------------------------------------------------------------
591
592
    /**
593
     * ModifierTrait::delete
594
     *
595
     * @param int|Row $id
596
     *
597
     * @return bool
598
     * @throws \O2System\Spl\Exceptions\RuntimeException
599
     * @throws \Psr\Cache\InvalidArgumentException
600
     */
601
    public function delete($id)
602
    {
603
        if (method_exists($this, 'beforeDelete')) {
604
            $this->beforeDelete();
605
        }
606
607
        if ($id instanceof Row) {
608
            $row = $id;
609
        } else {
610
            $row = $this->find($id);
0 ignored issues
show
Bug introduced by
It seems like find() 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

610
            /** @scrutinizer ignore-call */ 
611
            $row = $this->find($id);
Loading history...
611
        }
612
613
        // Delete Files
614
        if ($row instanceof Row) {
615
            if (isset($this->uploadedImageFilePath)) {
616
617
                // Remove Uploaded Image
618
                if ($this->uploadedImageKey) {
619
                    if (is_file($filePath = $this->uploadedImageFilePath . $row->offsetGet($this->uploadedImageKey))) {
620
                        unlink($filePath);
621
                    }
622
                } elseif (count($this->uploadedImageKeys)) {
623
                    foreach ($this->uploadedImageKeys as $uploadedImageKey) {
624
                        if (is_file($filePath = $this->uploadedImageFilePath . $row->offsetGet($uploadedImageKey))) {
625
                            unlink($filePath);
626
                        }
627
                    }
628
                }
629
630
                // Remove Uploaded File
631
                if ($this->uploadedFileFilepath) {
632
                    if (is_file($filePath = $this->uploadedFileFilepath . $row->offsetGet($this->uploadedFileKey))) {
633
                        unlink($filePath);
634
                    }
635
                } elseif (count($this->uploadedFileKeys)) {
636
                    foreach ($this->uploadedFileKeys as $uploadedFileKey) {
637
                        if (is_file($filePath = $this->uploadedFileFilepath . $row->offsetGet($uploadedFileKey))) {
638
                            unlink($filePath);
639
                        }
640
                    }
641
                }
642
            }
643
644
            if ( ! $id instanceof Row) {
645
                if ($row->delete()) {
646
                    if (method_exists($this, 'afterDelete')) {
647
                        $this->afterDelete();
648
                    }
649
650
                    if (method_exists($this, 'rebuildTree')) {
651
                        $this->rebuildTree();
652
                    }
653
                }
654
            }
655
656
            return $row->delete();
657
        }
658
659
        return false;
660
    }
661
662
    // ------------------------------------------------------------------------
663
664
    /**
665
     * ModifierTrait::deleteBy
666
     *
667
     * @param array $conditions
668
     *
669
     * @return bool|int
670
     * @throws \O2System\Spl\Exceptions\RuntimeException
671
     * @throws \Psr\Cache\InvalidArgumentException
672
     */
673
    public function deleteBy($conditions = [])
674
    {
675
        if (count($conditions)) {
676
            if (method_exists($this, 'beforeDelete')) {
677
                $this->beforeDelete();
678
            }
679
680
            $affectedRows = 0;
681
            if ($result = $this->findWhere($conditions)) {
682
                if ($result instanceof Result) {
683
                    foreach ($result as $row) {
684
                        if ($this->delete($row)) {
685
                            $affectedRows++;
686
                        }
687
                    }
688
                } elseif ($result instanceof Row) {
689
                    if ($this->delete($result)) {
690
                        $affectedRows++;
691
                    }
692
                }
693
            }
694
695
            if ($affectedRows > 0) {
696
                if (method_exists($this, 'afterDelete')) {
697
                    $this->afterDelete();
698
                }
699
700
                if (method_exists($this, 'rebuildTree')) {
701
                    $this->rebuildTree();
702
                }
703
704
                return $affectedRows;
705
            }
706
        }
707
708
        return false;
709
    }
710
711
    // ------------------------------------------------------------------------
712
713
    /**
714
     * ModifierTrait::deleteMany
715
     *
716
     * @param array $ids
717
     *
718
     * @return bool|int
719
     * @throws \O2System\Spl\Exceptions\RuntimeException
720
     * @throws \Psr\Cache\InvalidArgumentException
721
     */
722
    public function deleteMany(array $ids)
723
    {
724
        if (method_exists($this, 'beforeDelete')) {
725
            $this->beforeDelete();
726
        }
727
728
        $affectedRows = 0;
729
        if ($result = $this->findIn($ids)) {
0 ignored issues
show
Bug introduced by
It seems like findIn() 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

729
        if ($result = $this->/** @scrutinizer ignore-call */ findIn($ids)) {
Loading history...
730
            foreach ($result as $row) {
731
                if ($this->delete($row)) {
732
                    $affectedRows++;
733
                }
734
            }
735
        }
736
737
        if ($affectedRows > 0) {
738
            if (method_exists($this, 'afterDelete')) {
739
                $this->afterDelete();
740
            }
741
742
            if (method_exists($this, 'rebuildTree')) {
743
                $this->rebuildTree();
744
            }
745
746
            return $affectedRows;
747
        }
748
749
        return false;
750
    }
751
752
    // ------------------------------------------------------------------------
753
754
    /**
755
     * ModifierTrait::deleteManyBy
756
     *
757
     * @param array $conditions
758
     *
759
     * @return bool|int
760
     * @throws \O2System\Spl\Exceptions\RuntimeException
761
     * @throws \Psr\Cache\InvalidArgumentException
762
     */
763
    public function deleteManyBy($conditions = [])
764
    {
765
        return $this->deleteBy($conditions);
766
    }
767
768
    // ------------------------------------------------------------------------
769
770
    /**
771
     * ModifierTrait::updateRecordStatus
772
     *
773
     * @param int    $id
774
     * @param string $recordStatus
775
     * @param string $method
776
     *
777
     * @return bool
778
     */
779
    private function updateRecordStatus($id, $recordStatus, $method)
780
    {
781
        $sets[ 'record_status' ] = $recordStatus;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$sets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sets = array(); before regardless.
Loading history...
782
        $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';
783
784
        if (method_exists($this, 'updateRecordSets')) {
785
            $this->updateRecordSets($sets);
786
        }
787
788
        if (method_exists($this, $beforeMethod = 'before' . ucfirst($method))) {
789
            call_user_func_array([&$this, $beforeMethod], [$sets]);
790
        }
791
792
        if ($this->qb->table($this->table)->limit(1)->update($sets, [$primaryKey => $id])) {
793
            if (method_exists($this, $afterMethod = 'after' . ucfirst($method))) {
794
                call_user_func([&$this, $beforeMethod]);
795
            }
796
797
            if (method_exists($this, 'rebuildTree')) {
798
                $this->rebuildTree();
799
            }
800
801
            return true;
802
        }
803
804
        return false;
805
    }
806
807
    // ------------------------------------------------------------------------
808
809
    /**
810
     * ModifierTrait::updateRecordStatusMany
811
     *
812
     * @param array  $ids
813
     * @param string $recordStatus
814
     * @param string $method
815
     *
816
     * @return bool|int
817
     */
818
    private function updateRecordStatusMany(array $ids, $recordStatus, $method)
819
    {
820
        if (count($ids)) {
821
            $sets[ 'record_status' ] = $recordStatus;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$sets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sets = array(); before regardless.
Loading history...
822
            $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';
823
824
            $this->qb->whereIn($primaryKey, $ids);
825
826
            if (method_exists($this, 'updateRecordSets')) {
827
                $this->updateRecordSets($sets);
828
            }
829
830
            if (method_exists($this, $beforeMethod = 'before' . ucfirst($method))) {
831
                call_user_func_array([&$this, $beforeMethod], [$sets]);
832
            }
833
834
            if ($this->qb->table($this->table)->update($sets)) {
835
                if (method_exists($this, $afterMethod = 'after' . ucfirst($method))) {
836
                    call_user_func([&$this, $beforeMethod]);
837
                }
838
839
                $affectedRows = $this->db->getAffectedRows();
840
841
                if (method_exists($this, 'rebuildTree')) {
842
                    $this->rebuildTree();
843
                }
844
845
                return $affectedRows;
846
            }
847
        }
848
849
        return false;
850
    }
851
852
    // ------------------------------------------------------------------------
853
854
    /**
855
     * ModifierTrait::updateRecordStatusBy
856
     *
857
     * @param string $recordStatus
858
     * @param string $method
859
     * @param array  $conditions
860
     *
861
     * @return bool|int
862
     */
863
    private function updateRecordStatusBy($recordStatus, $method, array $conditions)
864
    {
865
        if (count($conditions)) {
866
            $sets[ 'record_status' ] = $recordStatus;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$sets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sets = array(); before regardless.
Loading history...
867
868
            if (method_exists($this, 'updateRecordSets')) {
869
                $this->updateRecordSets($sets);
870
            }
871
872
            if (method_exists($this, $beforeMethod = 'before' . ucfirst($method))) {
873
                call_user_func_array([&$this, $beforeMethod], [$sets]);
874
            }
875
876
            if ($this->qb->table($this->table)->update($sets, $conditions)) {
877
                if (method_exists($this, $afterMethod = 'after' . ucfirst($method))) {
878
                    call_user_func([&$this, $beforeMethod]);
879
                }
880
881
                $affectedRows = $this->db->getAffectedRows();
882
883
                if (method_exists($this, 'rebuildTree')) {
884
                    $this->rebuildTree();
885
                }
886
887
                return $affectedRows;
888
            }
889
        }
890
891
        return false;
892
    }
893
894
    // ------------------------------------------------------------------------
895
896
    /**
897
     * ModifierTrait::updateRecordStatusManyBy
898
     *
899
     * @param string $recordStatus
900
     * @param string $method
901
     * @param array  $conditions
902
     *
903
     * @return bool|int
904
     */
905
    private function updateRecordStatusManyBy($recordStatus, $method, array $conditions)
906
    {
907
        if (count($conditions)) {
908
            $sets[ 'record_status' ] = $recordStatus;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$sets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sets = array(); before regardless.
Loading history...
909
910
            if (method_exists($this, 'updateRecordSets')) {
911
                $this->updateRecordSets($sets);
912
            }
913
914
            if (method_exists($this, $beforeMethod = 'before' . ucfirst($method))) {
915
                call_user_func_array([&$this, $beforeMethod], [$sets]);
916
            }
917
918
            if ($this->qb->table($this->table)->update($sets, $conditions)) {
919
                if (method_exists($this, $afterMethod = 'after' . ucfirst($method))) {
920
                    call_user_func([&$this, $beforeMethod]);
921
                }
922
923
                $affectedRows = $this->db->getAffectedRows();
924
925
                if (method_exists($this, 'rebuildTree')) {
926
                    $this->rebuildTree();
927
                }
928
929
                return $affectedRows;
930
            }
931
        }
932
933
        return false;
934
    }
935
936
    // ------------------------------------------------------------------------
937
938
    /**
939
     * ModifierTrait::publish
940
     *
941
     * @param int $id
942
     *
943
     * @return bool
944
     */
945
    public function publish($id)
946
    {
947
        return $this->updateRecordStatus($id, 'PUBLISH', 'publish');
948
    }
949
950
    // ------------------------------------------------------------------------
951
952
    /**
953
     * ModifierTrait::publishBy
954
     *
955
     * @param array $conditions
956
     *
957
     * @return bool|int
958
     */
959
    public function publishBy(array $conditions)
960
    {
961
        return $this->updateRecordStatusBy('PUBLISH', 'publishBy', $conditions);
962
    }
963
964
    // ------------------------------------------------------------------------
965
966
    /**
967
     * ModifierTrait::publishMany
968
     *
969
     * @param array $ids
970
     *
971
     * @return bool|int
972
     */
973
    public function publishMany(array $ids)
974
    {
975
        return $this->updateRecordStatusMany($ids, 'PUBLISH', 'publishMany');
976
    }
977
978
    // ------------------------------------------------------------------------
979
980
    /**
981
     * ModifierTrait::publishManyBy
982
     *
983
     * @param array $conditions
984
     *
985
     * @return bool|int
986
     */
987
    public function publishManyBy(array $conditions)
988
    {
989
        return $this->updateRecordStatusManyBy('PUBLISH', 'publishManyBy', $conditions);
990
    }
991
992
    // ------------------------------------------------------------------------
993
994
    /**
995
     * ModifierTrait::restore
996
     *
997
     * @param int $id
998
     *
999
     * @return bool
1000
     */
1001
    public function restore($id)
1002
    {
1003
        return $this->updateRecordStatus($id, 'PUBLISH', 'restore');
1004
    }
1005
1006
    // ------------------------------------------------------------------------
1007
1008
    /**
1009
     * ModifierTrait::restoreBy
1010
     *
1011
     * @param array $conditions
1012
     *
1013
     * @return bool
1014
     */
1015
    public function restoreBy(array $conditions)
1016
    {
1017
        return $this->updateRecordStatusBy('PUBLISH', 'restoreBy', $conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->updateReco...estoreBy', $conditions) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
1018
    }
1019
1020
    // ------------------------------------------------------------------------
1021
1022
    /**
1023
     * ModifierTrait::restoreMany
1024
     *
1025
     * @param array $ids
1026
     *
1027
     * @return bool|int
1028
     */
1029
    public function restoreMany(array $ids)
1030
    {
1031
        return $this->updateRecordStatusMany($ids, 'PUBLISH', 'restoreMany');
1032
    }
1033
1034
    // ------------------------------------------------------------------------
1035
1036
    /**
1037
     * ModifierTrait::restoreManyBy
1038
     *
1039
     * @param array $conditions
1040
     *
1041
     * @return bool|int
1042
     */
1043
    public function restoreManyBy(array $conditions)
1044
    {
1045
        return $this->updateRecordStatusManyBy('PUBLISH', 'restoreManyBy', $conditions);
1046
    }
1047
1048
    // ------------------------------------------------------------------------
1049
1050
    /**
1051
     * ModifierTrait::unpublish
1052
     *
1053
     * @param int $id
1054
     *
1055
     * @return bool
1056
     */
1057
    public function unpublish($id)
1058
    {
1059
        return $this->updateRecordStatus($id, 'UNPUBLISH', 'unpublish');
1060
    }
1061
1062
    // ------------------------------------------------------------------------
1063
1064
    /**
1065
     * ModifierTrait::unpublishBy
1066
     *
1067
     * @param array $conditions
1068
     *
1069
     * @return bool
1070
     */
1071
    public function unpublishBy(array $conditions)
1072
    {
1073
        return $this->updateRecordStatusBy('UNPUBLISH', 'unpublishBy', $conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->updateReco...ublishBy', $conditions) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
1074
    }
1075
1076
    // ------------------------------------------------------------------------
1077
1078
    /**
1079
     * ModifierTrait::unpublishMany
1080
     *
1081
     * @param array $ids
1082
     *
1083
     * @return bool|int
1084
     */
1085
    public function unpublishMany(array $ids)
1086
    {
1087
        return $this->updateRecordStatusMany($ids, 'UNPUBLISH', 'unpublishMany');
1088
    }
1089
1090
    // ------------------------------------------------------------------------
1091
1092
    /**
1093
     * ModifierTrait::unpublishManyBy
1094
     *
1095
     * @param array $conditions
1096
     *
1097
     * @return bool|int
1098
     */
1099
    public function unpublishManyBy(array $conditions)
1100
    {
1101
        return $this->updateRecordStatusManyBy('UNPUBLISH', 'unpublishManyBy', $conditions);
1102
    }
1103
1104
    // ------------------------------------------------------------------------
1105
1106
    /**
1107
     * ModifierTrait::softDelete
1108
     *
1109
     * @param int $id
1110
     *
1111
     * @return bool
1112
     */
1113
    public function softDelete($id)
1114
    {
1115
        return $this->updateRecordStatus($id, 'DELETED', 'softDelete');
1116
    }
1117
1118
    // ------------------------------------------------------------------------
1119
1120
    /**
1121
     * ModifierTrait::softDeleteBy
1122
     *
1123
     * @param array $conditions
1124
     *
1125
     * @return bool
1126
     */
1127
    public function softDeleteBy(array $conditions)
1128
    {
1129
        return $this->updateRecordStatusBy('DELETED', 'softDeleteBy', $conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->updateReco...DeleteBy', $conditions) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
1130
    }
1131
1132
    // ------------------------------------------------------------------------
1133
1134
    /**
1135
     * ModifierTrait::softDeleteMany
1136
     *
1137
     * @param array $ids
1138
     *
1139
     * @return bool|int
1140
     */
1141
    public function softDeleteMany(array $ids)
1142
    {
1143
        return $this->updateRecordStatusMany($ids, 'DELETED', 'softDeleteMany');
1144
    }
1145
1146
    // ------------------------------------------------------------------------
1147
1148
    /**
1149
     * ModifierTrait::softDeleteManyBy
1150
     *
1151
     * @param array $conditions
1152
     *
1153
     * @return bool|int
1154
     */
1155
    public function softDeleteManyBy(array $conditions)
1156
    {
1157
        return $this->updateRecordStatusManyBy('DELETED', 'softDeleteManyBy', $conditions);
1158
    }
1159
1160
    // ------------------------------------------------------------------------
1161
1162
    /**
1163
     * ModifierTrait::archive
1164
     *
1165
     * @param int $id
1166
     *
1167
     * @return bool
1168
     */
1169
    public function archive($id)
1170
    {
1171
        return $this->updateRecordStatus($id, 'ARCHIVED', 'archive');
1172
    }
1173
1174
    // ------------------------------------------------------------------------
1175
1176
    /**
1177
     * ModifierTrait::archiveBy
1178
     *
1179
     * @param array $conditions
1180
     *
1181
     * @return bool
1182
     */
1183
    public function archiveBy(array $conditions)
1184
    {
1185
        return $this->updateRecordStatusBy('ARCHIVED', 'archiveBy', $conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->updateReco...rchiveBy', $conditions) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
1186
    }
1187
1188
    // ------------------------------------------------------------------------
1189
1190
    /**
1191
     * ModifierTrait::archiveMany
1192
     *
1193
     * @param array $ids
1194
     *
1195
     * @return bool|int
1196
     */
1197
    public function archiveMany(array $ids)
1198
    {
1199
        return $this->updateRecordStatusMany($ids, 'ARCHIVED', 'archiveMany');
1200
    }
1201
1202
    // ------------------------------------------------------------------------
1203
1204
    /**
1205
     * ModifierTrait::archiveManyBy
1206
     *
1207
     * @param array $conditions
1208
     *
1209
     * @return bool|int
1210
     */
1211
    public function archiveManyBy(array $conditions)
1212
    {
1213
        return $this->updateRecordStatusManyBy('ARCHIVED', 'archiveManyBy', $conditions);
1214
    }
1215
1216
    // ------------------------------------------------------------------------
1217
1218
    /**
1219
     * ModifierTrait::lock
1220
     *
1221
     * @param int $id
1222
     *
1223
     * @return bool
1224
     */
1225
    public function lock($id)
1226
    {
1227
        return $this->updateRecordStatus($id, 'LOCKED', 'lock');
1228
    }
1229
1230
    // ------------------------------------------------------------------------
1231
1232
    /**
1233
     * ModifierTrait::lockBy
1234
     *
1235
     * @param array $conditions
1236
     *
1237
     * @return bool
1238
     */
1239
    public function lockBy(array $conditions)
1240
    {
1241
        return $this->updateRecordStatusBy('LOCKED', 'lockBy', $conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->updateReco... 'lockBy', $conditions) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
1242
    }
1243
1244
    // ------------------------------------------------------------------------
1245
1246
    /**
1247
     * ModifierTrait::lockMany
1248
     *
1249
     * @param array $ids
1250
     *
1251
     * @return bool|int
1252
     */
1253
    public function lockMany(array $ids)
1254
    {
1255
        return $this->updateRecordStatusMany($ids, 'LOCKED', 'lockMany');
1256
    }
1257
1258
    // ------------------------------------------------------------------------
1259
1260
    /**
1261
     * ModifierTrait::lockManyBy
1262
     *
1263
     * @param array $conditions
1264
     *
1265
     * @return bool|int
1266
     */
1267
    public function lockManyBy(array $conditions)
1268
    {
1269
        return $this->updateRecordStatusManyBy('LOCKED', 'lockManyBy', $conditions);
1270
    }
1271
}