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 ( bc63cb...eb6a98 )
by Cees-Jan
01:54
created

HtmlCompressMiddleware::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.3149
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 React\Http\Io\HttpBodyStream;
8
use React\Promise\PromiseInterface;
9
use WyriHaximus\HtmlCompress\Factory;
10
use WyriHaximus\HtmlCompress\Parser;
11
use function React\Promise\resolve;
12
use function RingCentral\Psr7\stream_for;
13
14
final class HtmlCompressMiddleware
15
{
16
    const MIME_TYPES = [
17
        'text/html',
18
        'text/xhtml',
19
    ];
20
21
    /**
22
     * @var Parser
23
     */
24
    private $compressor;
25
26
    /**
27
     * @param Parser $compressor
28
     */
29 3
    public function __construct(Parser $compressor = null)
30
    {
31 3
        if ($compressor === null) {
32 3
            $compressor = Factory::constructFastest();
33
        }
34
35 3
        $this->compressor = $compressor;
36 3
    }
37
38 3
    public function __invoke(ServerRequestInterface $request, callable $next)
39
    {
40 3
        $response = $next($request);
41
42 3
        if (!($response instanceof PromiseInterface)) {
43 3
            return resolve($this->handleResponse($response));
44
        }
45
46
        return $response->then(function (ResponseInterface $response) {
47
            return $this->handleResponse($response);
48
        });
49
    }
50
51 3
    private function handleResponse(ResponseInterface $response)
52
    {
53 3
        if ($response->getBody() instanceof HttpBodyStream) {
54
            return $response;
55
        }
56
57 3
        if (!$response->hasHeader('content-type')) {
58 1
            return $response;
59
        }
60
61 2
        list($contentType) = explode(';', $response->getHeaderLine('content-type'));
62 2
        if (!in_array($contentType, self::MIME_TYPES, true)) {
63
            return $response;
64
        }
65
66 2
        $body = (string)$response->getBody();
67 2
        $compressedBody = $this->compressor->compress($body);
68
69 2
        return $response->withBody(stream_for($compressedBody))->withHeader('Content-Length', strlen($compressedBody));
70
    }
71
}
72