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::restoreBy()   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 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the O2System PHP 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\Reactor\Models\Sql\Traits;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class TraitModifier
20
 *
21
 * @package O2System\Reactor\Models\Sql\Traits
22
 */
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';
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...
100
        $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...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table seems to never exist and therefore isset should always be false.
Loading history...
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);
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

137
                    /** @scrutinizer ignore-call */ 
138
                    $set[ 'record_ordering' ] = $this->getRecordOrdering($table);
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table seems to never exist and therefore isset should always be false.
Loading history...
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';
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...
176
        $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...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $table is correct as it would always require null to be passed?
Loading history...
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';
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...
239
        $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...
240
241
        if (method_exists($this, 'updateRecordSets')) {
242
            $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

242
            $this->/** @scrutinizer ignore-call */ 
243
                   setRecordStatus('TRASH');
Loading history...
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)
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

305
    public function delete($id, /** @scrutinizer ignore-unused */ $force = false, $table = 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...
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';
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...
326
        $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...
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)
379
    {
380
        $table = isset($table) ? $table : $this->table;
381
        $primaryKey = isset($this->primaryKey) ? $this->primaryKey : 'id';
382
383
        $sets = [];
384
        $where = [];
385
386
        if (empty($this->primaryKeys)) {
387
            $where[ $primaryKey ] = $id;
388
            $sets[ $primaryKey ] = $id;
389
        } elseif (is_array($id)) {
390
            foreach ($this->primaryKeys as $primaryKey) {
391
                $where[ $primaryKey ] = $sets[ $primaryKey ];
392
                $sets[ $primaryKey ] = $id[ $primaryKey ];
393
            }
394
        } else {
395
            foreach ($this->primaryKeys as $primaryKey) {
396
                $where[ $primaryKey ] = $sets[ $primaryKey ];
397
            }
398
399
            $sets[ reset($this->primaryKeys) ] = $id;
400
        }
401
402
        // Reset Primary Keys
403
        $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...
404
        $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...
405
406
        if (method_exists($this, 'updateRecordSets')) {
407
            $this->setRecordStatus('PUBLISH');
408
            $this->updateRecordSets($sets);
409
        }
410
411
        if (method_exists($this, 'beforePublish')) {
412
            $this->beforePublish($sets);
413
        }
414
415
        if ($this->qb->table($table)->update($sets, $where)) {
416
            if (method_exists($this, 'afterPublish')) {
417
                return $this->afterPublish();
418
            }
419
420
            return true;
421
        }
422
423
        return false;
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)
485
    {
486
        return $this->publishManyBy($ids, $where, $table);
487
    }
488
}