TwigRenderer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace jschreuder\Middle\View;
4
5
use Psr\Http\Message\ResponseFactoryInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Twig\Environment;
9
10
final class TwigRenderer implements RendererInterface
11
{
12 2
    public function __construct(
13
        private Environment $twig,
14
        private ResponseFactoryInterface $responseFactory
15
    )
16
    {
17 2
    }
18
19 1
    public function render(ServerRequestInterface $request, ViewInterface $view): ResponseInterface
20
    {
21
        // Get new Response with HTML content type
22 1
        $response = $this->responseFactory->createResponse()->withHeader('Content-Type', 'text/html; charset=utf-8');
23
24
        // Write rendered Twig to body
25 1
        $response->getBody()->rewind();
26 1
        $response->getBody()->write($this->twig->render($view->getTemplate(), $view->getParameters()));
27 1
        $response->getBody()->rewind();
28
29 1
        return $response;
30
    }
31
}
32