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

ResponseCache::getCachedKeyFor()   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 2
1
<?php
2
3
namespace Spatie\ResponseCache;
4
5
use Illuminate\Http\Request;
6
use Spatie\ResponseCache\Replacers\Replacer;
7
use Symfony\Component\HttpFoundation\Response;
8
use Spatie\ResponseCache\CacheProfiles\CacheProfile;
9
10
class ResponseCache
11
{
12
    /** @var \Spatie\ResponseCache\ResponseCacheRepository */
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;
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(
53
            $this->hasher->getHashFor($request),
54
            $response,
55
            ($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request)
56
        );
57
58
        collect(config('responsecache.replacers', []))->map(function ($replacerClass) {
59
            return resolve($replacerClass);
60
        })->each(function (Replacer $replacer) use ($request, $lifetimeInSeconds) {
61
            $this->cache->putKey(
62
                $this->hasher->getHashFor($request).$replacer->searchFor(),
63
                $replacer->replaceBy(),
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))
75
            : false;
76
    }
77
78
    public function getCachedResponseFor(Request $request): Response
79
    {
80
        return $this->cache->get($this->hasher->getHashFor($request));
81
    }
82
83
    public function getCachedKeyFor(Request $request, string $key): string
84
    {
85
        return $this->cache->getKey($this->hasher->getHashFor($request).$key);
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(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...
121
            $hash = $this->hasher->getHashFor($request);
122
123
            if ($this->cache->has($hash)) {
124
                $this->cache->forget($hash);
125
            }
126
        });
127
128
        return $this;
129
    }
130
}
131