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.

QueryCacheService   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 196
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllQueryCacheStore() 0 4 1
A getDuplicateQueryCacheStore() 0 4 1
A getAllQueryCachePrefix() 0 4 1
A getDuplicateQueryCachePrefix() 0 4 1
A shouldCacheAllQueries() 0 4 1
A shouldCacheDuplicateQueries() 0 4 1
A cacheAllQueriesForeverType() 0 4 1
A cacheOnlyDuplicateQueriesOnceType() 0 4 1
A enableQueryCache() 0 4 1
A disableQueryCache() 0 4 1
A canCacheQueries() 0 4 2
A flushQueryCache() 0 10 3
B clearQueryCache() 0 23 9
1
<?php
2
3
namespace Neurony\QueryCache\Services;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use Neurony\QueryCache\Contracts\QueryCacheServiceContract;
8
use Neurony\QueryCache\Helpers\RelationHelper;
9
use Neurony\QueryCache\Traits\IsCacheable;
10
11
class QueryCacheService implements QueryCacheServiceContract
12
{
13
    /**
14
     * The model the cache should run on.
15
     * The model should use the IsCacheable trait for the whole process to work.
16
     *
17
     * @var Model
18
     */
19
    protected $model;
20
21
    /**
22
     * Flag whether or not to cache queries forever.
23
     *
24
     * @var bool
25
     */
26
    protected $cacheAllQueries = true;
27
28
    /**
29
     * Flag whether or not to cache only duplicate queries for the current request.
30
     *
31
     * @var bool
32
     */
33
    protected $cacheDuplicateQueries = true;
34
35
    /**
36
     * The query cache types available.
37
     *
38
     * @const
39
     */
40
    const TYPE_CACHE_ALL_QUERIES_FOREVER = 1;
41
    const TYPE_CACHE_ONLY_DUPLICATE_QUERIES_ONCE = 2;
42
43
    /**
44
     * Get the cache store to be used when caching queries forever.
45
     *
46
     * @return string
47
     */
48
    public function getAllQueryCacheStore(): string
49
    {
50
        return config('query-cache.all.store', 'array');
51
    }
52
53
    /**
54
     * Get the cache store to be used when caching only duplicate queries.
55
     *
56
     * @return string
57
     */
58
    public function getDuplicateQueryCacheStore(): string
59
    {
60
        return config('query-cache.duplicate.store', 'array');
61
    }
62
63
    /**
64
     * Get the cache prefix to be appended to the specific cache tag for the model instance.
65
     * Used when caching queries forever.
66
     *
67
     * @return string
68
     */
69
    public function getAllQueryCachePrefix(): string
70
    {
71
        return config('query-cache.all.prefix', 'cache.all_query');
72
    }
73
74
    /**
75
     * Get the cache prefix to be appended to the specific cache tag for the model instance.
76
     * Used when caching only duplicate queries.
77
     *
78
     * @return string
79
     */
80
    public function getDuplicateQueryCachePrefix(): string
81
    {
82
        return config('query-cache.duplicate.prefix', 'cache.duplicate_query');
83
    }
84
85
    /**
86
     * Verify if forever query caching should run.
87
     *
88
     * @return bool
89
     */
90
    public function shouldCacheAllQueries(): bool
91
    {
92
        return config('query-cache.all.enabled', false) === true;
93
    }
94
95
    /**
96
     * Verify if caching of duplicate queries should run.
97
     *
98
     * @return bool
99
     */
100
    public function shouldCacheDuplicateQueries(): bool
101
    {
102
        return config('query-cache.duplicate.enabled', false) === true;
103
    }
104
105
    /**
106
     * Get the "cache all queries forever" caching type.
107
     *
108
     * @return int
109
     */
110
    public function cacheAllQueriesForeverType(): int
111
    {
112
        return static::TYPE_CACHE_ALL_QUERIES_FOREVER;
113
    }
114
115
    /**
116
     * Get the "cache only duplicate queries once" caching type.
117
     *
118
     * @return int
119
     */
120
    public function cacheOnlyDuplicateQueriesOnceType(): int
121
    {
122
        return static::TYPE_CACHE_ONLY_DUPLICATE_QUERIES_ONCE;
123
    }
124
125
    /**
126
     * Enable caching of database queries for the current request.
127
     * This is generally useful when working with rolled back database migrations.
128
     *
129
     * @return void
130
     */
131
    public function enableQueryCache(): void
132
    {
133
        $this->cacheAllQueries = $this->cacheDuplicateQueries = true;
134
    }
135
136
    /**
137
     * Disable caching of database queries for the current request.
138
     * This is generally useful when working with rolled back database migrations.
139
     *
140
     * @return void
141
     */
142
    public function disableQueryCache(): void
143
    {
144
        $this->cacheAllQueries = $this->cacheDuplicateQueries = false;
145
    }
146
147
    /**
148
     * Verify if either forever query caching or duplicate query caching are enabled.
149
     *
150
     * @return bool
151
     */
152
    public function canCacheQueries(): bool
153
    {
154
        return $this->cacheAllQueries === true || $this->cacheDuplicateQueries === true;
155
    }
156
157
    /**
158
     * Flush all the query cache for the specified store.
159
     * Please note that this does not happen only for one caching type, but for all.
160
     *
161
     * @throws Exception
162
     */
163
    public function flushQueryCache(): void
164
    {
165
        if (! self::canCacheQueries()) {
166
            return;
167
        }
168
169
        if (self::shouldCacheAllQueries()) {
170
            cache()->store(self::getAllQueryCacheStore())->flush();
171
        }
172
    }
173
174
    /**
175
     * Flush the query cache from the store only for the tag corresponding to the model instance.
176
     * If something fails, flush all existing cache for the specified store.
177
     * This way, it's guaranteed that nothing will be left out of sync at the database level.
178
     *
179
     * @param Model $model
180
     * @return void
181
     * @throws Exception
182
     */
183
    public function clearQueryCache(Model $model): void
184
    {
185
        if (! ((self::shouldCacheAllQueries() || self::shouldCacheDuplicateQueries()) && self::canCacheQueries())) {
186
            return;
187
        }
188
189
        try {
190
            $this->model = $model;
191
192
            cache()->store(self::getAllQueryCacheStore())->tags($this->model->getQueryCacheTag())->flush();
193
194
            foreach (RelationHelper::getModelRelations($this->model) as $relation => $attributes) {
195
                if (
196
                    ($related = $attributes['model'] ?? null) && $related instanceof Model &&
197
                    array_key_exists(IsCacheable::class, class_uses($related))
198
                ) {
199
                    cache()->store(self::getAllQueryCacheStore())->tags($related->getQueryCacheTag())->flush();
200
                }
201
            }
202
        } catch (Exception $e) {
203
            self::flushQueryCache();
204
        }
205
    }
206
}
207