EloquentRepository::cacheTTLValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\EloquentRepository;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Contracts\Cache\Factory as Cache;
9
use Illuminate\Contracts\Foundation\Application;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Orkhanahmadov\EloquentRepository\Repository\Criteria;
12
use Illuminate\Contracts\Container\BindingResolutionException;
13
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Repository;
14
use Orkhanahmadov\EloquentRepository\Repository\Concerns\CreatesEntity;
15
use Orkhanahmadov\EloquentRepository\Repository\Concerns\DeletesEntity;
16
use Orkhanahmadov\EloquentRepository\Repository\Concerns\SelectsEntity;
17
use Orkhanahmadov\EloquentRepository\Repository\Concerns\UpdatesEntity;
18
19
class EloquentRepository implements Repository
20
{
21
    use SelectsEntity;
22
    use CreatesEntity;
23
    use UpdatesEntity;
24
    use DeletesEntity;
25
26
    /**
27
     * @var Application
28
     */
29
    private $application;
30
    /**
31
     * @var Cache
32
     */
33
    protected $cache;
34
    /**
35
     * @var int
36
     */
37
    protected $cacheTTL = 3600;
38
    /**
39
     * @var string|null
40
     */
41
    protected $entity = null;
42
    /**
43
     * @var Builder|Model
44
     */
45
    protected $model;
46
47
    /**
48
     * EloquentRepository constructor.
49
     *
50
     * @param Application $application
51
     * @param Cache $cache
52
     * @throws BindingResolutionException
53
     */
54
    public function __construct(Application $application, Cache $cache)
55
    {
56
        $this->application = $application;
57
        $this->cache = $cache;
58
59
        if ($this->entity) {
60
            $this->resolveEntity();
61
        }
62
    }
63
64
    /**
65
     * @param string $entity
66
     *
67
     * @return self
68
     *
69
     * @throws BindingResolutionException
70
     */
71
    public function setEntity($entity): self
72
    {
73
        $this->entity = $entity;
74
        $this->resolveEntity();
75
76
        return $this;
77
    }
78
79
    /**
80
     * Sets listed criteria for entity.
81
     *
82
     * @param mixed ...$criteria
83
     *
84
     * @return self
85
     */
86
    public function withCriteria(...$criteria): self
87
    {
88
        $criteria = Arr::flatten($criteria);
89
90
        foreach ($criteria as $criterion) {
91
            /* @var Criteria\Criterion $criterion */
92
            $this->model = $criterion->apply($this->model);
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * Defines cache key.
100
     *
101
     * @return string
102
     */
103
    public function cacheKey(): string
104
    {
105
        return $this->model->getTable();
106
    }
107
108
    /**
109
     * Removes cache for model.
110
     *
111
     * @param Model $model
112
     */
113
    public function invalidateCache($model): void
114
    {
115
        $this->cache->forget(
116
            $this->cacheKey() . '.*'
117
        );
118
        $this->cache->forget(
119
            $this->cacheKey() . '.' . $model->id
120
        );
121
    }
122
123
    /**
124
     * Resolves entity.
125
     *
126
     * @throws BindingResolutionException
127
     */
128
    private function resolveEntity(): void
129
    {
130
        $this->model = $this->application->make($this->entity);
131
    }
132
133
    /**
134
     * Get cache time-to-live value from property or method if available.
135
     *
136
     * @return int
137
     */
138
    private function cacheTTLValue(): int
0 ignored issues
show
Unused Code introduced by
The method cacheTTLValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
139
    {
140
        if (method_exists($this, 'cacheTTL')) {
141
            return $this->cacheTTL();
142
        }
143
144
        return $this->cacheTTL;
145
    }
146
147
    /**
148
     * Throws ModelNotFoundException exception.
149
     *
150
     * @param array|int $ids
151
     */
152
    private function throwModelNotFoundException($ids = [])
0 ignored issues
show
Unused Code introduced by
The method throwModelNotFoundException() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
153
    {
154
        throw (new ModelNotFoundException())->setModel(
155
            get_class($this->model->getModel()),
156
            $ids
157
        );
158
    }
159
}
160