Passed
Push — main ( a4aaf2...4acb8c )
by Mohammad
05:43 queued 02:51
created

CachePerRequest::refresh()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Shamaseen\Repository\Utility\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Psr\SimpleCache\InvalidArgumentException;
7
8
/**
9
 * @method Builder disableCache()
10
 * @method Builder enableCache()
11
 * @method Builder clearCache()
12
 */
13
trait CachePerRequest
14
{
15
    public bool $requestCacheEnabled = true;
16
    public RequestCache $requestCache;
17
18
    public function __construct(array $attributes = [])
19
    {
20
        $this->requestCache = new RequestCache($this->getRequestCacheKey());
21
        parent::__construct($attributes);
22
    }
23
24
    public function scopeDisableCache(Builder $query): Builder
25
    {
26
        $this->requestCacheEnabled = false;
27
28
        return $query;
29
    }
30
31
    public function scopeEnableCache(Builder $query): Builder
32
    {
33
        $this->requestCacheEnabled = true;
34
35
        return $query;
36
    }
37
38
    /**
39
     * @throws InvalidArgumentException
40
     */
41
    public function scopeClearCache(Builder $query): Builder
42
    {
43
        $this->requestCache->clear();
44
45
        return $query;
46
    }
47
48
    public function getRequestCacheKey(): string
49
    {
50
        return 'repository-cache';
51
    }
52
53
    public function getConnection(): ConnectionProxy
54
    {
55
        return new ConnectionProxy(static::resolveConnection($this->getConnectionName()), $this);
0 ignored issues
show
Bug introduced by
The method getConnectionName() does not exist on Shamaseen\Repository\Uti...\Models\CachePerRequest. Did you maybe mean getConnection()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        return new ConnectionProxy(static::resolveConnection($this->/** @scrutinizer ignore-call */ getConnectionName()), $this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
    }
57
58
    public function refresh(): self
59
    {
60
        $wasEnabled = $this->requestCacheEnabled;
61
62
        $this->disableCache();
63
64
        $result = parent::refresh();
65
66
        if ($wasEnabled) {
67
            $this->enableCache();
68
        }
69
70
        return $result;
71
    }
72
}
73