Passed
Pull Request — main (#1)
by Mohammad
02:54
created

CachePerRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 13
c 2
b 0
f 1
dl 0
loc 43
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConnection() 0 3 1
A scopeDisableCache() 0 5 1
A scopeClearCache() 0 5 1
A getRequestCacheKey() 0 3 1
A scopeEnableCache() 0 5 1
1
<?php
2
3
namespace Shamaseen\Repository\Utility\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Psr\SimpleCache\InvalidArgumentException;
7
8
trait CachePerRequest
9
{
10
    public bool $requestCacheEnabled = true;
11
    public RequestCache $requestCache;
12
13
    public function __construct(array $attributes = [])
14
    {
15
        $this->requestCache = new RequestCache($this->getRequestCacheKey());
16
        parent::__construct($attributes);
17
    }
18
19
    public function scopeDisableCache(Builder $query): Builder
20
    {
21
        $this->requestCacheEnabled = false;
22
23
        return $query;
24
    }
25
26
    public function scopeEnableCache(Builder $query): Builder
27
    {
28
        $this->requestCacheEnabled = true;
29
30
        return $query;
31
    }
32
33
    /**
34
     * @throws InvalidArgumentException
35
     */
36
    public function scopeClearCache(Builder $query): Builder
37
    {
38
        $this->requestCache->clear();
39
40
        return $query;
41
    }
42
43
    public function getRequestCacheKey(): string
44
    {
45
        return 'repository-cache';
46
    }
47
48
    public function getConnection()
49
    {
50
        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

50
        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...
51
    }
52
}
53