Passed
Branch master (f056f7)
by Faidz
03:05
created

DKBaseRepository::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Denknows\DKBase;
4
5
use Denknows\DKBase\Exception\GeneralException;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * Class DKBaseRepository
11
 *
12
 * @package Denknows\DKBaseRepository
13
 */
14
abstract class DKBaseRepository
15
{
16
    /**
17
     * The repository app.
18
     *
19
     * @var \Illuminate\Database\Eloquent\Model
20
     */
21
    protected $app;
22
23
    /**
24
     * The repository model.
25
     *
26
     * @var \Illuminate\Database\Eloquent\Model
27
     */
28
    protected $model;
29
30
    /**
31
     * The query builder.
32
     *
33
     * @var \Illuminate\Database\Eloquent\Builder
34
     */
35
    protected $query;
36
37
    /**
38
     * Alias for the query limit.
39
     *
40
     * @var int
41
     */
42
    protected $take;
43
44
    /**
45
     * Array of related models to eager load.
46
     *
47
     * @var array
48
     */
49
    protected $with = [];
50
51
    /**
52
     * Array of one or more where clause parameters.
53
     *
54
     * @var array
55
     */
56
    protected $wheres = [];
57
58
    /**
59
     * Array of one or more where in clause parameters.
60
     *
61
     * @var array
62
     */
63
    protected $whereIns = [];
64
65
    /**
66
     * Array of one or more ORDER BY column/value pairs.
67
     *
68
     * @var array
69
     */
70
    protected $orderBys = [];
71
72
    /**
73
     * Array of scope methods to call on the model.
74
     *
75
     * @var array
76
     */
77
    protected $scopes = [];
78
79
    /**
80
     * DKBaseRepository constructor.
81
     *
82
     * @throws GeneralException
83
     */
84
    public function __construct(Container $app)
0 ignored issues
show
Bug introduced by
The type Denknows\DKBase\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
    {
86
        $this->app = $app;
87
        $this->model = $this->makeModel();
88
    }
89
90
    /**
91
     * Specify Model class name.
92
     *
93
     * @return mixed
94
     */
95
    abstract public function model();
96
97
    /**
98
     * @param array|string[] $columns
99
     * @return \Illuminate\Database\Eloquent\Builder[]|Collection
100
     */
101
    public function all(array $columns = ['*'])
102
    {
103
        $this->newQuery()->eagerLoad();
104
105
        $models = $this->query->get($columns);
106
107
        $this->unsetClauses();
108
109
        return $models;
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function count(): int
116
    {
117
        return $this->model->count();
118
    }
119
120
    /**
121
     * @param array $data
122
     * @return mixed
123
     */
124
    public function create(array $data)
125
    {
126
        $this->unsetClauses();
127
128
        return $this->model->create($data);
129
    }
130
131
    /**
132
     * @param array $data
133
     * @return Collection
134
     */
135
    public function createMultiple(array $data)
136
    {
137
        $models = new Collection();
138
139
        foreach ($data as $d) {
140
            $models->push($this->create($d));
141
        }
142
143
        return $models;
144
    }
145
146
    /**
147
     * @return mixed
148
     */
149
    public function delete()
150
    {
151
        $this->newQuery()->setClauses()->setScopes();
152
153
        $result = $this->query->delete();
154
155
        $this->unsetClauses();
156
157
        return $result;
158
    }
159
160
    /**
161
     * @param $id
162
     * @return bool
163
     * @throws \Exception
164
     */
165
    public function deleteById($id): bool
166
    {
167
        $this->unsetClauses();
168
169
        return $this->getById($id)->delete();
170
    }
171
172
    /**
173
     * @param array $ids
174
     * @return int
175
     */
176
    public function deleteMultipleById(array $ids): int
177
    {
178
        return $this->model->destroy($ids);
179
    }
180
181
    /**
182
     * @param array|string[] $columns
183
     * @return \Illuminate\Database\Eloquent\Builder|Model
184
     */
185
    public function first(array $columns = ['*'])
186
    {
187
        $this->newQuery()->eagerLoad()->setClauses()->setScopes();
188
189
        $model = $this->query->firstOrFail($columns);
190
191
        $this->unsetClauses();
192
193
        return $model;
194
    }
195
196
    /**
197
     * @param array|string[] $columns
198
     * @return \Illuminate\Database\Eloquent\Builder[]|Collection
199
     */
200
    public function get(array $columns = ['*'])
201
    {
202
        $this->newQuery()->eagerLoad()->setClauses()->setScopes();
203
204
        $models = $this->query->get($columns);
205
206
        $this->unsetClauses();
207
208
        return $models;
209
    }
210
211
    /**
212
     * @param $id
213
     * @param array|string[] $columns
214
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|Collection|Model|null
215
     */
216
    public function getById($id, array $columns = ['*'])
217
    {
218
        $this->unsetClauses();
219
220
        $this->newQuery()->eagerLoad();
221
222
        return $this->query->findOrFail($id, $columns);
223
    }
224
225
    /**
226
     * @param $item
227
     * @param $column
228
     * @param array|string[] $columns
229
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
230
     */
231
    public function getByColumn($item, $column, array $columns = ['*'])
232
    {
233
        $this->unsetClauses();
234
235
        $this->newQuery()->eagerLoad();
236
237
        return $this->query->where($column, $item)->first($columns);
238
    }
239
240
    /**
241
     * @param int $limit
242
     * @param array|string[] $columns
243
     * @param string $pageName
244
     * @param null $page
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $page is correct as it would always require null to be passed?
Loading history...
245
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
246
     */
247
    public function paginate($limit = 25, array $columns = ['*'], $pageName = 'page', $page = null)
248
    {
249
        $this->newQuery()->eagerLoad()->setClauses()->setScopes();
250
251
        $models = $this->query->paginate($limit, $columns, $pageName, $page);
252
253
        $this->unsetClauses();
254
255
        return $models;
256
    }
257
258
    /**
259
     * @param $id
260
     * @param array $data
261
     * @param array $options
262
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|Collection|Model|null
263
     */
264
    public function updateById($id, array $data, array $options = [])
265
    {
266
        $this->unsetClauses();
267
268
        $model = $this->getById($id);
269
270
        $model->update($data, $options);
271
272
        return $model;
273
    }
274
275
    /**
276
     * @param $limit
277
     * @return $this
278
     */
279
    public function limit($limit)
280
    {
281
        $this->take = $limit;
282
283
        return $this;
284
    }
285
286
    /**
287
     * @param $column
288
     * @param string $direction
289
     * @return $this
290
     */
291
    public function orderBy($column, $direction = 'asc')
292
    {
293
        $this->orderBys[] = compact('column', 'direction');
294
295
        return $this;
296
    }
297
298
    /**
299
     * @param $column
300
     * @param $value
301
     * @param string $operator
302
     * @return $this
303
     */
304
    public function where($column, $value, $operator = '=')
305
    {
306
        $this->wheres[] = compact('column', 'value', 'operator');
307
308
        return $this;
309
    }
310
311
    /**
312
     * @param $column
313
     * @param $values
314
     * @return $this
315
     */
316
    public function whereIn($column, $values)
317
    {
318
        $values = is_array($values) ? $values : [$values];
319
320
        $this->whereIns[] = compact('column', 'values');
321
322
        return $this;
323
    }
324
325
    /**
326
     * @param $relations
327
     * @return $this
328
     */
329
    public function with($relations)
330
    {
331
        if (is_string($relations)) {
332
            $relations = func_get_args();
333
        }
334
335
        $this->with = $relations;
336
337
        return $this;
338
    }
339
340
    /**
341
     * @return $this
342
     */
343
    protected function newQuery()
344
    {
345
        $this->query = $this->model->newQuery();
346
347
        return $this;
348
    }
349
350
    /**
351
     * @return $this
352
     */
353
    protected function eagerLoad()
354
    {
355
        foreach ($this->with as $relation) {
356
            $this->query->with($relation);
357
        }
358
359
        return $this;
360
    }
361
362
    /**
363
     * @return $this
364
     */
365
    protected function setClauses()
366
    {
367
        foreach ($this->wheres as $where) {
368
            $this->query->where($where['column'], $where['operator'], $where['value']);
369
        }
370
371
        foreach ($this->whereIns as $whereIn) {
372
            $this->query->whereIn($whereIn['column'], $whereIn['values']);
373
        }
374
375
        foreach ($this->orderBys as $orders) {
376
            $this->query->orderBy($orders['column'], $orders['direction']);
377
        }
378
379
        if (isset($this->take) and !is_null($this->take)) {
380
            $this->query->take($this->take);
381
        }
382
383
        return $this;
384
    }
385
386
    /**
387
     * @return $this
388
     */
389
    protected function setScopes()
390
    {
391
        foreach ($this->scopes as $method => $args) {
392
            $this->query->$method(...$args);
393
        }
394
395
        return $this;
396
    }
397
398
    /**
399
     * @return $this
400
     */
401
    protected function unsetClauses()
402
    {
403
        $this->wheres = [];
404
        $this->whereIns = [];
405
        $this->scopes = [];
406
        $this->take = null;
407
408
        return $this;
409
    }
410
411
    /**
412
     * @param $scope
413
     * @param $args
414
     * @return $this
415
     */
416
    public function __call($scope, $args)
417
    {
418
        $this->scopes[$scope] = $args;
419
420
        return $this;
421
    }
422
423
    /**
424
     * @param $column
425
     * @param $key
426
     * @return \Illuminate\Support\Collection
427
     */
428
    public function pluck($column, $key = [])
429
    {
430
        $this->newQuery();
431
432
        $results = $this->query->pluck($column, $key);
433
434
        $this->unsetClauses();
435
436
        return $results;
437
    }
438
439
    /**
440
     * @return mixed
441
     * @throws Exception
442
     */
443
    private function makeModel()
444
    {
445
        $model = $this->app->make($this->model());
446
447
        if (!$model instanceof Model) {
0 ignored issues
show
introduced by
$model is always a sub-type of Illuminate\Database\Eloquent\Model.
Loading history...
448
            throw new Exception("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
449
        }
450
451
        return $model;
452
    }
453
}
454