Completed
Push — master ( 2cb525...32e808 )
by Abdelrahman
05:28 queued 03:06
created

BaseRepository::forgetCache()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 3
nop 0
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 repository identifier.
37
     *
38
     * @var string
39
     */
40
    protected $repositoryId;
41
42
    /**
43
     * The repository model.
44
     *
45
     * @var string
46
     */
47
    protected $model;
48
49
    /**
50
     * The relations to eager load on query execution.
51
     *
52
     * @var array
53
     */
54
    protected $relations = [];
55
56
    /**
57
     * The query where clauses.
58
     *
59
     * @var array
60
     */
61
    protected $where = [];
62
63
    /**
64
     * The query whereIn clauses.
65
     *
66
     * @var array
67
     */
68
    protected $whereIn = [];
69
70
    /**
71
     * The query whereNotIn clauses.
72
     *
73
     * @var array
74
     */
75
    protected $whereNotIn = [];
76
77
    /**
78
     * The "offset" value of the query.
79
     *
80
     * @var int
81
     */
82
    protected $offset;
83
84
    /**
85
     * The "limit" value of the query.
86
     *
87
     * @var int
88
     */
89
    protected $limit;
90
91
    /**
92
     * The column to order results by.
93
     *
94
     * @var array
95
     */
96
    protected $orderBy = [];
97
98
    /**
99
     * Execute given callback and return the result.
100
     *
101
     * @param string   $class
102
     * @param string   $method
103
     * @param array    $args
104
     * @param \Closure $closure
105
     *
106
     * @return mixed
107
     */
108
    protected function executeCallback($class, $method, $args, Closure $closure)
109
    {
110
        $skipUri = $this->getContainer('config')->get('rinvex.repository.cache.skip_uri');
111
112
        // Check if cache is enabled
113
        if ($this->getCacheLifetime() && ! $this->getContainer('request')->has($skipUri)) {
114
            return $this->cacheCallback($class, $method, $args, $closure);
115
        }
116
117
        // Cache disabled, just execute qurey & return result
118
        $result = call_user_func($closure);
119
120
        // We're done, let's clean up!
121
        $this->resetRepository();
122
123
        return $result;
124
    }
125
126
    /**
127
     * Reset repository to it's defaults.
128
     *
129
     * @return $this
130
     */
131
    protected function resetRepository()
132
    {
133
        $this->relations  = [];
134
        $this->where      = [];
135
        $this->whereIn    = [];
136
        $this->whereNotIn = [];
137
        $this->offset     = null;
138
        $this->limit      = null;
139
        $this->orderBy    = [];
140
141
        return $this;
142
    }
143
144
    /**
145
     * Prepare query.
146
     *
147
     * @param object $model
148
     *
149
     * @return object
150
     */
151
    protected function prepareQuery($model)
152
    {
153
        // Set the relationships that should be eager loaded
154
        if (! empty($this->relations)) {
155
            $model = $model->with($this->relations);
156
        }
157
158
        // Add a basic where clause to the query
159
        foreach ($this->where as $where) {
160
            list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null);
161
162
            $model = $model->where($attribute, $operator, $value, $boolean);
163
        }
164
165
        // Add a "where in" clause to the query
166
        foreach ($this->whereIn as $whereIn) {
167
            list($attribute, $values, $boolean, $not) = array_pad($whereIn, 4, null);
168
169
            $model = $model->whereIn($attribute, $values, $boolean, $not);
170
        }
171
172
        // Add a "where not in" clause to the query
173
        foreach ($this->whereNotIn as $whereNotIn) {
174
            list($attribute, $values, $boolean) = array_pad($whereNotIn, 3, null);
175
176
            $model = $model->whereNotIn($attribute, $values, $boolean);
177
        }
178
179
        // Set the "offset" value of the query
180
        if ($this->offset > 0) {
181
            $model = $model->offset($this->offset);
182
        }
183
184
        // Set the "limit" value of the query
185
        if ($this->limit > 0) {
186
            $model = $model->limit($this->limit);
187
        }
188
189
        // Add an "order by" clause to the query.
190
        if (! empty($this->orderBy)) {
191
            list($attribute, $direction) = $this->orderBy;
192
193
            $model = $model->orderBy($attribute, $direction);
194
        }
195
196
        return $model;
197
    }
198
199
    /**
200
     * Set the IoC container instance.
201
     *
202
     * @param \Illuminate\Contracts\Container\Container $container
203
     *
204
     * @return $this
205
     */
206
    public function setContainer(Container $container)
