Passed
Push — master ( da4f7e...b51252 )
by Mohammad
02:42
created

AbstractRepository::findOrFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
14
use Illuminate\Contracts\Pagination\Paginator;
15
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Builder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
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)->find($entityId,$columns);
224
    }
225
226
    /**
227
     * @param $entityId
228
     * @param array $columns
229
     *
230
     * @return Entity
231
     */
232
    public function findOrFail($entityId = 0, $columns = ['*'])
233
    {
234
        return $this->model->with($this->with)->findOrFail($entityId,$columns);
235
    }
236
    /**
237
     * @param array $filter
238
     * @param array $columns
239
     *
240
     * @return Entity
241
     */
242
    public function first($filter = [], $columns = ['*'])
243
    {
244
        return $this->model->with($this->with)->select($columns)->where($filter)->first();
245
    }
246
247
    /**
248
     * @param $haystack
249
     * @param $needle
250
     *
251
     * @return Entity[]|\Illuminate\Database\Eloquent\Collection
252
     */
253
    public function search($haystack, $needle)
254
    {
255
        return $this->model->where($haystack, 'like', $needle)->get();
256
    }
257
258
259
    /**
260
     * @param $criteria
261
     * @param array $columns
262
     *
263
     * @return Entity
264
     */
265
    public function findBy($criteria = [], $columns = ['*'])
266
    {
267
        return $this->model->with($this->with)->select($columns)->where($criteria)->first();
268
    }
269
270
    /**
271
     * @param array $attributes
272
     *
273
     * @return Entity|\Illuminate\Database\Eloquent\Model
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
274
     */
275
    public function create($attributes = [])
276
    {
277
        return $this->model->create($attributes);
278
    }
279
280
    /**
281
     * @param array $attributes
282
     *
283
     * @return Entity|\Illuminate\Database\Eloquent\Model
284
     */
285
    public function createOrUpdate($attributes = [])
286
    {
287
        return $this->model->updateOrCreate($attributes);
288
    }
289
290
    /**
291
     * @param array $data
292
     *
293
     * @return \Illuminate\Database\Eloquent\Model
294
     */
295
    public function createOrFirst($data = [])
296
    {
297
        return $this->model->firstOrCreate($data);
298
    }
299
300
    /**
301
     * Get entity name
302
     *
303
     * @return string
304
     */
305
    public function entityName()
306
    {
307
        return $this->getModelClass();
308
    }
309
310
    /**
311
     * @param int $entityId
312
     *
313
     * @return bool
314
     */
315
    public function restore($entityId = 0)
316
    {
317
        /** @var Entity $entity */
318
        $entity = $this->model->withTrashed()
319
            ->whereId($entityId)
320
            ->first();
321
        if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
322
            return $entity->restore() ?? false;
323
        }
324
325
        return false;
326
    }
327
328
    /**
329
     * @param int $entityId
330
     *
331
     * @return bool
332
     */
333
    public function forceDelete($entityId = 0)
334
    {
335
        /** @var Entity $entity */
336
        $entity = $this->model->withTrashed()
337
            ->whereId($entityId)
338
            ->first();
339
        if ($entity) {
0 ignored issues
show
introduced by
$entity is of type Shamaseen\Repository\Generator\Utility\Entity, thus it always evaluated to true.
Loading history...
340
            return $entity->forceDelete() ?? false;
341
        }
342
343
        return false;
344
    }
345
346
    /**
347
     * @return void
348
     */
349
    public function trash()
350
    {
351
        $this->trash = true;
352
        $this->withTrash = false;
353
    }
354
355
    /**
356
     * @return void
357
     */
358
    public function withTrash()
359
    {
360
        $this->trash = false;
361
        $this->withTrash = true;
362
    }
363
}
364