HtmlMinify   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 16
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 9 2
A minify() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VM\SlimHtmlMinify\Middlewares;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
12
use voku\helper\HtmlMin;
13
14
class HtmlMinify implements MiddlewareInterface
15
{
16
    private $htmlMin;
17
    private $container;
18
    private $isActive;
19
20 2
    public function __construct(HtmlMin $htmlMin, ContainerInterface $container, bool $isActive = true)
21
    {
22 2
        $this->htmlMin = $htmlMin;
23 2
        $this->container = $container;
24 2
        $this->isActive = $isActive;
25 2
    }
26
27 2
    public function process(Request $request, RequestHandler $requestHandler): Response
28
    {
29 2
        $response = $requestHandler->handle($request);
30
31 2
        if ($this->isActive) {
32 1
            $response = $this->minify($response);
33
        }
34
35 2
        return $response;
36
    }
37
38 1
    private function minify(Response $response): Response
39
    {
40 1
        $oldHtml = $response->getBody()->__toString();
41 1
        $minifiedHtml = $this->htmlMin->minify($oldHtml);
42 1
        $replaceBody = $this->container->get('body');
43 1
        $replaceBody->write($minifiedHtml);
44 1
        return $response->withBody($replaceBody);
45
    }
46
}
47