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

HtmlDebugTemplateEventRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A render() 0 24 3
A shouldRenderHtmlDebug() 0 6 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