AbstractRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 141
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findOrFail() 0 4 1
A find() 0 4 1
A getModel() 0 4 1
A newInstance() 0 4 1
A create() 0 4 1
A forceCreate() 0 4 1
A destroy() 0 4 1
A setWorkingModel() 0 4 1
A clearWorkingModel() 0 4 1
A getWorkingModel() 0 4 2
1
<?php
2
3
namespace App\Core;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Contracts\Container\Container;
9
10
abstract class AbstractRepository
11
{
12
    /**
13
     * @var Container
14
     */
15
    protected $app;
16
17
    /**
18
     * @var Model
19
     */
20
    protected $model;
21
22
    /**
23
     * @var Builder
24
     */
25
    protected $workingModel;
26
27
    /**
28
     * AbstractRepository constructor.
29
     *
30
     * @param Container $app
31
     * @param Model $model
32
     */
33
    public function __construct(Container $app, Model $model)
34
    {
35
        $this->app = $app;
36
        $this->model = $model;
37
    }
38
39
    /**
40
     * Find a model by its primary key. If none found, throw an exception
41
     *
42
     * @param mixed $id
43
     * @param array $columns
44
     *
45
     * @return Collection|Model
46
     */
47
    public function findOrFail($id, array $columns = ['*'])
48
    {
49
        return $this->getWorkingModel()->findOrFail($id, $columns);
50
    }
51
52
    /**
53
     * Find a model by its primary key
54
     *
55
     * @param mixed $id
56
     * @param array $columns
57
     *
58
     * @return Collection|Model
59
     */
60
    public function find($id, array $columns = ['*'])
61
    {
62
        return $this->getWorkingModel()->find($id, $columns);
63
    }
64
65
    /**
66
     * Get model
67
     *
68
     * @return Model
69
     */
70
    protected function getModel()
71
    {
72
        return $this->model;
73
    }
74
75
    /**
76
     * Create a new instance of model
77
     *
78
     * @param array $attributes
79
     *
80
     * @return Model
81
     */
82
    public function newInstance(array $attributes = [])
83
    {
84
        return $this->getModel()->newInstance($attributes);
85
    }
86
87
    /**
88
     * Save a new model and return the instance.
89
     *
90
     * @param array $attributes
91
     *
92
     * @return Model
93
     */
94
    public function create(array $attributes = [])
95
    {
96
        return $this->getModel()->create($attributes);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Database\Eloquent\Model. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
    }
98
99
    /**
100
     * Save a new model and return the instance. Allow mass-assignment.
101
     *
102
     * @param array $attributes
103
     *
104
     * @return Model
105
     */
106
    public function forceCreate(array $attributes = [])
107
    {
108
        return $this->getModel()->forceCreate($attributes);
109
    }
110
111
    /**
112
     * Destroy the models for the given primary keys.
113
     *
114
     * @param array|int $ids
115
     *
116
     * @return bool
117
     */
118
    public function destroy($ids)
119
    {
120
        return $this->getWorkingModel()->destroy($ids);
121
    }
122
123
    /**
124
     * Sets working model
125
     *
126
     * @param Builder $model
127
     */
128
    public function setWorkingModel(Builder $model)
129
    {
130
        $this->workingModel = $model;
131
    }
132
133
    /**
134
     * Clears working model
135
     */
136
    public function clearWorkingModel()
137
    {
138
        $this->workingModel = null;
139
    }
140
141
    /**
142
     * Get working model (if none set it will return model)
143
     *
144
     * @return Model
145
     */
146
    protected function getWorkingModel()
147
    {
148
        return $this->workingModel ?: $this->model;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->workingModel ?: $this->model; of type Illuminate\Database\Quer...Database\Eloquent\Model adds the type Illuminate\Database\Query\Builder to the return on line 148 which is incompatible with the return type documented by App\Core\AbstractRepository::getWorkingModel of type Illuminate\Database\Eloquent\Model.
Loading history...
149
    }
150
}
151