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.
Passed
Pull Request — master (#17)
by
unknown
01:34
created

WithRandomHeadersMiddleware::__invoke()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 13
ccs 0
cts 12
cp 0
crap 6
rs 9.9666
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 function React\Promise\resolve;
8
9
final class WithRandomHeadersMiddleware
10
{
11
    private $headers = [];
12
13
    private $minimum = 2;
14
15
    private $maximum = 2;
16
17
    /**
18
     * @param array $headers
19
     */
20
    public function __construct(array $headers, int $minimum = 2, int $maximum = 2)
21
    {
22
        $this->headers = $headers;
23
        $this->minimum = $minimum;
24
        $this->maximum = $maximum;
25
26
        $headersCount = count($headers);
27
        if ($this->minimum > $headersCount) {
28
            $this->minimum = $headersCount;
29
        }
30
        if ($this->maximum > $headersCount) {
31
            $this->maximum = $headersCount;
32
        }
33
        if ($this->maximum < $this->minimum) {
34
            $this->maximum = $this->minimum;
35
        }
36
    }
37
38
    public function __invoke(ServerRequestInterface $request, callable $next)
39
    {
40
        return resolve($next($request))->then(function (ResponseInterface $response) {
41
            $count = random_int($this->minimum, $this->maximum);
42
            $headers = $this->headers;
43
            for ($i = 0; $i < $count; $i++) {
44
                $randomizer = array_keys($headers);
45
                $header = $randomizer[random_int(0, count($headers) - 1)];
46
                $response = $response->withHeader($header, $headers[$header]);
47
                unset($headers[$header]);
48
            }
49
50
            return resolve($response);
51
        });
52
    }
53
}
54