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 (#190)
by Freek
03:24
created

ResponseCache::cacheResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
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;
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(
58
            $this->hasher->getHashFor($request),
59
            $response,
60
            $lifetimeInSeconds,
61
        );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
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))
70
            : false;
71
    }
72
73
    public function getCachedResponseFor(Request $request): Response
74
    {
75
        return $this->cache->get($this->hasher->getHashFor($request));
76
    }
77
78
    /**
79
     * @deprecated Use the new clear method, this is just an alias.
80
     */
81
    public function flush()
82
    {
83
        $this->clear();
84
    }
85
86
    public function clear()
87
    {
88
        $this->cache->clear();
89
    }
90
91
    protected function addCachedHeader(Response $response): Response
92
    {
93
        $clonedResponse = clone $response;
94
95
        $clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s'));
96
97
        return $clonedResponse;
98
    }
99
100
    /**
101
     * @param string|array $uris
102
     *
103
     * @return \Spatie\ResponseCache\ResponseCache
104
     */
105
    public function forget($uris): self
106
    {
107
        $uris = is_array($uris) ? $uris : func_get_args();
108
109
        collect($uris)->each(function ($uri) {
110
            $request = Request::create(url($uri));
111
            $hash = $this->hasher->getHashFor($request);
112
113
            if ($this->cache->has($hash)) {
114
                $this->cache->forget($hash);
115
            }
116
        });
117
118
        return $this;
119
    }
120
}
121