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

PhpRenderer::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
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