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

Repository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A setModel() 0 4 1
A getClass() 0 4 1
A setClass() 0 11 2
A forceDelete() 0 4 1
A isRestorable() 0 4 1
A __call() 0 4 1
A __callStatic() 0 4 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