207
    {
208
        $this->container = $container;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Get the IoC container instance or any of it's services.
215
     *
216
     * @param string|null $service
217
     *
218
     * @return object
219
     */
220
    public function getContainer($service = null)
221
    {
222
        return is_null($service) ? ($this->container ?: app()) : ($this->container[$service] ?: app($service));
223
    }
224
225
    /**
226
     * Set the repository identifier.
227
     *
228
     * @param string $repositoryId
229
     *
230
     * @return $this
231
     */
232
    public function setRepositoryId($repositoryId)
233
    {
234
        $this->repositoryId = $repositoryId;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get the repository identifier.
241
     *
242
     * @return string
243
     */
244
    public function getRepositoryId()
245
    {
246
        return $this->repositoryId ?: get_called_class();
247
    }
248
249
    /**
250
     * Set the repository model.
251
     *
252
     * @param string $model
253
     *
254
     * @return $this
255
     */
256
    public function setModel($model)
257
    {
258
        $this->model = $model;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Get the repository model.
265
     *
266
     * @return string
267
     */
268
    public function getModel()
269
    {
270
        $model = $this->getContainer('config')->get('rinvex.repository.models');
271
272
        return $this->model ?: str_replace(['Repositories', 'Repository'], [$model, ''], get_called_class());
273
    }
274
275
    /**
276
     * Set the relationships that should be eager loaded.
277
     *
278
     * @param array $relations
279
     *
280
     * @return $this
281
     */
282
    public function with(array $relations)
283
    {
284
        $this->relations = $relations;
285
286
        return $this;
287
    }
288
289
    /**
290
     * Add a basic where clause to the query.
291
     *
292
     * @param string $attribute
293
     * @param string $operator
294
     * @param mixed  $value
295
     * @param string $boolean
296
     *
297
     * @return $this
298
     */
299
    public function where($attribute, $operator = null, $value = null, $boolean = 'and')
300
    {
301
        // The last `$boolean` expression is intentional to fix list() & array_pad() results
302
        $this->where[] = [$attribute, $operator, $value, $boolean ?: 'and'];
303
304
        return $this;
305
    }
306
307
    /**
308
     * Add a "where in" clause to the query.
309
     *
310
     * @param string $attribute
311
     * @param mixed  $values
312
     * @param string $boolean
313
     * @param bool   $not
314
     *
315
     * @return $this
316
     */
317
    public function whereIn($attribute, $values, $boolean = 'and', $not = false)
318
    {
319
        // The last `$boolean` & `$not` expressions are intentional to fix list() & array_pad() results
320
        $this->whereIn[] = [$attribute, $values, $boolean ?: 'and', (bool) $not];
321
322
        return $this;
323
    }
324
325
    /**
326
     * Add a "where not in" clause to the query.
327
     *
328
     * @param string $attribute
329
     * @param mixed  $values
330
     * @param string $boolean
331
     *
332
     * @return $this
333
     */
334
    public function whereNotIn($attribute, $values, $boolean = 'and')
335
    {
336
        // The last `$boolean` expression is intentional to fix list() & array_pad() results
337
        $this->whereNotIn[] = [$attribute, $values, $boolean ?: 'and'];
338
339
        return $this;
340
    }
341
342
    /**
343
     * Set the "offset" value of the query.
344
     *
345
     * @param int $offset
346
     *
347
     * @return $this
348
     */
349
    public function offset($offset)
350
    {
351
        $this->offset = $offset;
352
353
        return $this;
354
    }
355
356
    /**
357
     * Set the "limit" value of the query.
358
     *
359
     * @param int $limit
360
     *
361
     * @return $this
362
     */
363
    public function limit($limit)
364
    {
365
        $this->limit = $limit;
366
367
        return $this;
368
    }
369
370
    /**
371
     * Add an "order by" clause to the query.
372
     *
373
     * @param string $attribute
374
     * @param string $direction
375
     *
376
     * @return $this
377
     */
378
    public function orderBy($attribute, $direction = 'asc')
379
    {
380
        $this->orderBy = [$attribute, $direction];
381
382
        return $this;
383
    }
384
385
    /**
386
     * Dynamically pass missing static methods to the model.
387
     *
388
     * @param $method
389
     * @param $parameters
390
     *
391
     * @return mixed
392
     */
393
    public static function __callStatic($method, $parameters)
394
    {
395
        return call_user_func_array([new static(), $method], $parameters);
396
    }
397
398
    /**
399
     * Dynamically pass missing methods to the model.
400
     *
401
     * @param string $method
402
     * @param array  $parameters
403
     *
404
     * @return mixed
405
     */
406
    public function __call($method, $parameters)
407
    {
408
        $model = $this->model;
409
410
        return call_user_func_array([$model, $method], $parameters);
411
    }
412
}
413