Completed
Push — master ( 477ca5...2d5ed5 )
by Daniel
10s
created

PhpRenderer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A render() 0 22 2
1
<?php
2
3
namespace Psi\Component\ContentType\View\Renderer;
4
5
use Psi\Component\ContentType\View\RendererInterface;
6
use Psi\Component\ContentType\View\TemplateNotFoundException;
7
use Psi\Component\ContentType\View\ViewInterface;
8
9
class PhpRenderer implements RendererInterface
10
{
11
    private $templatePath;
12
13
    public function __construct(string $templatePath)
14
    {
15
        $this->templatePath = $templatePath;
16
    }
17
18
    public function render(ViewInterface $view): string
19
    {
20
        $templateName = $view->getTemplate();
21
        $templatePath = sprintf('%s/%s.php', $this->templatePath, $templateName);
22
23
        if (!file_exists($templatePath)) {
24
            throw new TemplateNotFoundException(sprintf(
25
                'Template "%s" not found at "%s"',
26
                $templateName, $templatePath
27
            ));
28
        }
29
30
        $output = function () use ($view, $templatePath) {
31
            ob_start();
32
            $renderer = $this;
33
            require $templatePath;
34
35
            return ob_get_clean();
36
        };
37
38
        return $output();
39
    }
40
}
41