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 ( 1685d2...a9f7fe )
by Cees-Jan
07:31
created

CacheMiddleware::cleanUpTransaction()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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