Completed
Push — master ( 047880...b09c02 )
by Kamil
92:08 queued 74:48
created

HtmlDebugTemplateEventRenderer::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 4
nop 2
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 Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface;
18
19
final class HtmlDebugTemplateEventRenderer implements TemplateEventRendererInterface
20
{
21
    /** @var TemplateEventRendererInterface */
22
    private $templateEventRenderer;
23
24
    /** @var TemplateBlockRegistryInterface */
25
    private $templateBlockRegistry;
26
27
    public function __construct(TemplateEventRendererInterface $templateEventRenderer, TemplateBlockRegistryInterface $templateBlockRegistry)
28
    {
29
        $this->templateEventRenderer = $templateEventRenderer;
30
        $this->templateBlockRegistry = $templateBlockRegistry;
31
    }
32
33
    public function render(string $eventName, array $context = []): string
34
    {
35
        $shouldRenderHtmlDebug = $this->shouldRenderHtmlDebug($this->templateBlockRegistry->findEnabledForEvent($eventName));
36
37
        $renderedParts = [];
38
39
        if ($shouldRenderHtmlDebug) {
40
            $renderedParts[] = sprintf(
41
                '<!-- BEGIN EVENT | event name: "%s" -->',
42
                $eventName
43
            );
44
        }
45
46
        $renderedParts[] = $this->templateEventRenderer->render($eventName, $context);
47
48
        if ($shouldRenderHtmlDebug) {
49
            $renderedParts[] = sprintf(
50
                '<!-- END EVENT | event name: "%s" -->',
51
                $eventName
52
            );
53
        }
54
55
        return implode("\n", $renderedParts);
56
    }
57
58
    /**
59
     * @param TemplateBlock[] $templateBlocks
60
     */
61
    private function shouldRenderHtmlDebug(array $templateBlocks): bool
62
    {
63
        return count($templateBlocks) === 0 || count(array_filter($templateBlocks, static function (TemplateBlock $templateBlock): bool {
64
            return strrpos($templateBlock->getTemplate(), '.html.twig') !== false;
65
        })) >= 1;
66
    }
67
}
68