GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CacheService   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 3 Features 4
Metric Value
c 10
b 3
f 4
dl 0
loc 224
wmc 20
lcom 1
cbo 6
ccs 50
cts 50
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A update() 0 6 1
A refresh() 0 8 1
A cacheKey() 0 14 4
A cache() 0 4 2
A store() 0 10 1
A delete() 0 6 1
A forget() 0 10 2
A __call() 0 4 1
A retrieveOrStore() 0 6 2
A retrieve() 0 8 2
A has() 0 4 1
1
<?php
2
3
namespace SebastianBerc\Repositories\Services;
4
5
use Illuminate\Contracts\Container\Container as Application;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model as Eloquent;
8
use SebastianBerc\Repositories\Contracts\ServiceInterface;
9
use SebastianBerc\Repositories\Repository;
10
11
/**
12
 * Class CacheService.
13
 *
14
 * @author    Sebastian Berć <[email protected]>
15
 * @copyright Copyright (c) Sebastian Berć
16
 */
17
class CacheService implements ServiceInterface
18
{
19
    /**
20
     * Contains instance of repository.
21
     *
22
     * @var Repository
23
     */
24
    protected $repository;
25
26
    /**
27
     * Contains cache repository.
28
     *
29
     * @var \Illuminate\Contracts\Cache\Repository
30
     */
31
    protected $cache;
32
33
    /**
34
     * Contains tag for repository model.
35
     *
36
     * @var string
37
     */
38
    protected $tag;
39
40
    /**
41
     * Contains cache life time.
42
     *
43
     * @var int
44
     */
45
    protected $lifetime;
46
47
    /**
48
     * Contains cache key for given action.
49
     *
50
     * @var string
51
     */
52
    protected $cacheKey;
53
54
    /**
55
     * Create a new cache service instance.
56
     *
57
     * @param Application $app
58
     * @param Repository  $repository
59
     */
60 120
    public function __construct(Application $app, Repository $repository)
61
    {
62 120
        $this->repository = $repository;
63 120
        $this->lifetime   = $repository->lifetime ?: 30;
64 120
        $this->tag        = $repository->makeModel()->getTable();
65 120
        $this->cache      = $app->make('cache.store');
66 120
    }
67
68
    /**
69
     * Execute refresh on cache service and update action on database service.
70
     *
71
     * @param int   $identifier
72
     * @param array $attributes
73
     *
74
     * @return mixed
75
     */
76 2
    public function update($identifier, array $attributes = [])
77
    {
78 2
        $this->forget('all', ['columns' => ['*']]);
79
80 2
        return $this->refresh('update', compact('identifier', 'attributes'));
81
    }
82
83
    /**
84
     * Forget, and store new data into cache.
85
     *
86
     * @param string $caller
87
     * @param array  $parameters
88
     *
89
     * @return mixed
90
     */
91 2
    public function refresh($caller, array $parameters = [])
92
    {
93 2
        $cacheKey = $this->cacheKey($caller, $parameters);
94
95 2
        $this->cache()->forget($cacheKey);
96
97 2
        return $this->store($caller, $parameters);
98
    }
99
100
    /**
101
     * Generate and return cache key for caller with specified parameters.
102
     *
103
     * @param string $caller
104
     * @param array  $parameters
105
     *
106
     * @return string
107
     */
108 20
    public function cacheKey($caller, array $parameters = [])
109
    {
110 20
        $parameters = compact('caller', 'parameters');
111
112 20
        if ($this->repository->model instanceof Eloquent || $this->repository->model instanceof Builder) {
113 20
            $parameters['eager'] = md5(serialize($this->repository->with));
114 20
        }
115
116 20
        if ($this->repository->mediator->criteria()->hasCriteria()) {
117 2
            $parameters['criteria'] = md5(serialize($this->repository->mediator->criteria()->getCriterias()));
118 2
        }
119
120 20
        return md5(serialize($parameters));
121
    }
122
123
    /**
124
     * Initialize cache repository with specified tag for given model.
125
     *
126
     * @return \Illuminate\Contracts\Cache\Repository
127
     */
128 20
    protected function cache()
129
    {
130 20
        return method_exists($this->cache, 'tags') ? $this->cache->tags($this->tag) : $this->cache;
131
    }
132
133
    /**
134
     * Store data in cache behind caller with specified parameters.
135
     *
136
     * @param string $caller
137
     * @param array  $parameters
138
     *
139
     * @return mixed
140
     */
141 20
    public function store($caller, array $parameters = [])
142
    {
143 20
        $this->forget('all', ['columns' => ['*']]);
144
145 20
        $cacheKey = $this->cacheKey($caller, $parameters);
146
147 20
        return $this->cache()->remember($cacheKey, $this->lifetime, function () use ($caller, $parameters) {
148 20
            return call_user_func_array([$this->repository->mediator, 'database'], [$caller, $parameters]);
149 20
        });
150
    }
151
152
    /**
153
     * Execute forget on cache service and delete action on database services.
154
     *
155
     * @param int $identifier
156
     *
157
     * @return bool
158
     */
159 2
    public function delete($identifier)
160
    {
161 2
        $this->forget('all', ['columns' => ['*']]);
162
163 2
        return $this->forget('delete', compact('identifier'));
164
    }
165
166
    /**
167
     * Forget data in cache behind caller with specified parameters.
168
     *
169
     * @param string $caller
170
     * @param array  $parameters
171
     *
172
     * @return bool
173
     */
174 20
    public function forget($caller, array $parameters = [])
175
    {
176 20
        $cacheKey = $this->cacheKey($caller, $parameters);
177
178 20
        if ($caller == 'delete') {
179 2
            $this->repository->mediator->database($caller, $parameters);
180 2
        }
181
182 20
        return $this->cache()->forget($cacheKey);
183
    }
184
185
    /**
186
     * Dynamicly call method on cache service.
187
     *
188
     * @param string $caller
189
     * @param array  $parameters
190
     *
191
     * @return mixed
192
     */
193 20
    public function __call($caller, array $parameters = [])
194
    {
195 20
        return $this->retrieveOrStore($caller, $parameters);
196
    }
197
198
    /**
199
     * Retrieve or store and return data from cache.
200
     *
201
     * @param string $caller
202
     * @param array  $parameters
203
     *
204
     * @return mixed
205
     */
206 20
    public function retrieveOrStore($caller, array $parameters = [])
207
    {
208 20
        $cacheKey = $this->cacheKey($caller, $parameters);
209
210 20
        return $this->retrieve($cacheKey) ?: $this->store($caller, $parameters);
211
    }
212
213
    /**
214
     * Return data for given cache key.
215
     *
216
     * @param string $cacheKey
217
     *
218
     * @return bool
219
     */
220 20
    public function retrieve($cacheKey)
221
    {
222 20
        if ($this->has($cacheKey)) {
223 10
            return $this->cache()->get($cacheKey);
224
        }
225
226 20
        return false;
227
    }
228
229
    /**
230
     * Check if specified cache key exists and has data.
231
     *
232
     * @param string $cacheKey
233
     *
234
     * @return bool
235
     */
236 20
    public function has($cacheKey)
237
    {
238 20
        return $this->cache()->has($cacheKey);
239
    }
240
}
241