Completed
Push — master ( 4405b1...07981c )
by wen
03:14
created

Repository::__construct()   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
c 0
b 0
f 0
rs 10
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 Illuminate\Foundation\Application;
9
use Sco\Admin\Contracts\RepositoryInterface;
10
use Sco\Admin\Exceptions\RepositoryException;
11
12
/**
13
 * @method static \Illuminate\Database\Eloquent\Model getKeyName()
14
 */
15
class Repository implements RepositoryInterface
16
{
17
    protected $app;
18
19
    protected $model;
20
21
    protected $class;
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
    public function getClass()
42
    {
43
        return $this->class;
44
    }
45
46
    public function setClass($class)
47
    {
48
        if (!class_exists($class)) {
49
            throw new RepositoryException("Class {$class} not found.");
50
        }
51
52
        $this->class = $class;
53
        $this->setModel(
54
            new $class()
55
        );
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param $id
62
     *
63
     * @return Model
64
     */
65
    public function findOnlyTrashed($id)
66
    {
67
        return $this->getModel()->onlyTrashed()->findOrFail($id);
68
    }
69
70
71
    public function store()
72
    {
73
    }
74
75
    public function update()
76
    {
77
    }
78
79
80
81
    public function forceDelete($id)
82
    {
83
        return $this->findOnlyTrashed($id)->forceDelete();
84
    }
85
86
    public function restore($id)
87
    {
88
        return $this->findOnlyTrashed($id)->restore();
89
    }
90
91
92
    public function isRestorable()
93
    {
94
        return in_array(SoftDeletes::class, class_uses_recursive($this->getClass()));
95
    }
96
97
    /**
98
     * Handle dynamic method calls into the model.
99
     *
100
     * @param  string $method
101
     * @param  array  $parameters
102
     *
103
     * @return mixed
104
     */
105
    public function __call($method, $parameters)
106
    {
107
        return $this->getModel()->$method(...$parameters);
108
    }
109
110
    /**
111
     * Handle dynamic static method calls into the method.
112
     *
113
     * @param  string $method
114
     * @param  array  $parameters
115
     *
116
     * @return mixed
117
     */
118
    public static function __callStatic($method, $parameters)
119
    {
120
        return (new static)->$method(...$parameters);
0 ignored issues
show
Bug introduced by
The call to Repository::__construct() misses a required argument $app.

This check looks for function calls that miss required arguments.

Loading history...
121
    }
122
}
123