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.

CacheMiddleware::pre()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.0283

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 22
cts 24
cp 0.9167
rs 8.2826
c 0
b 0
f 0
cc 7
nc 4
nop 3
crap 7.0283
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Middleware\Cache;
4
5
use ApiClients\Foundation\Middleware\MiddlewareInterface;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use React\Cache\CacheInterface;
9
use React\Promise\CancellablePromiseInterface;
10
use React\Promise\PromiseInterface;
11
use function React\Promise\reject;
12
use function React\Promise\resolve;
13
use RingCentral\Psr7\BufferStream;
14
use Throwable;
15
16
final class CacheMiddleware implements MiddlewareInterface
17
{
18
    const DEFAULT_GLUE = '/';
19
20
    /**
21
     * @var CacheInterface[]
22
     */
23
    private $cache;
24
25
    /**
26
     * @var string[]
27
     */
28
    private $key;
29
30
    /**
31
     * @var RequestInterface[]
32
     */
33
    private $request;
34
35
    /**
36
     * @var bool[]
37
     */
38
    private $store = false;
39
40
    /**
41
     * @var StrategyInterface[]
42
     */
43
    private $strategy;
44
45
    /**
46
     * @param  RequestInterface            $request
47
     * @param  array                       $options
48
     * @return CancellablePromiseInterface
49
     */
50 10
    public function pre(
51
        RequestInterface $request,
52
        string $transactionId,
53
        array $options = []
54
    ): CancellablePromiseInterface {
55 10
        if (!isset($options[self::class][Options::CACHE]) || !isset($options[self::class][Options::STRATEGY])) {
56
            return resolve($request);
57
        }
58 10
        $this->cache[$transactionId] = $options[self::class][Options::CACHE];
59 10
        $this->strategy[$transactionId] = $options[self::class][Options::STRATEGY];
60 10
        if (!($this->cache[$transactionId] instanceof CacheInterface) ||
61 10
            !($this->strategy[$transactionId] instanceof StrategyInterface)
62
        ) {
63
            return resolve($request);
64
        }
65
66 10
        if ($request->getMethod() !== 'GET') {
67 6
            $this->cleanUpTransaction($transactionId);
68
69 6
            return resolve($request);
70
        }
71
72 4
        $this->request[$transactionId] = $request;
73 4
        $this->key[$transactionId] = CacheKey::create(
74 4
            $this->request[$transactionId]->getUri(),
75 4
            $options[self::class][Options::GLUE] ?? self::DEFAULT_GLUE
76
        );
77
78 4
        return $this->cache[$transactionId]->get(
79 4
            $this->key[$transactionId]
80
        )->then(function (string $json) use ($transactionId) {
81 2
            $document = Document::createFromString($json);
82
83 2
            if ($document->hasExpired()) {
84 1
                $this->cache[$transactionId]->delete($this->key[$transactionId]);
85
86 1
                return resolve($this->request[$transactionId]);
87
            }
88
89 1
            return reject($document->getResponse());
90
        }, function () use ($transactionId) {
91 2
            return resolve($this->request[$transactionId]);
92 4
        });
93
    }
94
95
    /**
96
     * @param  ResponseInterface           $response
97
     * @param  array                       $options
98
     * @return CancellablePromiseInterface
99
     */
100 7
    public function post(
101
        ResponseInterface $response,
102
        string $transactionId,
103
        array $options = []
104
    ): CancellablePromiseInterface {
105 7
        if (!isset($this->request[$transactionId]) ||
106 7
            !($this->request[$transactionId] instanceof RequestInterface)
107
        ) {
108 6
            $this->cleanUpTransaction($transactionId);
109
110 6
            return resolve($response);
111
        }
112
113 1
        $this->store[$transactionId] = $this->strategy[$transactionId]->
114 1
            decide($this->request[$transactionId], $response);
115
116 1
        if (!$this->store[$transactionId]) {
117
            $this->cleanUpTransaction($transactionId);
118
119
            return resolve($response);
120
        }
121
122
        return $this->hasBody($response)->then(function (ResponseInterface $response) use ($transactionId) {
123 1
            $document = Document::createFromResponse(
124 1
                $response,
125 1
                $this->strategy[$transactionId]->determineTtl(
126 1
                    $this->request[$transactionId],
127 1
                    $response
128
                )
129
            );
130
131 1
            $this->cache[$transactionId]->set($this->key[$transactionId], (string)$document);
132
133 1
            return resolve($document->getResponse());
134
        }, function () use ($response) {
135
            return resolve($response);
136
        })->always(function () use ($transactionId): void {
137 1
            $this->cleanUpTransaction($transactionId);
138 1
        });
139
    }
140
141
    public function error(
142
        Throwable $throwable,
143
        string $transactionId,
144
        array $options = []
145
    ): CancellablePromiseInterface {
146
        $this->cleanUpTransaction($transactionId);
147
148
        return reject($throwable);
149
    }
150
151
    /**
152
     * @param  ResponseInterface $response
153
     * @return PromiseInterface
154
     */
155 1
    private function hasBody(ResponseInterface $response): PromiseInterface
156
    {
157 1
        if ($response->getBody() instanceof BufferStream) {
158 1
            return resolve($response);
159
        }
160
161
        return reject();
162
    }
163
164 7
    private function cleanUpTransaction(string $transactionId): void
165
    {
166 7
        if (isset($this->cache[$transactionId])) {
167 7
            unset($this->cache[$transactionId]);
168
        }
169
170 7
        if (isset($this->strategy[$transactionId])) {
171 7
            unset($this->strategy[$transactionId]);
172
        }
173
174 7
        if (isset($this->request[$transactionId])) {
175 1
            unset($this->request[$transactionId]);
176
        }
177
178 7
        if (isset($this->key[$transactionId])) {
179 1
            unset($this->key[$transactionId]);
180
        }
181
182 7
        if (isset($this->store[$transactionId])) {
183 1
            unset($this->store[$transactionId]);
184
        }
185 7
    }
186
}
187