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.
Completed
Pull Request — master (#180)
by Rias
01:33
created

ResponseCache::hasBeenCached()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\ResponseCache;
4
5
use Illuminate\Http\Request;
6
use Spatie\ResponseCache\Exceptions\InvalidReplacer;
7
use Symfony\Component\HttpFoundation\Response;
8
use Spatie\ResponseCache\CacheProfiles\CacheProfile;
9
10
class ResponseCache
11
{
12
    /** @var \Spatie\ResponseCache\ResponseCache */
13
    protected $cache;
14
15
    /** @var \Spatie\ResponseCache\RequestHasher */
16
    protected $hasher;
17
18
    /** @var \Spatie\ResponseCache\CacheProfiles\CacheProfile */
19
    protected $cacheProfile;
20
21
    public function __construct(ResponseCacheRepository $cache, RequestHasher $hasher, CacheProfile $cacheProfile)
22
    {
23
        $this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache of type object<Spatie\ResponseCa...esponseCacheRepository> is incompatible with the declared type object<Spatie\ResponseCache\ResponseCache> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
        $this->hasher = $hasher;
25
        $this->cacheProfile = $cacheProfile;
26
    }
27
28
    public function enabled(Request $request): bool
29
    {
30
        return $this->cacheProfile->enabled($request);
31
    }
32
33
    public function shouldCache(Request $request, Response $response): bool
34
    {
35
        if ($request->attributes->has('responsecache.doNotCache')) {
36
            return false;
37
        }
38
39
        if (! $this->cacheProfile->shouldCacheRequest($request)) {
40
            return false;
41
        }
42
43
        return $this->cacheProfile->shouldCacheResponse($response);
44
    }
45
46
    public function cacheResponse(Request $request, Response $response, $lifetimeInSeconds = null): Response
47
    {
48
        if (config('responsecache.add_cache_time_header')) {
49
            $response = $this->addCachedHeader($response);
50
        }
51
52
        $this->cache->put(
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Spatie\ResponseCache\ResponseCache>.

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...
53
            $this->hasher->getHashFor($request),
54
            $response,
55
            ($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request)
56
        );
57
58
        foreach ($this->cacheProfile->replacers($request) as $key => $callback) {
59
            $replacer = new Replacer($key, $callback);
60
61
            $this->cache->putKey(
0 ignored issues
show
Bug introduced by
The method putKey() does not seem to exist on object<Spatie\ResponseCache\ResponseCache>.

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...
62
                $this->hasher->getHashFor($request).$replacer->getKey(),
63
                $replacer->getValue(),
64
                ($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request)
65
            );
66
        }
67
68
        return $response;
69
    }
70
71
    public function hasBeenCached(Request $request): bool
72
    {
73
        return config('responsecache.enabled')
74
            ? $this->cache->has($this->hasher->getHashFor($request))
0 ignored issues
show
Bug introduced by
The method has() does not seem to exist on object<Spatie\ResponseCache\ResponseCache>.

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...
75
            : false;
76
    }
77
78
    public function getCachedResponseFor(Request $request): Response
79
    {
80
        return $this->cache->get($this->hasher->getHashFor($request));
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Spatie\ResponseCache\ResponseCache>.

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...
81
    }
82
83
    public function getCachedKeyFor(Request $request, string $key): string
84
    {
85
        return $this->cache->getKey($this->hasher->getHashFor($request).$key);
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Spatie\ResponseCache\ResponseCache>.

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...
86
    }
87
88
    /**
89
     * @deprecated Use the new clear method, this is just an alias.
90
     */
91
    public function flush()
92
    {
93
        $this->clear();
94
    }
95
96
    public function clear()
97
    {
98
        $this->cache->clear();
99
    }
100
101
    protected function addCachedHeader(Response $response): Response
102
    {
103
        $clonedResponse = clone $response;
104
105
        $clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s'));
106
107
        return $clonedResponse;
108
    }
109
110
    /**
111
     * @param string|array $uris
112
     *
113
     * @return \Spatie\ResponseCache\ResponseCache
114
     */
115
    public function forget($uris): self
116
    {
117
        $uris = is_array($uris) ? $uris : func_get_args();
118
119
        collect($uris)->each(function ($uri) {
120
            $request = Request::create($uri);
121
            $hash = $this->hasher->getHashFor($request);
122
123
            if ($this->cache->has($hash)) {
0 ignored issues
show
Bug introduced by
The method has() does not seem to exist on object<Spatie\ResponseCache\ResponseCache>.

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...
124
                $this->cache->forget($hash);
125
            }
126
        });
127
128
        return $this;
129
    }
130
}
131