Completed
Push — develop ( 31d57c...44bfc6 )
by Abdelrahman
01:58
created

BaseRepository::getModelInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Repository Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Repository Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Repository\Repositories;
17
18
use Closure;
19
use Rinvex\Repository\Traits\Cacheable;
20
use Illuminate\Contracts\Container\Container;
21
use Rinvex\Repository\Contracts\CacheableContract;
22
use Rinvex\Repository\Contracts\RepositoryContract;
23
24
abstract class BaseRepository implements RepositoryContract, CacheableContract
25
{
26
    use Cacheable;
27
28
    /**
29
     * The IoC container instance.
30
     *
31
     * @var \Illuminate\Contracts\Container\Container
32
     */
33
    protected $container;
34
35
    /**
36
     * The connection name for the repository.
37
     *
38
     * @var string
39
     */
40
    protected $connection;
41
42
    /**
43
     * The repository identifier.
44
     *
45
     * @var string
46
     */
47
    protected $repositoryId;
48
49
    /**
50
     * The repository model.
51
     *
52
     * @var string
53
     */
54
    protected $model;
55
56
    /**
57
     * The relations to eager load on query execution.
58
     *
59
     * @var array
60
     */
61
    protected $relations = [];
62
63
    /**
64
     * The query where clauses.
65
     *
66
     * @var array
67
     */
68
    protected $where = [];
69
70
    /**
71
     * The query whereIn clauses.
72
     *
73
     * @var array
74
     */
75
    protected $whereIn = [];
76
77
    /**
78
     * The query whereNotIn clauses.
79
     *
80
     * @var array
81
     */
82
    protected $whereNotIn = [];
83
84
    /**
85
     * The query whereHas clauses.
86
     *
87
     * @var array
88
     */
89
    protected $whereHas = [];
90
91
    /**
92
     * The "offset" value of the query.
93
     *
94
     * @var int
95
     */
96
    protected $offset;
97
98
    /**
99
     * The "limit" value of the query.
100
     *
101
     * @var int
102
     */
103
    protected $limit;
104
105
    /**
106
     * The column to order results by.
107
     *
108
     * @var array
109
     */
110
    protected $orderBy = [];
111
112
    /**
113
     * Execute given callback and return the result.
114
     *
115
     * @param string   $class
116
     * @param string   $method
117
     * @param array    $args
118
     * @param \Closure $closure
119
     *
120
     * @return mixed
121
     */
122
    protected function executeCallback($class, $method, $args, Closure $closure)
123
    {
124
        $skipUri = $this->getContainer('config')->get('rinvex.repository.cache.skip_uri');
125
126
        // Check if cache is enabled
127
        if ($this->getCacheLifetime() && ! $this->getContainer('request')->has($skipUri)) {
128
            return $this->cacheCallback($class, $method, $args, $closure);
129
        }
130
131
        // Cache disabled, just execute query & return result
132
        $result = call_user_func($closure);
133
134
        // We're done, let's clean up!
135
        $this->resetRepository();
136
137
        return $result;
138
    }
139
140
    /**
141
     * Reset repository to it's defaults.
142
     *
143
     * @return $this
144
     */
145
    protected function resetRepository()
146
    {
147
        $this->relations  = [];
148
        $this->where      = [];
149
        $this->whereIn    = [];
150
        $this->whereNotIn = [];
151
        $this->whereHas   = [];
152
        $this->offset     = null;
153
        $this->limit      = null;
154
        $this->orderBy    = [];
155
156
        return $this;
157
    }
158
159
    /**
160
     * Prepare query.
161
     *
162
     * @param object $model
163
     *
164
     * @return object
165
     */
166
    protected function prepareQuery($model)
167
    {
168
        // Set the relationships that should be eager loaded
169
        if (! empty($this->relations)) {
170
            $model = $model->with($this->relations);
171
        }
172
173
        // Add a basic where clause to the query
174
        foreach ($this->where as $where) {
175
            list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null);
176
177
            $model = $model->where($attribute, $operator, $value, $boolean);
178
        }
179
180
        // Add a "where in" clause to the query
181
        foreach ($this->whereIn as $whereIn) {
182
            list($attribute, $values, $boolean, $not) = array_pad($whereIn, 4, null);
183
184
            $model = $model->whereIn($attribute, $values, $boolean, $not);
185
        }
186
187
        // Add a "where not in" clause to the query
188
        foreach ($this->whereNotIn as $whereNotIn) {
189
            list($attribute, $values, $boolean) = array_pad($whereNotIn, 3, null);
190
191
            $model = $model->whereNotIn($attribute, $values, $boolean);
192
        }
193
194
        // Add a "where has" clause to the query
195
        foreach ($this->whereHas as $whereHas) {
196
            list($relation, $callback, $operator, $count) = array_pad($whereHas, 4, null);
197
198
            $model = $model->whereHas($relation, $callback, $operator, $count);
199
        }
200
201
        // Set the "offset" value of the query
202
        if ($this->offset > 0) {
203
            $model = $model->offset($this->offset);
204
        }
205
206
        // Set the "limit" value of the query
207
        if ($this->limit > 0) {
208
            $model = $model->limit($this->limit);
209
        }
210
211
        // Add an "order by" clause to the query.
212
        if (! empty($this->orderBy)) {
213
            list($attribute, $direction) = $this->orderBy;
214
215
            $model = $model->orderBy($attribute, $direction);
216
        }
217
218
        return $model;
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function setContainer(Container $container)
225
    {
226
        $this->container = $container;
227
228
        return $this;
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    public function getContainer($service = null)
235
    {
236
        return is_null($service) ? ($this->container ?: app()) : ($this->container[$service] ?: app($service));
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function setConnection($name)
243
    {
244
        $this->connection = $name;
245
246
        return $this;
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function getConnection()
253
    {
254
        return $this->connection;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function setRepositoryId($repositoryId)
261
    {
262
        $this->repositoryId = $repositoryId;
263
264
        return $this;
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function getRepositoryId()
271
    {
272
        return $this->repositoryId ?: get_called_class();
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function setModel($model)
279
    {
280
        $this->model = $model;
281
282
        return $this;
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288
    public function getModel()
289
    {
290
        $model = $this->getContainer('config')->get('rinvex.repository.models');
291
292
        return $this->model ?: str_replace(['Repositories', 'Repository'], [$model, ''], get_called_class());
293
    }
294
295
    /**
296
     * {@inheritdoc}
297
     */
298
    public function getModelInstance($id)
299
    {
300
        return $id ? $this->find($id) : $this->createModel();
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     */
306
    public function with($relations)
307
    {
308
        if (is_string($relations)) {
309
            $relations = func_get_args();
310
        }
311
312
        $this->relations = $relations;
313
314
        return $this;
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    public function where($attribute, $operator = null, $value = null, $boolean = 'and')
321
    {
322
        // The last `$boolean` expression is intentional to fix list() & array_pad() results
323
        $this->where[] = [$attribute, $operator, $value, $boolean ?: 'and'];
324
325
        return $this;
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331
    public function whereIn($attribute, $values, $boolean = 'and', $not = false)
332
    {
333
        // The last `$boolean` & `$not` expressions are intentional to fix list() & array_pad() results
334
        $this->whereIn[] = [$attribute, $values, $boolean ?: 'and', (bool) $not];
335
336
        return $this;
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     */
342
    public function whereNotIn($attribute, $values, $boolean = 'and')
343
    {
344
        // The last `$boolean` expression is intentional to fix list() & array_pad() results
345
        $this->whereNotIn[] = [$attribute, $values, $boolean ?: 'and'];
346
347
        return $this;
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1)
354
    {
355
        // The last `$operator` & `$count` expressions are intentional to fix list() & array_pad() results
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
356
        $this->whereHas[] = [$relation, $callback, $operator ?: '>=', $count ?: 1];
357
358
        return $this;
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     */
364
    public function offset($offset)
365
    {
366
        $this->offset = $offset;
367
368
        return $this;
369
    }
370
371
    /**
372
     * {@inheritdoc}
373
     */
374
    public function limit($limit)
375
    {
376
        $this->limit = $limit;
377
378
        return $this;
379
    }
380
381
    /**
382
     * {@inheritdoc}
383
     */
384
    public function orderBy($attribute, $direction = 'asc')
385
    {
386
        $this->orderBy = [$attribute, $direction];
387
388
        return $this;
389
    }
390
391
    /**
392
     * {@inheritdoc}
393
     */
394
    public function store($id, array $attributes = [])
395
    {
396
        return ! $id ? $this->create($attributes) : $this->update($id, $attributes);
397
    }
398
399
    /**
400
     * {@inheritdoc}
401
     */
402
    public static function __callStatic($method, $parameters)
403
    {
404
        return call_user_func_array([new static(), $method], $parameters);
405
    }
406
407
    /**
408
     * {@inheritdoc}
409
     */
410
    public function __call($method, $parameters)
411
    {
412
        return call_user_func_array([$this->createModel(), $method], $parameters);
413
    }
414
}
415