Test Setup Failed
Push — master ( 7d9f37...6a8dff )
by Adrian
03:16
created

Repository::findOrFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Ablunier\Laravel\Database\Repository\Eloquent;
4
5
use Ablunier\Laravel\Database\Contracts\Repository\Criteria;
6
use Ablunier\Laravel\Database\Contracts\Repository\CriteriaPerformer;
7
use Ablunier\Laravel\Database\Contracts\Repository\Repository as RepositoryContract;
8
use Ablunier\Laravel\Database\Repository\Eloquent\Criteria\WithCriteria;
9
use Ablunier\Laravel\Database\Repository\Exceptions\RepositoryException;
10
use Illuminate\Database\Eloquent\Model as EloquentModel;
11
use Illuminate\Support\Collection;
12
13
class Repository implements RepositoryContract, CriteriaPerformer
14
{
15
    /**
16
     * @var EloquentModel
17
     */
18
    protected $model;
19
20
    /**
21
     * @var Collection
22
     */
23
    protected $criteria;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $skipCriteria = false;
29
30
    public function __construct(EloquentModel $model)
31 17
    {
32
        $this->model = $model;
33 17
        $this->criteria = new Collection();
34 17
    }
35 17
36
    /**
37
     * @return EloquentModel
38
     */
39
    public function getModel()
40 1
    {
41
        return $this->model;
42 1
    }
43
44
    /**
45
     * @param array $with
46
     *
47
     * @return Collection
48
     */
49 1
    public function all(array $with = [])
50
    {
51 1
        $this->addWithCriteria($with);
52 1
        $this->applyCriteria();
53
54 1
        $result = $this->model->get();
55
56 1
        $this->refresh();
57
58 1
        return $result;
59
    }
60
61
    /**
62
     * @param int $perPage
63
     *
64
     * @return mixed
65 1
     */
66
    public function paginate($perPage = 15, array $with = [])
67 1
    {
68 1
        $this->addWithCriteria($with);
69
        $this->applyCriteria();
70 1
71
        $result = $this->model->paginate($perPage);
72 1
73
        $this->refresh();
74 1
75
        return $result;
76
    }
77
78
    /**
79
     * @param array $data
80
     *
81 7
     * @return EloquentModel
82
     */
83 7
    public function create(array $data)
84
    {
85 7
        $result = $this->model->create($data);
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...
86
87 7
        $this->refresh();
88
89
        return $result;
90
    }
91
92
    /**
93
     * @param array $data
94
     * @param $id
95
     * @param string $field
96 2
     *
97
     * @return EloquentModel
98 2
     */
99
    public function update(array $data, $id, $field = 'id')
100 2
    {
101
        $result = $this->model->where($field, '=', $id)->update($data);
102 2
103 1
        $this->refresh();
104
105
        if (!$result) {
106 1
            throw new RepositoryException('There was an error updating the model');
107
        }
108 1
109
        $model = $this->find($id);
110
111
        return $model;
112
    }
113
114
    /**
115 3
     * @param $id
116
     *
117 3
     * @return int
118
     */
119 3
    public function delete($id)
120
    {
121 3
        $result = $this->model->destroy($id);
122
123
        $this->refresh();
124
125
        return $result;
126
    }
127
128 3
    /**
129
     * @param $id
130 3
     *
131 3
     * @return mixed
132
     */
133 3
    public function find($id, array $with = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135 3
        $this->addWithCriteria($with);
136
        $this->applyCriteria();
137 3
138
        $result = $this->model->find($id);
139
140
        $this->refresh();
141
142
        return $result;
143
    }
144 1
145
    /**
146 1
     * @param $id
147 1
     *
148
     * @return mixed|Exception
149 1
     */
150
    public function findOrFail($id, array $with = [])
151 1
    {
152
        $this->addWithCriteria($with);
153 1
        $this->applyCriteria();
154
155
        $result = $this->model->findOrFail($id);
156
157
        $this->refresh();
158
159
        return $result;
160
    }
161 1
162
    /**
163 1
     * @param $field
164 1
     * @param $value
165
     *
166 1
     * @return mixed
167
     */
168 1
    public function findBy($field, $value, array $with = [])
169
    {
170 1
        $this->addWithCriteria($with);
171
        $this->applyCriteria();
172
173
        $result = $this->model->where($field, '=', $value)->first();
174
175
        $this->refresh();
176
177
        return $result;
178 1
    }
179
180 1
    /**
181 1
     * @param $field
182
     * @param $value
183 1
     *
184
     * @return mixed
185 1
     */
186
    public function findByOrFail($field, $value, array $with = [])
187 1
    {
188
        $this->addWithCriteria($with);
189
        $this->applyCriteria();
190
191
        $result = $this->model->where($field, '=', $value)->firstOrFail();
192
193
        $this->refresh();
194
195 1
        return $result;
196
    }
197 1
198 1
    /**
199
     * @param $field
200 1
     * @param $value
201
     *
202 1
     * @return mixed
203
     */
204 1
    public function findAllBy($field, $value, array $with = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        $this->addWithCriteria($with);
207
        $this->applyCriteria();
208
209
        $result = $this->model->where($field, '=', $value)->get();
210
211 9
        $this->refresh();
212
213 9
        return $result;
214
    }
215
216 9
    /**
217
     * @param array $with
218
     * @return mixed
219
     */
220
    public function first(array $with = [])
221 11
    {
222
        $this->addWithCriteria($with);
223 11
        $this->applyCriteria();
224
225
        $result = $this->model->first();
226
227 11
        $this->refresh();
228 11
229 11
        return $result;
230
    }
231
232
    /**
233
     * @param array $with
234
     * @return mixed
235
     */
236
    public function firstOrFail(array $with = [])
237
    {
238
        $this->addWithCriteria($with);
239
        $this->applyCriteria();
240
241
        $result = $this->model->firstOrFail();
242
243
        $this->refresh();
244
245 9
        return $result;
246
    }
247 9
248
    /**
249
     * @param array $with
250
     *
251
     * @return void
252
     */
253
    protected function addWithCriteria(array $with = [])
254
    {
255
        if (count($with) > 0) {
256
            $this->pushCriteria(new WithCriteria($with));
257
        }
258
    }
259
260
    protected function refresh()
261
    {
262
        if (!$this->model instanceof EloquentModel) {
263
            $this->model = $this->model->getModel();
264
        }
265
266
        $this->model = $this->model->newInstance();
267
        $this->criteria = new Collection();
268
    }
269
270
    /**
271
     * @param bool $status
272
     *
273
     * @return $this
274
     */
275 9
    public function skipCriteria($status = true)
276
    {
277 9
        $this->skipCriteria = $status;
278
279
        return $this;
280
    }
281 9
282
    /**
283 9
     * @return Collection
284
     */
285 9
    public function getCriteria()
286
    {
287
        return $this->criteria;
288
    }
289
290
    /**
291
     * @param Criteria $criteria
292
     *
293
     * @return $this
294
     */
295
    public function getByCriteria(Criteria $criteria)
296
    {
297
        $this->model = $criteria->apply($this->model, $this);
298
299
        return $this;
300
    }
301
302
    /**
303
     * @param Criteria $criteria
304
     *
305
     * @return $this
306
     */
307
    public function pushCriteria(Criteria $criteria)
308
    {
309
        $this->criteria->push($criteria);
310
311
        return $this;
312
    }
313
314
    /**
315
     * @return $this
316
     */
317
    public function applyCriteria()
318
    {
319
        if ($this->skipCriteria === true) {
320
            return $this;
321
        }
322
323
        foreach ($this->getCriteria() as $criteria) {
324
            $this->model = $criteria->apply($this->model, $this);
325
        }
326
327
        return $this;
328
    }
329
}
330