Passed
Pull Request — master (#17)
by Orkhan
03:43
created

DeletesEntity::restore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
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(int $modelId)
13
 * @method void invalidateCache()
14
 */
15
trait DeletesEntity
16
{
17
    /**
18
     * Finds a model with ID and deletes it.
19
     *
20
     * @param int|string $modelId
21
     *
22
     * @return bool|mixed|null
23
     * @throws \Exception
24
     */
25
    public function findAndDelete($modelId)
26
    {
27
        $model = $this->find($modelId);
0 ignored issues
show
Bug introduced by
It seems like $modelId can also be of type string; however, parameter $modelId of Orkhanahmadov\EloquentRe...s\DeletesEntity::find() does only seem to accept integer, 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

27
        $model = $this->find(/** @scrutinizer ignore-type */ $modelId);
Loading history...
28
29
        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

29
        return $this->delete(/** @scrutinizer ignore-type */ $model);
Loading history...
30
    }
31
32
    /**
33
     * Deletes a model.
34
     *
35
     * @param Model $model
36
     *
37
     * @return bool|mixed|null
38
     * @throws \Exception
39
     */
40
    public function delete($model)
41
    {
42
        if ($this instanceof Cacheable) {
43
            $this->invalidateCache($model);
0 ignored issues
show
Unused Code introduced by
The call to Orkhanahmadov\EloquentRe...tity::invalidateCache() has too many arguments starting with $model. ( Ignorable by Annotation )

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

43
            $this->/** @scrutinizer ignore-call */ 
44
                   invalidateCache($model);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
44
        }
45
46
        return $model->delete();
47
    }
48
49
    /**
50
     * Finds a soft deleted model with given ID and restores it.
51
     *
52
     * @param int|string $modelId
53
     *
54
     * @return bool|null
55
     */
56
    public function findAndRestore($modelId)
57
    {
58
        $model = $this->findFromTrashed($modelId);
59
60
        return $this->restore($model);
61
    }
62
63
    /**
64
     * Finds a soft deleted model with given ID.
65
     *
66
     * @param int|string $modelId
67
     *
68
     * @return Builder|Builder[]|Collection|Model|null
69
     */
70
    public function findFromTrashed($modelId)
71
    {
72
        if (! method_exists($this->entity, 'restore')) {
73
            throw new \BadMethodCallException('Model is not using "soft delete" feature.');
74
        }
75
76
        $model = $this->model->onlyTrashed()->find($modelId);
77
78
        if (! $model) {
79
            $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

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