Passed
Push — master ( ee4194...734cc2 )
by Hamzah
02:15
created

AbstractRepository::createOrFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Hamza Alayed
5
 * Date: 11/29/18
6
 * Time: 9:38 AM.
7
 */
8
9
namespace Shamaseen\Repository\Generator\Utility;
10
11
12
use Illuminate\Container\Container as App;
13
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
14
use Illuminate\Contracts\Pagination\Paginator;
15
use Illuminate\Database\Eloquent\Builder;
16
17
/**
18
 * Class Database.
19
 */
20
abstract class AbstractRepository implements ContractInterface
21
{
22
    protected $with = [];
23
    /**
24
     * @var App
25
     */
26
    protected $app;
27
28
    /** @var string */
29
    protected $order = null;
30
31
    protected $direction = 'desc';
32
    /**
33
     * @var Entity
34
     */
35
    protected $model;
36
    /**
37
     * @var boolean
38
     */
39
    private $trash = false;
40
    /**
41
     * @var boolean
42
     */
43
    private $withTrash = false;
44
45
    /**
46
     * @param App $app
47
     */
48
    public function __construct(App $app)
49
    {
50
        $this->app = $app;
51
        $this->makeModel();
52
    }
53
54
    protected function makeModel()
55
    {
56
        $this->model = $this->app->make($this->getModelClass());
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    abstract protected function getModelClass(): string;
63
64
    /**
65
     * @param int $limit
66
     * @param array $criteria
67
     *
68
     * @return Paginator
69
     */
70
    public function simplePaginate($limit = 10, $criteria = [])
71
    {
72
        return $this->filter($criteria)->simplePaginate($limit);
73
    }
74
75
    /**
76
     * @param array $criteria
77
     * @return Entity
78
     */
79
    public function filter($criteria = [])
80
    {
81
        $criteria= $this->order($criteria);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $criteria. This often makes code more readable.
Loading history...
82
83
        /** @var Entity $latest */
84
        $latest = $this->model->with($this->with);
85
        if ('' != $this->order) {
86
            $latest->orderBy($this->order, $this->direction);
87
        }
88
89
        if (isset($criteria['search'])) {
90
            foreach ($this->model->searchable as $item) {
91
                $latest->where($item, 'like', '%' . $criteria['search'] . '%', 'or');
92
            }
93
            unset($criteria['search']);
94
        }
95
96
97
        if ($this->trash) {
98
            $latest->onlyTrashed();
99
        }
100
        if ($this->withTrash) {
101
            $latest->withTrashed();
102
        }
103
104
        return $latest->where($criteria);
105
    }
106
107
    /**
108
     * prepare order for query
109
     *
110
     * @param array $criteria
111
     *
112
     * @return array
113
     */
114
    private function order($criteria=[]){
115
116
        if (isset($criteria['order'])) {
117
            $this->order = $criteria['order'];
118
            unset($criteria['order']);
119
        }
120
121
        if (isset($criteria['direction'])) {
122
            $this->direction = $criteria['direction'];
123
            unset($criteria['direction']);
124
        }
125
        unset($criteria['page']);
126
127
        return $criteria;
128
    }
129
130
    /**
131
     * @param int $limit
132
     * @param array $criteria
133
     *
134
     * @return LengthAwarePaginator
135
     */
136
    public function paginate($limit = 10, $criteria = [])
137
    {
138
        return $this->filter($criteria)->paginate($limit);
139
    }
140
141
    /**
142
     * @param array $criteria
143
     *
144
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
145
     */
146
    public function get($criteria = [])
147
    {
148
        return $this->filter($criteria)->get();
149
    }
150
151
    /**
152
     * @param $entityId
153
     * @param array $attributes
154
     *
155
     * @return bool
156
     */
157
    public function update($entityId = 0, $attributes = [])
158
    {
159
        $item = $this->model->where('id', $entityId);
160
161
        if ($item) {
162
            return $item->update($attributes);
163
        }
164
165
        return false;
166
    }
167
168
    /**
169
     * @param $entityId
170
     *
171
     * @throws \Exception
172
     *
173
     * @return bool
174
     */
175
    public function delete($entityId = 0)
176
    {
177
        $item = $this->model->where('id', $entityId);
178
179
        return $item->delete();
180
    }
181
182
    /**
183
     * @param array $attributes
184
     *
185
     * @return bool
186
     */
187
    public function insert($attributes = [])
188
    {
189
        return $this->model->insert($attributes);
190
    }
191
192
193
    /**
194
     * @param array $columns
195
     *
196
     * @return mixed
197
     */
198
    public function all($columns = ['*'])
199
    {
200
        return $this->model->all($columns);
201
    }
202
203
    /**
204
     * @param string $name
205
     * @param string $entityId
206
     * @param array $criteria
207
     *
208
     * @return array
209
     */
210
    public function pluck($name = 'name', $entityId = 'id', $criteria = [])
211
    {
212
        return $this->model->where($criteria)->pluck($name, $entityId)->toArray();
213
    }
214
215
    /**
216
     * @param $entityId
217
     * @param array $columns
218
     *
219
     * @return Entity
220
     */
221
    public function find($entityId = 0, $columns = ['*'])
222
    {
223
        return $this->model->with($this->with)->select($columns)->where('id', $entityId)->first();
224
    }
225
226
    /**
227
     * @param array $filter
228
     * @param array $columns
229
     *
230
     * @return Entity
231
     */
232
    public function first($filter = [], $columns = ['*'])
233
    {
234
        return $this->model->with($this->with)->select($columns)->where($filter)->first();
235
    }
236
237
    /**
238
     * @param $haystack
239
     * @param $needle
240
     *
241
     * @return Entity[]|\Illuminate\Database\Eloquent\Collection
242
     */
243
    public function search($haystack, $needle)
244
    {
245
        return $this->model->where($haystack, 'like', $needle)->get();
246
    }
247
248
249
    /**
250
     * @param $criteria
251
     * @param array $columns
252
     *
253
     * @return Entity
254
     */
255
    public function findBy($criteria = [], $columns = ['*'])
256
    {
257
        return $this->model->with($this->with)->select($columns)->where($criteria)->first();
258
    }
259
260
    /**
261
     * @param array $attributes
262
     *
263
     * @return Entity|\Illuminate\Database\Eloquent\Model
264
     */
265
    public function create($attributes = [])
266
    {
267
        return $this->model->create($attributes);
268
    }
269
270
    /**
271
     * @param array $attributes
272
     *
273
     * @return Entity|\Illuminate\Database\Eloquent\Model
274
     */
275
    public function createOrUpdate($attributes = [])
276
    {
277
        return $this->model->updateOrCreate($attributes);
278
    }
279
280
    /**
281
     * @param array $data
282
     *
283
     * @return \Illuminate\Database\Eloquent\Model
284
     */
285
    public function createOrFirst($data = [])
286
    {
287
        return $this->model->firstOrCreate($data);
288
    }
289
290
    /**
291
     * Get entity name
292
     *
293
     * @return string
294
     */
295
    public function entityName()
296
    {
297
        return $this->getModelClass();
298
    }
299
300
    /**
301
     * @param int $entityId
302
     *
303
     * @return bool
304
     */
305
    public function restore($entityId = 0)
306
    {
307
        /** @var Entity $entity */
308
        $entity = $this->model->withTrashed()
309
            ->whereId($entityId)
310
            ->first();
311
        if ($entity) {
312
            return $entity->restore() ?? false;
313
        }
314
315
        return false;
316
    }
317
318
    /**
319
     * @param int $entityId
320
     *
321
     * @return bool
322
     */
323
    public function forceDelete($entityId = 0)
324
    {
325
        /** @var Entity $entity */
326
        $entity = $this->model->withTrashed()
327
            ->whereId($entityId)
328
            ->first();
329
        if ($entity) {
330
            return $entity->forceDelete() ?? false;
331
        }
332
333
        return false;
334
    }
335
336
    /**
337
     * @return void
338
     */
339
    public function trash()
340
    {
341
        $this->trash = true;
342
        $this->withTrash = false;
343
    }
344
345
    /**
346
     * @return void
347
     */
348
    public function withTrash()
349
    {
350
        $this->trash = false;
351
        $this->withTrash = true;
352
    }
353
}
354