SoftDeleteTrait::applySoftDeletedConditions()   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
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Jidaikobo\Kontiki\Models\Traits;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Query\Builder;
7
8
trait SoftDeleteTrait
9
{
10
    protected string $softDeleteField = 'deleted_at';
11
12
    public function applySoftDeletedConditions(Builder $query): Builder
13
    {
14
        return $query->whereNotNull($this->softDeleteField);
15
    }
16
17
    public function applyNotSoftDeletedConditions(Builder $query): Builder
18
    {
19
        return $query->whereNull($this->softDeleteField);
20
    }
21
22
    public function trash($id): bool
23
    {
24
        $currentTime = Carbon::now('UTC')->format('Y-m-d H:i:s');
25
        $data = $this->getById($id);
0 ignored issues
show
Bug introduced by
It seems like getById() 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

25
        /** @scrutinizer ignore-call */ 
26
        $data = $this->getById($id);
Loading history...
26
        if (!$data) {
27
            return false;
28
        }
29
        return $this->update($id, [$this->softDeleteField => $currentTime], true);
0 ignored issues
show
Bug introduced by
It seems like update() 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

29
        return $this->/** @scrutinizer ignore-call */ update($id, [$this->softDeleteField => $currentTime], true);
Loading history...
30
    }
31
32
    public function restore($id): bool
33
    {
34
        $data = $this->getById($id);
35
        if (!$data) {
36
            return false;
37
        }
38
        return $this->update($id, [$this->softDeleteField => null], true);
39
    }
40
}
41