SelectsEntity   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 150
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 13 2
A getWhereFirst() 0 13 3
A getWhere() 0 7 2
A paginate() 0 3 1
A find() 0 19 3
A getWhereIn() 0 3 1
A get() 0 9 2
A getWhereInFirst() 0 9 2
1
<?php
2
3
namespace Orkhanahmadov\EloquentRepository\Repository\Concerns;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Contracts\Cache\Factory;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Collection;
10
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Cacheable;
11
12
/**
13
 * @property-read Builder|Model $model
14
 * @property-read Factory $cache
15
 * @method string cacheKey()
16
 * @method int cacheTTLValue()
17
 * @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
18
 */
19
trait SelectsEntity
20
{
21
    /**
22
     * Returns all models.
23
     *
24
     * @return Builder[]|Collection
25
     */
26
    public function all()
27
    {
28
        if ($this instanceof Cacheable) {
29
            return $this->cache->remember(
30
                $this->cacheKey() . '.*',
31
                $this->cacheTTLValue(),
32
                function () {
33
                    return $this->get();
34
                }
35
            );
36
        }
37
38
        return $this->get();
39
    }
40
41
    /**
42
     * Returns all models with selected columns.
43
     *
44
     * @param mixed $columns
45
     *
46
     * @return Builder[]|Collection
47
     */
48
    public function get(...$columns)
49
    {
50
        $columns = Arr::flatten($columns);
51
52
        if (count($columns) === 0) {
53
            $columns = ['*'];
54
        }
55
56
        return $this->model->get($columns);
57
    }
58
59
    /**
60
     * Finds a model with ID.
61
     *
62
     * @param int|string $modelId
63
     *
64
     * @return Builder|Builder[]|Collection|Model|null
65
     */
66
    public function find($modelId)
67
    {
68
        if ($this instanceof Cacheable) {
69
            $model = $this->cache->remember(
70
                $this->cacheKey() . '.' . $modelId,
71
                $this->cacheTTLValue(),
72
                function () use ($modelId) {
73
                    return $this->model->find($modelId);
74
                }
75
            );
76
        } else {
77
            $model = $this->model->find($modelId);
78
        }
79
80
        if (! $model) {
81
            $this->throwModelNotFoundException($modelId);
0 ignored issues
show
Bug introduced by
It seems like throwModelNotFoundException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            $this->/** @scrutinizer ignore-call */ 
82
                   throwModelNotFoundException($modelId);
Loading history...
82
        }
83
84
        return $model;
85
    }
86
87
    /**
88
     * Paginates models.
89
     *
90
     * @param int $perPage
91
     *
92
     * @return Builder[]|Collection|mixed
93
     */
94
    public function paginate(int $perPage)
95
    {
96
        return $this->model->paginate($perPage);
97
    }
98
99
    /**
100
     * Finds models with "where" condition.
101
     *
102
     * @param string|array $column
103
     * @param mixed $value
104
     *
105
     * @return Builder[]|Collection
106
     */
107
    public function getWhere($column, $value = null)
108
    {
109
        if (is_array($column)) {
110
            return $this->model->where($column)->get();
111
        }
112
113
        return $this->model->where($column, $value)->get();
114
    }
115
116
    /**
117
     * Finds models with "whereIn" condition.
118
     *
119
     * @param string $column
120
     * @param mixed $values
121
     *
122
     * @return Builder[]|Collection
123
     */
124
    public function getWhereIn(string $column, $values)
125
    {
126
        return $this->model->whereIn($column, $values)->get();
127
    }
128
129
    /**
130
     * Finds first model with "where" condition.
131
     *
132
     * @param string|array $column
133
     * @param mixed $value
134
     *
135
     * @return Builder|Model|object|null
136
     */
137
    public function getWhereFirst($column, $value = null)
138
    {
139
        if (is_array($column)) {
140
            $model = $this->model->where($column)->first();
141
        } else {
142
            $model = $this->model->where($column, $value)->first();
143
        }
144
145
        if (! $model) {
146
            $this->throwModelNotFoundException();
147
        }
148
149
        return $model;
150
    }
151
152
    /**
153
     * Finds first model with "whereIn" condition.
154
     *
155
     * @param string $column
156
     * @param mixed $values
157
     *
158
     * @return Builder|Model|object|null
159
     */
160
    public function getWhereInFirst(string $column, $values)
161
    {
162
        $model = $this->model->whereIn($column, $values)->first();
163
164
        if (! $model) {
165
            $this->throwModelNotFoundException();
166
        }
167
168
        return $model;
169
    }
170
}
171