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.

JsCompressMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 4
cts 7
cp 0.5714
rs 9.8666
c 0
b 0
f 0
cc 2
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 View Code Duplication
final class JsCompressMiddleware
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    const MIME_TYPE = 'application/javascript';
17
18
    /**
19
     * @var Parser
20
     */
21
    private $compressor;
22
23
    /**
24
     * @param Parser $compressor
25
     */
26 1
    public function __construct(Parser $compressor = null)
27
    {
28 1
        if ($compressor === null) {
29 1
            $compressor = Factory::construct();
30
        }
31
32 1
        $this->compressor = $compressor;
33 1
    }
34
35 1
    public function __invoke(ServerRequestInterface $request, callable $next)
36
    {
37 1
        $response = $next($request);
38
39 1
        if (!($response instanceof PromiseInterface)) {
40 1
            return resolve($this->handleResponse($response));
41
        }
42
43
        return $response->then(function (ResponseInterface $response) {
44
            return $this->handleResponse($response);
45
        });
46
    }
47
48 1
    private function handleResponse(ResponseInterface $response)
49
    {
50 1
        if ($response->getBody() instanceof HttpBodyStream) {
51
            return $response;
52
        }
53
54 1
        if (!$response->hasHeader('content-type')) {
55
            return $response;
56
        }
57
58 1
        list($contentType) = explode(';', $response->getHeaderLine('content-type'));
59 1
        if ($contentType !== self::MIME_TYPE) {
60
            return $response;
61
        }
62
63 1
        $body = (string)$response->getBody();
64 1
        $compressedBody = substr($this->compressor->compress('<script>' . $body . '</script>'), 8, -9);
65
66 1
        return $response->withBody(stream_for($compressedBody))->withHeader('Content-Length', strlen($compressedBody));
67
    }
68
}
69