Completed
Push — master ( 52cd2c...047880 )
by Kamil
47:26 queued 26:32
created

TemplateBlockRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A render() 0 19 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UiBundle\Renderer;
15
16
use Sylius\Bundle\UiBundle\Registry\TemplateBlock;
17
use Twig\Environment;
18
19
final class TemplateBlockRenderer implements TemplateBlockRendererInterface
20
{
21
    /** @var Environment */
22
    private $twig;
23
24
    /** @var bool */
25
    private $debug;
26
27
    public function __construct(Environment $twig, bool $debug)
28
    {
29
        $this->twig = $twig;
30
        $this->debug = $debug;
31
    }
32
33
    public function render(string $eventName, TemplateBlock $templateBlock, array $context = []): string
34
    {
35
        $renderedBlock = '';
36
37
        if ($this->debug && strrpos($templateBlock->getTemplate(), '.html.twig') !== false) {
38
            $renderedBlock .= sprintf(
39
                '<!-- event name: "%s", block name: "%s", template: "%s", priority: %d -->',
40
                $eventName,
41
                $templateBlock->getName(),
42
                $templateBlock->getTemplate(),
43
                $templateBlock->getPriority()
44
            );
45
            $renderedBlock .= "\n";
46
        }
47
48
        $renderedBlock .= $this->twig->render($templateBlock->getTemplate(), array_replace($templateBlock->getContext(), $context));
49
50
        return $renderedBlock;
51
    }
52
}
53