Test Failed
Push — master ( fdaa2f...711c23 )
by Derek Stephen
09:23
created

LayoutMiddleware::process()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 22
rs 9.2222
cc 6
nc 8
nop 2
1
<?php
2
3
namespace Bone\View\Middleware;
4
5
use Bone\View\ViewEngineInterface;
6
use Laminas\Diactoros\Response;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Laminas\Diactoros\Response\HtmlResponse;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Response\HtmlResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Laminas\Diactoros\Stream;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Stream was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class LayoutMiddleware implements MiddlewareInterface
15
{
16
    /** @var ViewEngineInterface $viewEngine */
17
    private $viewEngine;
18
19
    /** @var string $defaultLayout\ */
20
    private $defaultLayout;
21
22
    /**
23
     * LayoutMiddleware constructor.
24
     * @param ViewEngineInterface $viewEngine
25
     */
26
    public function __construct(ViewEngineInterface $viewEngine, string $defaultLayout)
27
    {
28
        $this->viewEngine = $viewEngine;
29
        $this->defaultLayout = $defaultLayout;
30
    }
31
32
    /**
33
     * @param ServerRequestInterface $request
34
     * @param RequestHandlerInterface $handler
35
     * @return ResponseInterface
36
     */
37
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
38
    {
39
        $response = $handler->handle($request);
40
        $layout = $this->defaultLayout;
41
42
        if ($response->hasHeader('layout')) {
43
            $layout = $response->getHeader('layout')[0];
44
        }
45
46
        $response = $response->withoutHeader('header');
47
        $contentType = $response->getHeader('Content-Type');
48
        $isHtmlResponse = $response instanceof HtmlResponse;
49
        $hasHtmlContent = count($contentType) ? strstr($contentType[0], 'text/html') : true;
50
51
        if ((!$isHtmlResponse || !$hasHtmlContent || !$layout)) {
52
            return $response;
53
        }
54
55
        $body = ['content' => $response->getBody()->getContents()];
56
        $body = $this->viewEngine->render($layout, $body);
57
58
        return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode());
59
    }
60
61
62
    /**
63
     * @param Response $response
64
     * @param string $body
65
     * @param int $status
66
     * @return ResponseInterface
67
     */
68
    private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200): ResponseInterface
69
    {
70
        $stream = new Stream('php://memory', 'r+');
71
        $stream->write($body);
72
        $response = $response->withStatus($status)->withBody($stream);
73
74
        return $response;
75
    }
76
}