Passed
Push — master ( 284e72...816a06 )
by Iman
04:12
created

DataRemover::doDeleteWithHook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 1
dl 0
loc 5
c 0
b 0
f 0
cc 1
rs 9.4285
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule;
4
5
use Illuminate\Support\Facades\Schema;
6
7
class DataRemover
8
{
9
    private $ctrl;
10
11
    /**
12
     * DataRemover constructor.
13
     *
14
     * @param $ctrl
15
     */
16
    public function __construct($ctrl)
17
    {
18
        $this->ctrl =  $ctrl;
19
    }
20
21
    /**
22
     * @param $idsArray
23
     */
24
    public function deleteIds(array $idsArray)
25
    {
26
        $query = $this->ctrl->table()->whereIn($this->ctrl->primaryKey, $idsArray);
27
        if (Schema::hasColumn($this->ctrl->table, 'deleted_at')) {
28
            $query->update(['deleted_at' => YmdHis()]);
29
        } else {
30
            $query->delete();
31
        }
32
    }
33
34
    /**
35
     * @param $idsArray
36
     */
37
    public function doDeleteWithHook(array $idsArray)
38
    {
39
        $idsArray = $this->ctrl->hookBeforeDelete($idsArray);
40
        $this->deleteIds($idsArray);
41
        $this->ctrl->hookAfterDelete($idsArray);
42
    }
43
}