Completed
Push — master ( 7e02e6...ea0e93 )
by wen
03:01
created

Repository::getWith()   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 Sco\Admin\Contracts\RepositoryInterface;
10
use Sco\Admin\Exceptions\RepositoryException;
11
12
/**
13
 * @method static \Illuminate\Database\Eloquent\Model getKeyName()
14
 */
15
class Repository implements RepositoryInterface
16
{
17
    protected $app;
18
19
    protected $model;
20
21
    protected $class;
22
23
    protected $with = [];
24
25
    public function __construct(Application $app)
26
    {
27
        $this->app = $app;
28
    }
29
30
    public function getModel()
31
    {
32
        return $this->model;
33
    }
34
35
    public function setModel(Model $model)
36
    {
37
        $this->model = $model;
38
        $this->class = get_class($model);
39
40
        return $this;
41
    }
42
43
    /**
44
     * @return string[]
45
     */
46
    public function getWith()
47
    {
48
        return $this->with;
49
    }
50
51
    public function with($relations)
52
    {
53
        $this->with = array_flatten(func_get_args());
54
55
        return $this;
56
    }
57
58
    public function getClass()
59
    {
60
        return $this->class;
61
    }
62
63
    public function setClass($class)
64
    {
65
        if (!class_exists($class)) {
66
            throw new RepositoryException("Model class {$class} not found.");
67
        }
68
69
        $this->class = $class;
70
        $this->setModel(
71
            new $class()
72
        );
73
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getQuery()
81
    {
82
        return $this->getModel()
83
            ->query()
84
            ->with($this->getWith());
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function findOnlyTrashed($id)
91
    {
92
        return $this->getQuery()->onlyTrashed()->findOrFail($id);
93
    }
94
95
    public function store()
96
    {
97
    }
98
99
    public function update()
100
    {
101
    }
102
103
104
    public function forceDelete($id)
105
    {
106
        return $this->findOnlyTrashed($id)->forceDelete();
107
    }
108
109
    public function restore($id)
110
    {
111
        return $this->findOnlyTrashed($id)->restore();
112
    }
113
114
115
    public function isRestorable()
116
    {
117
        return in_array(SoftDeletes::class, class_uses_recursive($this->getClass()));
118
    }
119
}
120