RenderBlock   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
eloc 12
c 4
b 0
f 2
dl 0
loc 29
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedId() 0 3 1
A render() 0 14 4
A __construct() 0 3 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
        $view = $this->twig->load($template);
28
        if (false === $view->hasBlock($blockId)) {
29
            throw new TemplateDoesNotContainBlock();
30
        }
31
32
        $html = $view->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