Completed
Push — master ( b271b8...ef0682 )
by wen
03:13
created

Repository::restore()   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 1
1
<?php
2
3
4
namespace Sco\Admin\Repositories;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Sco\Admin\Contracts\Repository as RepositoryContract;
9
use Sco\Admin\Exceptions\RepositoryException;
10
11
12
/**
13
 * @method static \Illuminate\Database\Eloquent\Model getKeyName()
14
 */
15
class Repository implements RepositoryContract
16
{
17
    protected $model;
18
19
    protected $class;
20
21
    public function getModel()
22
    {
23
        return $this->model;
24
    }
25
26
    public function setModel(Model $model)
27
    {
28
        $this->model = $model;
29
    }
30
31
    public function getClass()
32
    {
33
        return $this->class;
34
    }
35
36
    public function setClass($class)
37
    {
38
        if (!class_exists($class)) {
39
            throw new RepositoryException("Class {$class} not found.");
40
        }
41
42
        $this->class = $class;
43
        $this->setModel(
44
            new $class()
45
        );
46
    }
47
48
    public function forceDelete($id)
49
    {
50
        return $this->getModel()->onlyTrashed()->findOrFail($id)->forceDelete();
51
    }
52
53
    public function restore($id)
54
    {
55
        return $this->getModel()->onlyTrashed()->findOrFail($id)->restore();
56
    }
57
58
59
    public function isRestorable()
60
    {
61
        return in_array(SoftDeletes::class, class_uses_recursive($this->getClass()));
62
    }
63
64
    /**
65
     * Handle dynamic method calls into the model.
66
     *
67
     * @param  string $method
68
     * @param  array  $parameters
69
     *
70
     * @return mixed
71
     */
72
    public function __call($method, $parameters)
73
    {
74
        return $this->getModel()->$method(...$parameters);
75
    }
76
77
    /**
78
     * Handle dynamic static method calls into the method.
79
     *
80
     * @param  string $method
81
     * @param  array  $parameters
82
     *
83
     * @return mixed
84
     */
85
    public static function __callStatic($method, $parameters)
86
    {
87
        return (new static)->$method(...$parameters);
88
    }
89
}
90