Skip to content

Commit 5ea2ad8

Browse files
committed
refactor code
1 parent 165c120 commit 5ea2ad8

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/Repository/Concerns/SelectsEntity.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,31 @@ public function getWhereIn(string $column, $values)
115115
* @param string|array $column
116116
* @param mixed $value
117117
*
118-
* @return Builder|Model|object|null
118+
* @return \Illuminate\Database\Eloquent\Model|object|null
119119
*/
120120
public function getWhereFirst($column, $value = null)
121121
{
122-
if (is_array($column)) {
123-
$model = $this->model->where($column)->first();
124-
} else {
125-
$model = $this->model->where($column, $value)->first();
126-
}
127-
122+
$model = $this->buildGetWhereDelegator($column, $value)->first();
128123
if (!$model) {
129124
$this->throwModelNotFoundException();
130125
}
131-
132126
return $model;
133127
}
134128

129+
/**
130+
* Build delegator for get where expression
131+
*
132+
* @param $column
133+
* @param null $value
134+
*
135+
* @return \Illuminate\Database\Eloquent\Builder
136+
*/
137+
private function buildGetWhereDelegator($column, $value = null): Builder
138+
{
139+
return is_array($column) ?
140+
$this->model->where($column) : $this->model->where($column, $value);
141+
}
142+
135143
/**
136144
* Finds first model with "whereIn" condition.
137145
*
@@ -161,7 +169,7 @@ public function getWhereInFirst(string $column, $values)
161169
*/
162170
public function count($column, $value = null)
163171
{
164-
return $this->getWhere($column, $value)->count();
172+
return $this->buildGetWhereDelegator($column, $value)->count();
165173
}
166174

167175
/**
@@ -174,11 +182,7 @@ public function count($column, $value = null)
174182
*/
175183
public function getWhere($column, $value = null)
176184
{
177-
if (is_array($column)) {
178-
return $this->model->where($column)->get();
179-
}
180-
181-
return $this->model->where($column, $value)->get();
185+
return $this->buildGetWhereDelegator($column, $value)->get();
182186
}
183187

184188
/**
@@ -191,7 +195,7 @@ public function getWhere($column, $value = null)
191195
*/
192196
public function existed($column, $value = null)
193197
{
194-
return $this->getWhere($column, $value)->count() > 0;
198+
return $this->buildGetWhereDelegator($column, $value)->exists();
195199
}
196200

197201
/**
@@ -206,11 +210,19 @@ public function existed($column, $value = null)
206210
*/
207211
public function getWhereWithSorted(array $conditions, array $sortedBy = null, ?int $limit = 5000, ?int $offset = 0)
208212
{
209-
$delegator = $this->getDelegatorByConditionsAndSortedBy($conditions, $sortedBy);
213+
$delegator = $this->buildGetWhereDelegatorWithOrderBy($conditions, $sortedBy);
210214
return $delegator->skip($offset)->take($limit)->get();
211215
}
212216

213-
private function getDelegatorByConditionsAndSortedBy(array $conditions, array $sortedBy = null)
217+
/**
218+
* Build delegator get where expression with sorted by
219+
*
220+
* @param array $conditions
221+
* @param array|null $sortedBy
222+
*
223+
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
224+
*/
225+
private function buildGetWhereDelegatorWithOrderBy(array $conditions, array $sortedBy = null)
214226
{
215227
$sortedBy = empty($sortedBy) ? [$this->model->getKeyName() => 'desc'] : $sortedBy;
216228
$delegator = $this->model->where($conditions);
@@ -230,8 +242,8 @@ private function getDelegatorByConditionsAndSortedBy(array $conditions, array $s
230242
*/
231243
public function getWhereFirstWithSorted(array $conditions, array $sortedBy = null)
232244
{
233-
$delegator = $this->getDelegatorByConditionsAndSortedBy($conditions, $sortedBy);
234-
$model = $delegator->take(1)->first();
245+
$delegator = $this->buildGetWhereDelegatorWithOrderBy($conditions, $sortedBy);
246+
$model = $delegator->first();
235247
if (!$model) {
236248
$this->throwModelNotFoundException();
237249
}

src/Repository/Contracts/Repository.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ public function paginate(int $perPage);
4747
*/
4848
public function find($modelId);
4949

50+
51+
/**
52+
* Count models with "where" condition.
53+
*
54+
* @param string|array $column
55+
* @param mixed $value
56+
*
57+
* @return int
58+
*/
59+
public function count($column, $value = null);
60+
61+
/**
62+
* Check if model existed with "where" condition.
63+
*
64+
* @param string|array $column
65+
* @param mixed $value
66+
*
67+
* @return bool
68+
*/
69+
public function existed($column, $value = null);
70+
5071
/**
5172
* Finds models with "where" condition.
5273
*

0 commit comments

Comments
 (0)