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 (#217)
by
unknown
01:18
created

ResponseCache::cacheResponse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
namespace Spatie\ResponseCache;
4
5
use Illuminate\Http\Request;
6
use Spatie\ResponseCache\Hasher\DefaultHasher;
7
use Spatie\ResponseCache\Hasher\RequestHasher;
8
use Symfony\Component\HttpFoundation\Response;
9
use Spatie\ResponseCache\CacheProfiles\CacheProfile;
10
11
class ResponseCache
12
{
13
    /** @var \Spatie\ResponseCache\ResponseCache */
14
    protected $cache;
15
16
    /** @var RequestHasher */
17
    protected $hasher;
18
19
    /** @var \Spatie\ResponseCache\CacheProfiles\CacheProfile */
20
    protected $cacheProfile;
21
22
    public function __construct(ResponseCacheRepository $cache, RequestHasher $hasher, CacheProfile $cacheProfile)
23
    {
24
        $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...
25
        $this->hasher = $hasher;
26
        $this->cacheProfile = $cacheProfile;
27
    }
28
29
    public function enabled(Request $request): bool
30
    {
31
        return $this->cacheProfile->enabled($request);
32
    }
33
34
    public function shouldCache(Request $request, Response $response): bool
35
    {
36
        if ($request->attributes->has('responsecache.doNotCache')) {
37
            return false;
38
        }
39
40
        if (! $this->cacheProfile->shouldCacheRequest($request)) {
41
            return false;
42
        }
43
44
        return $this->cacheProfile->shouldCacheResponse($response);
45
    }
46
47
    public function cacheResponse(Request $request, Response $response, $lifetimeInSeconds = null): Response
48
    {
49
        if (config('responsecache.add_cache_time_header')) {
50
            $response = $this->addCachedHeader($response);
51
        }
52
53
        $lifetimeInSeconds = $lifetimeInSeconds
54
            ? (int)$lifetimeInSeconds
55
            : $this->cacheProfile->cacheRequestUntil($request);
56
57
        $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...
58
            $this->hasher->getHashFor($request),
59
            $response,
60
            $lifetimeInSeconds
61
        );
62
63
        return $response;
64
    }
65
66
    public function hasBeenCached(Request $request): bool
67
    {
68
        return config('responsecache.enabled')
69
            ? $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...
70
            : false;
71
    }
72
73
    public function getCachedResponseFor(Request $request): Response
74
    {
75
        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...
76
    }
77
78
    public function clear()
79
    {
80
        $this->cache->clear();
81
    }
82
83
    protected function addCachedHeader(Response $response): Response
84
    {
85
        $clonedResponse = clone $response;
86
87
        $clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s'));
88
89
        return $clonedResponse;
90
    }
91
92
    /**
93
     * @param string|array $uris
94
     *
95
     * @return \Spatie\ResponseCache\ResponseCache
96
     */
97
    public function forget($uris): self
98
    {
99
        $uris = is_array($uris) ? $uris : func_get_args();
100
101
        collect($uris)->each(function ($uri) {
102
            $request = Request::create(url($uri));
0 ignored issues
show
Bug introduced by
It seems like url($uri) targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Symfony\Component\HttpFoundation\Request::create() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
            $hash = $this->hasher->getHashFor($request);
104
105
            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...
106
                $this->cache->forget($hash);
107
            }
108
        });
109
110
        return $this;
111
    }
112
}
113