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.

ResponseCacheMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
c 3
b 0
f 0
dl 0
loc 28
ccs 13
cts 15
cp 0.8667
rs 9.4222
cc 5
nc 2
nop 2
crap 5.0592
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use React\Cache\ArrayCache;
8
use React\Cache\CacheInterface;
9
use React\Http\Io\HttpBodyStream;
10
use function React\Promise\resolve;
11
use function RingCentral\Psr7\stream_for;
12
13
final class ResponseCacheMiddleware
14
{
15
    /**
16
     * @var CacheConfigurationInterface
17
     */
18
    private $cacheConfiguration;
19
20
    /**
21
     * @var CacheInterface
22
     */
23
    private $cache;
24
25
    /**
26
     * @param CacheConfigurationInterface $cacheConfiguration
27
     * @param CacheInterface|null         $cache
28
     */
29 1
    public function __construct(CacheConfigurationInterface $cacheConfiguration, CacheInterface $cache = null)
30
    {
31 1
        $this->cacheConfiguration = $cacheConfiguration;
32 1
        $this->cache = $cache instanceof CacheInterface ? $cache : new ArrayCache();
33 1
    }
34
35 1
    public function __invoke(ServerRequestInterface $request, callable $next)
36
    {
37 1
        if (!$this->cacheConfiguration->requestIsCacheable($request)) {
38
            return resolve($next($request));
39
        }
40
41 1
        $key = $this->cacheConfiguration->cacheKey($request);
42
43
        return $this->cache->get($key)->then(function ($json) use ($next, $request, $key) {
44 1
            if ($json !== null) {
45 1
                return $this->cacheConfiguration->cacheDecode($json);
46
            }
47
48
            return resolve($next($request))->then(function (ResponseInterface $response) use ($request, $key) {
49 1
                if ($response->getBody() instanceof HttpBodyStream) {
50 1
                    return $response;
51
                }
52
53 1
                if (!$this->cacheConfiguration->responseIsCacheable($request, $response)) {
54
                    return $response;
55
                }
56
57 1
                $ttl = $this->cacheConfiguration->cacheTtl($request, $response);
58 1
                $body = (string)$response->getBody();
59 1
                $encodedResponse = $this->cacheConfiguration->cacheEncode($response->withBody(stream_for($body)));
60 1
                $this->cache->set($key, $encodedResponse, $ttl);
61
62 1
                return $response->withBody(stream_for($body));
63 1
            });
64 1
        });
65
    }
66
}
67