Completed
Push — master ( fda7e1...904ceb )
by wen
03:37
created

Repository::__call()   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 2
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
class Repository implements RepositoryContract
12
{
13
    protected $model;
14
15
    protected $class;
16
17
    public function getModel()
18
    {
19
        return $this->model;
20
    }
21
22
    public function setModel(Model $model)
23
    {
24
        $this->model = $model;
25
    }
26
27
    public function getClass()
28
    {
29
        return $this->class;
30
    }
31
32
    public function setClass($class)
33
    {
34
        if (!class_exists($class)) {
35
            throw new RepositoryException("Class {$class} not found.");
36
        }
37
38
        $this->class = $class;
39
        $this->setModel(
40
            new $class()
41
        );
42
    }
43
44
    public function isRestorable()
45
    {
46
        return in_array(SoftDeletes::class, class_uses_recursive($this->getClass()));
47
    }
48
49
    /**
50
     * Handle dynamic method calls into the model.
51
     *
52
     * @param  string  $method
53
     * @param  array  $parameters
54
     * @return mixed
55
     */
56
    public function __call($method, $parameters)
57
    {
58
        return $this->getModel()->$method(...$parameters);
59
    }
60
61
    /**
62
     * Handle dynamic static method calls into the method.
63
     *
64
     * @param  string  $method
65
     * @param  array  $parameters
66
     * @return mixed
67
     */
68
    public static function __callStatic($method, $parameters)
69
    {
70
        return (new static)->$method(...$parameters);
71
    }
72
}
73