DeletesEntity::findAndDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Orkhanahmadov\EloquentRepository\Repository\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Cacheable;
9
10
/**
11
 * @property-read Builder|Model $model
12
 * @method Builder|Model find($modelId)
13
 * @method void invalidateCache($model)
14
 * @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
15
 */
16
trait DeletesEntity
17
{
18
    /**
19
     * Finds a model with ID and deletes it.
20
     *
21
     * @param int|string $modelId
22
     *
23
     * @return bool|mixed|null
24
     * @throws \Exception
25
     */
26
    public function findAndDelete($modelId)
27
    {
28
        $model = $this->find($modelId);
29
30
        return $this->delete($model);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $model of Orkhanahmadov\EloquentRe...DeletesEntity::delete() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

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

30
        return $this->delete(/** @scrutinizer ignore-type */ $model);
Loading history...
31
    }
32
33
    /**
34
     * Deletes a model.
35
     *
36
     * @param Model $model
37
     *
38
     * @return bool|mixed|null
39
     * @throws \Exception
40
     */
41
    public function delete($model)
42
    {
43
        if ($this instanceof Cacheable) {
44
            $this->invalidateCache($model);
45
        }
46
47
        return $model->delete();
48
    }
49
50
    /**
51
     * Finds a soft deleted model with given ID and restores it.
52
     *
53
     * @param int|string $modelId
54
     *
55
     * @return bool|null
56
     */
57
    public function findAndRestore($modelId)
58
    {
59
        $model = $this->findFromTrashed($modelId);
60
61
        return $this->restore($model);
62
    }
63
64
    /**
65
     * Finds a soft deleted model with given ID.
66
     *
67
     * @param int|string $modelId
68
     *
69
     * @return Builder|Builder[]|Collection|Model|null
70
     */
71
    public function findFromTrashed($modelId)
72
    {
73
        if (! method_exists($this->entity, 'restore')) {
74
            throw new \BadMethodCallException('Model is not using "soft delete" feature.');
75
        }
76
77
        $model = $this->model->onlyTrashed()->find($modelId);
78
79
        if (! $model) {
80
            $this->throwModelNotFoundException($modelId);
0 ignored issues
show
Bug introduced by
It seems like throwModelNotFoundException() 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

80
            $this->/** @scrutinizer ignore-call */ 
81
                   throwModelNotFoundException($modelId);
Loading history...
81
        }
82
83
        return $model;
84
    }
85
86
    /**
87
     * Restores soft deleted model.
88
     *
89
     * @param Model $model
90
     *
91
     * @return bool|null
92
     */
93
    public function restore($model)
94
    {
95
        if (! method_exists($this->entity, 'restore')) {
96
            throw new \BadMethodCallException('Model is not using "soft delete" feature.');
97
        }
98
99
        return $model->restore();
100
    }
101
}
102