Completed
Push — master ( 0998e9...c3faac )
by wen
12:12
created

Repository::getGlobalScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace Sco\Admin\Repositories;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Illuminate\Foundation\Application;
9
use InvalidArgumentException;
10
use Sco\Admin\Contracts\RepositoryInterface;
11
use Sco\Admin\Contracts\WithModel;
12
13
class Repository implements RepositoryInterface
14
{
15
    protected $app;
16
17
    protected $model;
18
19
    /**
20
     * @var Model
21
     */
22
    protected $class;
23
24
    protected $with = [];
25
26
    public function __construct(Application $app)
27
    {
28
        $this->app = $app;
29
    }
30
31
    public function getModel()
32
    {
33
        return $this->model;
34
    }
35
36
    public function setModel(Model $model)
37
    {
38
        $this->model = $model;
39
        $this->class = get_class($model);
0 ignored issues
show
Documentation Bug introduced by
It seems like get_class($model) of type string is incompatible with the declared type object<Illuminate\Database\Eloquent\Model> of property $class.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return string[]
46
     */
47
    public function getWith()
48
    {
49
        return $this->with;
50
    }
51
52
    public function with($relations)
53
    {
54
        $this->with = array_flatten(func_get_args());
55
56
        return $this;
57
    }
58
59
    public function getClass()
60
    {
61
        return $this->class;
62
    }
63
64
    public function setClass($class)
65
    {
66
        if (!class_exists($class)) {
67
            throw new InvalidArgumentException("Model class {$class} not found.");
68
        }
69
70
        $this->class = $class;
71
        $this->setModel(
72
            new $class()
73
        );
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getQuery()
82
    {
83
        $model = $this->getModel();
84
85
        return $model->query()->with($this->getWith());
86
    }
87
88
    public function find($id)
89
    {
90
        $query = $this->getQuery();
91
        if ($this->isRestorable()) {
92
            $query->withTrashed();
93
        }
94
95
        return $query->find($id);
96
    }
97
98
    public function findOrFail($id)
99
    {
100
        $query = $this->getQuery();
101
        if ($this->isRestorable()) {
102
            $query->withTrashed();
103
        }
104
105
        return $query->findOrFail($id);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function findOnlyTrashed($id)
112
    {
113
        return $this->getQuery()->onlyTrashed()->findOrFail($id);
114
    }
115
116
    /*public function store()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
    {
118
    }
119
120
    public function update()
121
    {
122
    }*/
123
124
125
    public function forceDelete($id)
126
    {
127
        return $this->findOnlyTrashed($id)->forceDelete();
128
    }
129
130
    public function restore($id)
131
    {
132
        return $this->findOnlyTrashed($id)->restore();
133
    }
134
135
136
    public function isRestorable()
137
    {
138
        return in_array(SoftDeletes::class, class_uses_recursive($this->getClass()));
139
    }
140
}
141