AbstractMiddleware   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 2
b 0
f 0
dl 0
loc 46
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A containsHtml() 0 18 5
A getSuffixStatistics() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctw\Middleware;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
9
abstract class AbstractMiddleware implements MiddlewareInterface
10
{
11
    /**
12
     * Suffix added to HTML Responses
13
     * @var string
14
     */
15
    protected const HTML_SUFFIX = '<!-- html: in %d b | out %d b | diff %01.4f %% -->';
16
17
    /**
18
     * Responses with these MIME types are HTML Responses
19
     * @var string[]
20
     */
21
    protected const   HTML_MIME_TYPES
22
        = ['text/html', 'application/xhtml'];
23
24 6
    protected function containsHtml(ResponseInterface $response): bool
25
    {
26 6
        $header = $response->getHeader('Content-Type');
27
28 6
        if ([] === $header) {
29 2
            return false;
30
        }
31
32 4
        foreach (self::HTML_MIME_TYPES as $needle) {
33 4
            foreach ($header as $haystack) {
34 4
                $pos = strpos($haystack, $needle);
35 4
                if (is_int($pos)) {
36 2
                    return true;
37
                }
38
            }
39
        }
40
41 2
        return false;
42
    }
43
44
    /**
45
     * Return an array of statistics for use in the suffix added to the HTML
46
     */
47 2
    protected function getSuffixStatistics(string $original, string $minified): array
48
    {
49 2
        $in      = mb_strlen($original);
50 2
        $out     = mb_strlen($minified);
51 2
        $percent = 100 * ($out / $in);
52 2
        $diff    = 100 - $percent;
53
54 2
        return [$in, $out, $diff];
55
    }
56
}
57