Passed
Push — master ( 3d191d...b6a6e2 )
by Ivan
02:46
created

RenderBlock::getNormalizedId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\AjaxcomBundle\Service;
6
7
use Everlution\AjaxcomBundle\DataObject\Block;
8
9
/**
10
 * Class RenderBlock.
11
 *
12
 * @author Ivan Barlog <[email protected]>
13
 */
14
class RenderBlock
15
{
16
    /** @var \Twig_Environment */
17
    private $twig;
18
19
    public function __construct(\Twig_Environment $twig)
20
    {
21
        $this->twig = $twig;
22
    }
23
24
    public function render(Block $block, string $template, array $parameters = []): string
25
    {
26
        $blockId = $this->getNormalizedId($block);
27
        $template = $this->twig->load($template);
28
        if (false === $template->hasBlock($blockId)) {
29
            throw new TemplateDoesNotContainBlock();
30
        }
31
32
        $html = $template->renderBlock($blockId, $parameters);
33
        if (empty($html) && false === $block->shouldRefresh()) {
34
            throw new BlockIsEmpty();
35
        }
36
37
        return $html;
38
    }
39
40
    private function getNormalizedId(Block $block): string
41
    {
42
        return str_replace('-', '_', $block->getId());
43
    }
44
}
45