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 (#278)
by Matthew
08:28
created

CacheResponse::getLifetime()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
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
    /**
17
     * @var ResponseCache
18
     */
19
    protected $responseCache;
20
21
    public function __construct(ResponseCache $responseCache)
22
    {
23
        $this->responseCache = $responseCache;
24
    }
25
26
    public function handle(Request $request, Closure $next, ...$args): Response
27
    {
28
        $lifetimeInSeconds = $this->getLifetime($args);
29
        $tags = $this->getTags($args);
30
31
        if ($this->responseCache->enabled($request)) {
32
            if ($this->responseCache->hasBeenCached($request, $tags)) {
33
                event(new ResponseCacheHit($request));
34
35
                $response = $this->responseCache->getCachedResponseFor($request, $tags);
36
37
                $this->getReplacers()->each(function (Replacer $replacer) use ($response) {
38
                    $replacer->replaceInCachedResponse($response);
39
                });
40
41
                return $response;
42
            }
43
        }
44
45
        $response = $next($request);
46
47
        if ($this->responseCache->enabled($request)) {
48
            if ($this->responseCache->shouldCache($request, $response)) {
49
                $this->makeReplacementsAndCacheResponse($request, $response, $lifetimeInSeconds, $tags);
50
            }
51
        }
52
53
        event(new CacheMissed($request));
54
55
        return $response;
56
    }
57
58
    protected function makeReplacementsAndCacheResponse(
59
        Request $request,
60
        Response $response,
61
        ?int $lifetimeInSeconds = null,
62
        array $tags = []
63
    ): void {
64
        $cachedResponse = clone $response;
65
66
        $this->getReplacers()->each(function (Replacer $replacer) use ($cachedResponse) {
67
            $replacer->prepareResponseToCache($cachedResponse);
68
        });
69
70
        $this->responseCache->cacheResponse($request, $cachedResponse, $lifetimeInSeconds, $tags);
71
    }
72
73
    protected function getReplacers(): Collection
74
    {
75
        return collect(config('responsecache.replacers'))
76
            ->map(function (string $replacerClass) {
77
                return app($replacerClass);
78
            });
79
    }
80
81
    protected function getLifetime(array $args): ?int
82
    {
83
        if (count($args) >= 1 && is_numeric($args[0])) {
84
            return (int) $args[0];
85
        }
86
87
        return null;
88
    }
89
90
    protected function getTags(array $args): array
91
    {
92
        $tags = $args;
93
94
        if (count($args) >= 1 && is_numeric($args[0])) {
95
            $tags = array_slice($args, 1);
96
        }
97
98
        return array_filter($tags);
99
    }
100
}
101