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
Push — master ( 02f07d...0e7d0b )
by Freek
01:47
created

CacheResponse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 28 5
A makeReplacementsAndCacheResponse() 0 13 1
A getReplacers() 0 7 1
1
<?php
2
3
namespace Spatie\ResponseCache\Middlewares;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
use Spatie\ResponseCache\ResponseCache;
9
use Spatie\ResponseCache\Events\CacheMissed;
10
use Spatie\ResponseCache\Replacers\Replacer;
11
use Symfony\Component\HttpFoundation\Response;
12
use Spatie\ResponseCache\Events\ResponseCacheHit;
13
14
class CacheResponse
15
{
16
    /** @var \Spatie\ResponseCache\ResponseCache */
17
    protected $responseCache;
18
19
    public function __construct(ResponseCache $responseCache)
20
    {
21
        $this->responseCache = $responseCache;
22
    }
23
24
    public function handle(Request $request, Closure $next, $lifetimeInSeconds = null): Response
25
    {
26
        if ($this->responseCache->enabled($request)) {
27
            if ($this->responseCache->hasBeenCached($request)) {
28
                event(new ResponseCacheHit($request));
29
30
                $response = $this->responseCache->getCachedResponseFor($request);
31
32
                $this->getReplacers()->each(function (Replacer $replacer) use ($response) {
33
                    $replacer->replaceInCachedResponse($response);
34
                });
35
36
                return $response;
37
            }
38
        }
39
40
        $response = $next($request);
41
42
        if ($this->responseCache->enabled($request)) {
43
            if ($this->responseCache->shouldCache($request, $response)) {
44
                $this->makeReplacementsAndCacheResponse($request, $response, $lifetimeInSeconds);
45
            }
46
        }
47
48
        event(new CacheMissed($request));
49
50
        return $response;
51
    }
52
53
    protected function makeReplacementsAndCacheResponse(
54
        Request $request,
55
        Response $response,
56
        $lifetimeInSeconds = null
57
    ): void {
58
        $cachedResponse = clone $response;
59
60
        $this->getReplacers()->each(function (Replacer $replacer) use ($cachedResponse) {
61
            $replacer->prepareResponseToCache($cachedResponse);
62
        });
63
64
        $this->responseCache->cacheResponse($request, $cachedResponse, $lifetimeInSeconds);
65
    }
66
67
    protected function getReplacers(): Collection
68
    {
69
        return collect(config('responsecache.replacers'))
70
            ->map(function (string $replacerClass) {
71
                return app($replacerClass);
72
            });
73
    }
74
}
75