Passed
Push — master ( 5bf517...dce4a9 )
by Hamzah
02:58
created

AbstractRepository::forceDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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