Passed
Push — master ( 7fb54d...008f84 )
by Hamzah
02:17
created

AbstractRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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\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 Contract
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
        if (isset($filters['order'])) {
80
            $this->order = $filters['order'];
81
            unset($filters['order']);
82
        }
83
84
        if (isset($filters['direction'])) {
85
            $this->direction = $filters['direction'];
86
            unset($filters['direction']);
87
        }
88
89
        /** @var Entity $latest */
90
        $latest = $this->model->with($this->with);
91
        if ('' != $this->order) {
92
            $latest->orderBy($this->order, $this->direction);
93
        }
94
95
        if (isset($filters['search'])) {
96
            foreach ($this->model->searchable as $item) {
97
                $latest->where($item, 'like', '%' . $filters['search'] . '%', 'or');
98
            }
99
100
            unset($filters['search']);
101
        }
102
        unset($filters['page']);
103
104
        if ($this->trash) {
105
            $latest->onlyTrashed();
106
        }
107
        if ($this->withTrash) {
108
            $latest->withTrashed();
109
        }
110
111
        return $latest->where($filters);
112
    }
113
114
    /**
115
     * @param int $limit
116
     * @param array $filters
117
     *
118
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
119
     */
120
    public function paginate($limit = 10, $filters = [])
121
    {
122
        return $this->filter($filters)->paginate($limit);
123
    }
124
125
    /**
126
     * @param array $filters
127
     *
128
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
129
     */
130
    public function get($filters = [])
131
    {
132
        return $this->filter($filters)->get();
133
    }
134
135
    /**
136
     * @param $entityId
137
     * @param array $attributes
138
     *
139
     * @return bool
140
     */
141
    public function update($entityId = 0, $attributes = [])
142
    {
143
        $item = $this->model->where('id', $entityId);
144
145
        if ($item) {
146
            return $item->update($attributes);
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * @param $entityId
154
     *
155
     * @throws \Exception
156
     *
157
     * @return bool
158
     */
159
    public function delete($entityId = 0)
160
    {
161
        $item = $this->model->where('id', $entityId);
162
163
        return $item->delete();
164
    }
165
166
    /**
167
     * @param array $attributes
168
     *
169
     * @return bool
170
     */
171
    public function insert($attributes = [])
172
    {
173
        return $this->model->insert($attributes);
174
    }
175
176
177
    /**
178
     * @param array $columns
179
     *
180
     * @return mixed
181
     */
182
    public function all($columns = ['*'])
183
    {
184
        return $this->model->all($columns);
185
    }
186
187
    /**
188
     * @param string $name
189
     * @param string $entityId
190
     * @param array $filters
191
     *
192
     * @return array
193
     */
194
    public function pluck($name = 'name', $entityId = 'id', $filters = [])
195
    {
196
        return $this->model->where($filters)->pluck($name, $entityId)->toArray();
197
    }
198
199
    /**
200
     * @param $entityId
201
     * @param array $columns
202
     *
203
     * @return Entity
204
     */
205
    public function find($entityId = 0, $columns = ['*'])
206
    {
207
        return $this->model->with($this->with)->select($columns)->where('id', $entityId)->first();
208
    }
209
210
    /**
211
     * @param array $filter
212
     * @param array $columns
213
     *
214
     * @return Entity
215
     */
216
    public function first($filter = [], $columns = ['*'])
217
    {
218
        return $this->model->with($this->with)->select($columns)->where($filter)->first();
219
    }
220
221
    /**
222
     * @param $haystack
223
     * @param $needle
224
     *
225
     * @return Entity[]|\Illuminate\Database\Eloquent\Collection
226
     */
227
    public function search($haystack, $needle)
228
    {
229
        return $this->model->where($haystack, 'like', $needle)->get();
230
    }
231
232
233
    /**
234
     * @param $filters
235
     * @param array $columns
236
     *
237
     * @return Entity
238
     */
239
    public function findBy($filters = [], $columns = ['*'])
240
    {
241
        return $this->model->with($this->with)->select($columns)->where($filters)->first();
242
    }
243
244
    /**
245
     * @param array $attributes
246
     *
247
     * @return Entity|\Illuminate\Database\Eloquent\Model
248
     */
249
    public function create($attributes = [])
250
    {
251
        return $this->model->create($attributes);
252
    }
253
254
    /**
255
     * @param array $attributes
256
     *
257
     * @return Entity|\Illuminate\Database\Eloquent\Model
258
     */
259
    public function createOrUpdate($attributes = [])
260
    {
261
        return $this->model->updateOrCreate($attributes);
262
    }
263
264
    /**
265
     * @param array $data
266
     *
267
     * @return \Illuminate\Database\Eloquent\Model
268
     */
269
    public function createOrFirst($data = [])
270
    {
271
        return $this->model->firstOrCreate($data);
272
    }
273
274
    /**
275
     * Get entity name
276
     *
277
     * @return string
278
     */
279
    public function entityName()
280
    {
281
        return $this->getModelClass();
282
    }
283
284
    /**
285
     * @param int $entityId
286
     *
287
     * @return bool|null
288
     */
289
    public function restore($entityId = 0)
290
    {
291
        /** @var Entity $entity */
292
        $entity = $this->model->withTrashed()
293
            ->whereId($entityId)
294
            ->first();
295
        if ($entity) {
296
            return $entity->restore();
297
        }
298
299
        return false;
300
    }
301
302
    /**
303
     * @param int $entityId
304
     *
305
     * @return bool|null
306
     */
307
    public function forceDelete($entityId = 0)
308
    {
309
        /** @var Entity $entity */
310
        $entity = $this->model->withTrashed()
311
            ->whereId($entityId)
312
            ->first();
313
        if ($entity) {
314
            return $entity->forceDelete();
315
        }
316
317
        return false;
318
    }
319
320
    /**
321
     * @return void
322
     */
323
    public function trash()
324
    {
325
        $this->trash = true;
326
        $this->withTrash = false;
327
    }
328
329
    /**
330
     * @return void
331
     */
332
    public function withTrash()
333
    {
334
        $this->trash = false;
335
        $this->withTrash = true;
336
    }
337
}
338