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 ( 6122da...bfe9a2 )
by Freek
10:18 queued 11s
created

CacheResponse::__construct()   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\Middlewares;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
use Spatie\ResponseCache\Events\CacheMissed;
9
use Spatie\ResponseCache\Events\ResponseCacheHit;
10
use Spatie\ResponseCache\Replacers\Replacer;
11
use Spatie\ResponseCache\ResponseCache;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class CacheResponse
15
{
16
    protected ResponseCache $responseCache;
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 T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
17
18
    public function __construct(ResponseCache $responseCache)
19
    {
20
        $this->responseCache = $responseCache;
21
    }
22
23
    public function handle(Request $request, Closure $next, ...$args): Response
24
    {
25
        $lifetimeInSeconds = $this->getLifetime($args);
26
        $tags = $this->getTags($args);
27
28
        if ($this->responseCache->enabled($request)) {
29
            if ($this->responseCache->hasBeenCached($request, $tags)) {
30
                event(new ResponseCacheHit($request));
31
32
                $response = $this->responseCache->getCachedResponseFor($request, $tags);
33
34
                $this->getReplacers()->each(function (Replacer $replacer) use ($response) {
35
                    $replacer->replaceInCachedResponse($response);
36
                });
37
38
                return $response;
39
            }
40
        }
41
42
        $response = $next($request);
43
44
        if ($this->responseCache->enabled($request)) {
45
            if ($this->responseCache->shouldCache($request, $response)) {
46
                $this->makeReplacementsAndCacheResponse($request, $response, $lifetimeInSeconds, $tags);
47
            }
48
        }
49
50
        event(new CacheMissed($request));
51
52
        return $response;
53
    }
54
55
    protected function makeReplacementsAndCacheResponse(
56
        Request $request,
57
        Response $response,
58
        ?int $lifetimeInSeconds = null,
59
        array $tags = []
60
    ): void {
61
        $cachedResponse = clone $response;
62
63
        $this->getReplacers()->each(function (Replacer $replacer) use ($cachedResponse) {
64
            $replacer->prepareResponseToCache($cachedResponse);
65
        });
66
67
        $this->responseCache->cacheResponse($request, $cachedResponse, $lifetimeInSeconds, $tags);
68
    }
69
70
    protected function getReplacers(): Collection
71
    {
72
        return collect(config('responsecache.replacers'))
73
            ->map(function (string $replacerClass) {
74
                return app($replacerClass);
75
            });
76
    }
77
78
    protected function getLifetime(array $args): ?int
79
    {
80
        if (count($args) >= 1 && is_numeric($args[0])) {
81
            return (int) $args[0];
82
        }
83
84
        return null;
85
    }
86
87
    protected function getTags(array $args): array
88
    {
89
        $tags = $args;
90
91
        if (count($args) >= 1 && is_numeric($args[0])) {
92
            $tags = array_slice($args, 1);
93
        }
94
95
        return array_filter($tags);
96
    }
97
}
98