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 ( cdf96f...74e120 )
by Cees-Jan
8s
created

CacheMiddleware::pre()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 4
nop 2
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Middleware\Cache;
4
5
use ApiClients\Foundation\Middleware\DefaultPriorityTrait;
6
use ApiClients\Foundation\Middleware\MiddlewareInterface;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use React\Cache\CacheInterface;
10
use React\Promise\CancellablePromiseInterface;
11
use React\Promise\PromiseInterface;
12
use RingCentral\Psr7\BufferStream;
13
use function React\Promise\reject;
14
use function React\Promise\resolve;
15
16
final class CacheMiddleware implements MiddlewareInterface
17
{
18
    use DefaultPriorityTrait;
19
20
    const DEFAULT_GLUE = '/';
21
22
    /**
23
     * @var CacheInterface
24
     */
25
    private $cache;
26
27
    /**
28
     * @var string
29
     */
30
    private $key;
31
32
    /**
33
     * @var RequestInterface
34
     */
35
    private $request;
36
37
    /**
38
     * @var bool
39
     */
40
    private $store = false;
41
42
    /**
43
     * @var StrategyInterface
44
     */
45
    private $strategy;
46
47
    /**
48
     * @param RequestInterface $request
49
     * @param array $options
50
     * @return CancellablePromiseInterface
51
     */
52
    public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
53
    {
54
        if (!isset($options[self::class][Options::CACHE]) || !isset($options[self::class][Options::STRATEGY])) {
55
            return resolve($request);
56
        }
57
        $this->cache = $options[self::class][Options::CACHE];
58
        $this->strategy = $options[self::class][Options::STRATEGY];
59
        if (!($this->cache instanceof CacheInterface) || !($this->strategy instanceof StrategyInterface)) {
60
            return resolve($request);
61
        }
62
63
        if ($request->getMethod() !== 'GET') {
64
            return resolve($request);
65
        }
66
67
        $this->request = $request;
68
        $this->key = CacheKey::create(
69
            $this->request->getUri(),
70
            $options[self::class][Options::GLUE] ?? self::DEFAULT_GLUE
71
        );
72
73
        return $this->cache->get($this->key)->then(function (string $json) {
74
            $document = Document::createFromString($json);
75
76
            if ($document->hasExpired()) {
77
                $this->cache->remove($this->key);
78
                return resolve($this->request);
79
            }
80
81
            return reject($document->getResponse());
82
        }, function () {
83
            return resolve($this->request);
84
        });
85
    }
86
87
    /**
88
     * @param ResponseInterface $response
89
     * @param array $options
90
     * @return CancellablePromiseInterface
91
     */
92
    public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface
93
    {
94
        if (!($this->request instanceof RequestInterface)) {
95
            return resolve($response);
96
        }
97
98
        $this->store = $this->strategy->decide($this->request, $response);
99
100
        if (!$this->store) {
101
            return resolve($response);
102
        }
103
104
        return $this->hasBody($response)->then(function (ResponseInterface $response) {
105
            $document = Document::createFromResponse(
106
                $response,
107
                $this->strategy->determineTtl(
108
                    $this->request,
109
                    $response
110
                )
111
            );
112
113
            $this->cache->set($this->key, (string)$document);
114
115
            return resolve($document->getResponse());
116
        }, function () use ($response) {
117
            return resolve($response);
118
        });
119
    }
120
121
    /**
122
     * @param ResponseInterface $response
123
     * @return PromiseInterface
124
     */
125
    protected function hasBody(ResponseInterface $response): PromiseInterface
126
    {
127
        if ($response->getBody() instanceof BufferStream) {
128
            return resolve($response);
129
        }
130
131
        return reject();
132
    }
133
}
134