Renderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A render() 0 6 1
A setView() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Charcoal\View;
6
7
// From PSR-7
8
use Psr\Http\Message\ResponseInterface as Response;
9
10
/**
11
 * Provides a PSR-7 renderer that uses a Charcoal View.
12
 *
13
 * A "PSR-7" renderer is a service that renders a template identifier inside a HTTP Response
14
 *
15
 * ## Dependencies
16
 * - `view` A "Charcoal View", which is any class that implements `\Charcoal\View\ViewInterface`.
17
 */
18
class Renderer
19
{
20
    /**
21
     * @var ViewInterface
22
     */
23
    private $view;
24
25
    /**
26
     * @param array $data The constructor dependencies.
27
     */
28
    public function __construct(array $data)
29
    {
30
        $this->setView($data['view']);
31
    }
32
33
    /**
34
     * @param Response $response      The HTTP response.
35
     * @param string   $templateIdent The template identifier to load and render.
36
     * @param mixed    $context       The view controller / context.
37
     * @return Response
38
     */
39
    public function render(Response $response, string $templateIdent, $context = null): Response
40
    {
41
        $rendered = $this->view->render($templateIdent, $context);
42
        $response->getBody()->write($rendered);
43
        return $response;
44
    }
45
46
    /**
47
     * @param ViewInterface $view The view instance to use.
48
     * @return void
49
     */
50
    private function setView(ViewInterface $view): void
51
    {
52
        $this->view = $view;
53
    }
54
}
55