Completed
Push — master ( 46aff2...b271b8 )
by wen
02:45
created

Repository::forceDelete()   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
54
    public function isRestorable()
55
    {
56
        return in_array(SoftDeletes::class, class_uses_recursive($this->getClass()));
57
    }
58
59
    /**
60
     * Handle dynamic method calls into the model.
61
     *
62
     * @param  string $method
63
     * @param  array  $parameters
64
     *
65
     * @return mixed
66
     */
67
    public function __call($method, $parameters)
68
    {
69
        return $this->getModel()->$method(...$parameters);
70
    }
71
72
    /**
73
     * Handle dynamic static method calls into the method.
74
     *
75
     * @param  string $method
76
     * @param  array  $parameters
77
     *
78
     * @return mixed
79
     */
80
    public static function __callStatic($method, $parameters)
81
    {
82
        return (new static)->$method(...$parameters);
83
    }
84
}
85