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
02:53
created

ResponseCache::cacheResponse()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 6
nop 3
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
use Spatie\ResponseCache\Replacers\Replacer;
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
        foreach (config('responsecache.replacers', []) as $replacerClass) {
59
            $replacer = resolve($replacerClass);
60
            if ($replacer instanceof Replacer) {
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
69
        return $response;
70
    }
71
72
    public function hasBeenCached(Request $request): bool
73
    {
74
        return config('responsecache.enabled')
75
            ? $this->cache->has($this->hasher->getHashFor($request))
76
            : false;
77
    }
78
79
    public function getCachedResponseFor(Request $request): Response
80
    {
81
        return $this->cache->get($this->hasher->getHashFor($request));
82
    }
83
84
    public function getCachedKeyFor(Request $request, string $key): string
85
    {
86
        return $this->cache->getKey($this->hasher->getHashFor($request).$key);
87
    }
88
89
    /**
90
     * @deprecated Use the new clear method, this is just an alias.
91
     */
92
    public function flush()
93
    {
94
        $this->clear();
95
    }
96
97
    public function clear()
98
    {
99
        $this->cache->clear();
100
    }
101
102
    protected function addCachedHeader(Response $response): Response
103
    {
104
        $clonedResponse = clone $response;
105
106
        $clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s'));
107
108
        return $clonedResponse;
109
    }
110
111
    /**
112
     * @param string|array $uris
113
     *
114
     * @return \Spatie\ResponseCache\ResponseCache
115
     */
116
    public function forget($uris): self
117
    {
118
        $uris = is_array($uris) ? $uris : func_get_args();
119
120
        collect($uris)->each(function ($uri) {
121
            $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...
122
            $hash = $this->hasher->getHashFor($request);
123
124
            if ($this->cache->has($hash)) {
125
                $this->cache->forget($hash);
126
            }
127
        });
128
129
        return $this;
130
    }
131
}
132