Passed
Pull Request — master (#17)
by Orkhan
03:43
created

SelectsEntity::getWhereFirst()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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
 */
18
trait SelectsEntity
19
{
20
    /**
21
     * Returns all models.
22
     *
23
     * @return Builder[]|Collection
24
     */
25
    public function all()
26
    {
27
        if ($this instanceof Cacheable) {
28
            return $this->cache->remember(
29
                $this->cacheKey().'.*',
30
                $this->cacheTTLValue(),
31
                function () {
32
                    return $this->get();
33
                }
34
            );
35
        }
36
37
        return $this->get();
38
    }
39
40
    /**
41
     * Returns all models with selected columns.
42
     *
43
     * @param mixed $columns
44
     *
45
     * @return Builder[]|Collection
46
     */
47
    public function get(...$columns)
48
    {
49
        $columns = Arr::flatten($columns);
50
51
        if (count($columns) === 0) {
52
            $columns = ['*'];
53
        }
54
55
        return $this->model->get($columns);
56
    }
57
58
    /**
59
     * Finds a model with ID.
60
     *
61
     * @param int|string $modelId
62
     *
63
     * @return Builder|Builder[]|Collection|Model|null
64
     */
65
    public function find($modelId)
66
    {
67
        if ($this instanceof Cacheable) {
68
            $model = $this->cache->remember(
69
                $this->cacheKey().'.'.$modelId,
70
                $this->cacheTTLValue(),
71
                function () use ($modelId) {
72
                    return $this->model->find($modelId);
73
                }
74
            );
75
        } else {
76
            $model = $this->model->find($modelId);
77
        }
78
79
        if (! $model) {
80
            $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

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