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   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 58
ccs 18
cts 23
cp 0.7826
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __invoke() 0 12 2
A handleResponse() 0 20 4
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