Completed
Push — master ( 956296...cca79d )
by wen
03:03
created

Repository::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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