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:50
created

ResponseCache::getCachedCsrfTokenFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\ResponseCache;
4
5
use Illuminate\Http\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Spatie\ResponseCache\CacheProfiles\CacheProfile;
8
9
class ResponseCache
10
{
11
    /** @var \Spatie\ResponseCache\ResponseCache */
12
    protected $cache;
13
14
    /** @var \Spatie\ResponseCache\RequestHasher */
15
    protected $hasher;
16
17
    /** @var \Spatie\ResponseCache\CacheProfiles\CacheProfile */
18
    protected $cacheProfile;
19
20
    public function __construct(ResponseCacheRepository $cache, RequestHasher $hasher, CacheProfile $cacheProfile)
21
    {
22
        $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...
23
        $this->hasher = $hasher;
24
        $this->cacheProfile = $cacheProfile;
25
    }
26
27
    public function enabled(Request $request): bool
28
    {
29
        return $this->cacheProfile->enabled($request);
30
    }
31
32
    public function shouldCache(Request $request, Response $response): bool
33
    {
34
        if ($request->attributes->has('responsecache.doNotCache')) {
35
            return false;
36
        }
37
38
        if (! $this->cacheProfile->shouldCacheRequest($request)) {
39
            return false;
40
        }
41
42
        return $this->cacheProfile->shouldCacheResponse($response);
43
    }
44
45
    public function cacheResponse(Request $request, Response $response, $lifetimeInSeconds = null): Response
46
    {
47
        if (config('responsecache.add_cache_time_header')) {
48
            $response = $this->addCachedHeader($response);
49
        }
50
51
        $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...
52
            $this->hasher->getHashFor($request),
53
            $response,
54
            ($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request)
55
        );
56
57
        $this->cache->putToken(
0 ignored issues
show
Bug introduced by
The method putToken() 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
            csrf_token(),
60
            ($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request)
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 getCachedCsrfTokenFor(Request $request): string
79
    {
80
        return $this->cache->getToken($this->hasher->getHashFor($request));
0 ignored issues
show
Bug introduced by
The method getToken() 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
    /**
84
     * @deprecated Use the new clear method, this is just an alias.
85
     */
86
    public function flush()
87
    {
88
        $this->clear();
89
    }
90
91
    public function clear()
92
    {
93
        $this->cache->clear();
94
    }
95
96
    protected function addCachedHeader(Response $response): Response
97
    {
98
        $clonedResponse = clone $response;
99
100
        $clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s'));
101
102
        return $clonedResponse;
103
    }
104
105
    /**
106
     * @param string|array $uris
107
     *
108
     * @return \Spatie\ResponseCache\ResponseCache
109
     */
110
    public function forget($uris): self
111
    {
112
        $uris = is_array($uris) ? $uris : func_get_args();
113
114
        collect($uris)->each(function ($uri) {
115
            $request = Request::create($uri);
116
            $hash = $this->hasher->getHashFor($request);
117
118
            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...
119
                $this->cache->forget($hash);
120
            }
121
        });
122
123
        return $this;
124
    }
125
}
126