Passed
Pull Request — master (#30)
by
unknown
08:02
created

SelectsEntity::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
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|null $perPage
91
     *
92
     * @return Builder[]|Collection|mixed
93
     */
94
    public function paginate(?int $perPage = null)
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
     * Determine if any rows exist for the current query.
118
     *
119
     * @param $column
120
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
121
     * @return bool
122
     */
123
    public function exists($column, $value = null)
124
    {
125
        if (is_array($column)) {
126
            return $this->model->where($column)->exists();
127
        }
128
129
        return $this->model->where($column, $value)->exists();
130
    }
131
132
    /**
133
     * Finds models with "whereIn" condition.
134
     *
135
     * @param string $column
136
     * @param mixed $values
137
     *
138
     * @return Builder[]|Collection
139
     */
140
    public function getWhereIn(string $column, $values)
141
    {
142
        return $this->model->whereIn($column, $values)->get();
143
    }
144
145
    /**
146
     * Finds first model with "where" condition.
147
     *
148
     * @param string|array $column
149
     * @param mixed $value
150
     * @param bool $failIfNotFound
151
     * @return Builder|Model|object|null
152
     */
153
    public function getWhereFirst($column, $value = null, $failIfNotFound = false)
154
    {
155
        if (is_array($column)) {
156
            $model = $this->model->where($column)->first();
157
        } else {
158
            $model = $this->model->where($column, $value)->first();
159
        }
160
161
        if (! $model && $failIfNotFound) {
162
            $this->throwModelNotFoundException();
163
        }
164
165
        return $model;
166
    }
167
168
    /**
169
     * Finds first model with "whereIn" condition.
170
     *
171
     * @param string $column
172
     * @param mixed $values
173
     * @param bool $failIfNotFound
174
     * @return Builder|Model|object|null
175
     */
176
    public function getWhereInFirst(string $column, $values, $failIfNotFound = false)
177
    {
178
        $model = $this->model->whereIn($column, $values)->first();
179
180
        if (! $model && $failIfNotFound) {
181
            $this->throwModelNotFoundException();
182
        }
183
184
        return $model;
185
    }
186
}
187