| 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
Loading history...
|
|||
| 17 | use const WyriHaximus\Constants\Numeric\TWO; |
||
|
0 ignored issues
–
show
|
|||
| 18 | use const WyriHaximus\Constants\Numeric\ZERO; |
||
|
0 ignored issues
–
show
|
|||
| 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 |