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
Push — master ( 35bb54...9b0758 )
by Cees-Jan
08:54 queued 12s
created

WithRandomHeadersMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 65
rs 10
ccs 19
cts 22
cp 0.8636
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 13 2
A withMinimum() 0 7 1
A enforceMinimumMaximum() 0 16 4
A withMaximum() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WyriHaximus\React\Http\Middleware;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use React\Promise\PromiseInterface;
10
11
use function array_keys;
12
use function count;
13
use function random_int;
14
use function React\Promise\resolve;
15
16
use const WyriHaximus\Constants\Numeric\ONE;
0 ignored issues
show
Bug introduced by
The constant WyriHaximus\Constants\Numeric\ONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use const WyriHaximus\Constants\Numeric\TWO;
0 ignored issues
show
Bug introduced by
The constant WyriHaximus\Constants\Numeric\TWO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
use const WyriHaximus\Constants\Numeric\ZERO;
0 ignored issues
show
Bug introduced by
The constant WyriHaximus\Constants\Numeric\ZERO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
20 1
final class WithRandomHeadersMiddleware
21
{
22 1
    /** @var Header[] */
23 1
    private array $headers;
24 1
25
    private int $minimum = TWO;
26 1
27 1
    private int $maximum = TWO;
28
29
    public function __construct(Header ...$headers)
30 1
    {
31
        $this->headers = $headers;
32
    }
33 1
34
    public function withMinimum(int $minimum): self
35
    {
36 1
        $clone          = clone $this;
37
        $clone->minimum = $minimum;
38
        $clone->enforceMinimumMaximum();
39
40 1
        return $clone;
41 1
    }
42 1
43 1
    public function withMaximum(int $maximum): self
44 1
    {
45 1
        $clone          = clone $this;
46 1
        $clone->maximum = $maximum;
47 1
        $clone->enforceMinimumMaximum();
48
49
        return $clone;
50 1
    }
51 1
52
    public function __invoke(ServerRequestInterface $request, callable $next): PromiseInterface
53
    {
54
        return resolve($next($request))->then(function (ResponseInterface $response): ResponseInterface {
55
            $count   = random_int($this->minimum, $this->maximum);
56
            $headers = $this->headers;
57
            for ($i = ZERO; $i < $count; $i++) {
58
                $randomizer = array_keys($headers);
59
                $header     = $randomizer[random_int(ZERO, count($headers) - ONE)];
60
                $response   = $response->withHeader($headers[$header]->name(), $headers[$header]->contents());
61
                unset($headers[$header]);
62
            }
63
64
            return $response;
65
        });
66
    }
67
68
    // phpcs:disable
69
    private function enforceMinimumMaximum(): void
70
    {
71
        $headersCount = count($this->headers);
72
        if ($this->minimum > $headersCount) {
73
            $this->minimum = $headersCount;
74
        }
75
76
        if ($this->maximum > $headersCount) {
77
            $this->maximum = $headersCount;
78
        }
79
80
        if ($this->maximum >= $this->minimum) {
81
            return;
82
        }
83
84
        $this->maximum = $this->minimum;
85
    }
86
}
87