ListRenderer::setListTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Povs\ListerTwigBundle\Service;
4
5
use Povs\ListerBundle\View\FieldView;
6
use Povs\ListerBundle\View\ViewInterface;
7
use Throwable;
8
use Twig\Environment;
9
use Twig\TemplateWrapper;
10
11
/**
12
 * @author Povilas Margaiatis <[email protected]>
13
 */
14
class ListRenderer
15
{
16
    /**
17
     * @var Environment
18
     */
19
    private $twig;
20
21
    /**
22
     * @var string|null
23
     */
24
    private $listTemplateName;
25
26
    /**
27
     * @var TemplateWrapper|null
28
     */
29
    private $themeTemplate;
30
31
    /**
32
     * @var TemplateWrapper|null
33
     */
34
    private $listTemplate;
35
36
    /**
37
     * @var bool
38
     */
39
    private $templatesLoaded = false;
40
41
    /**
42
     * ListRenderer constructor.
43
     *
44
     * @param Environment $twig
45
     */
46 5
    public function __construct(Environment $twig)
47
    {
48 5
        $this->twig = $twig;
49
    }
50
51
    /**
52
     * @param string|null $listTemplateName
53
     */
54 3
    public function setListTemplate(?string $listTemplateName): void
55
    {
56 3
        $this->listTemplateName = $listTemplateName;
57
    }
58
59
    /**
60
     * @param ViewInterface $view
61
     * @param string        $blockName
62
     * @param array         $context
63
     * @param bool          $rewritable
64
     *
65
     * @return string
66
     * @throws Throwable
67
     */
68 5
    public function render(ViewInterface $view, string $blockName, array $context, bool $rewritable): string
69
    {
70 5
        $this->loadTemplates($context);
71
72 5
        if ($rewritable && $block = $this->findBlock($view, $blockName)) {
73 2
            return $this->listTemplate->renderBlock($block, $this->getContext($view, $context));
74
        }
75
76 3
        return $this->themeTemplate->renderBlock($blockName, $this->getContext($view, $context));
77
    }
78
79
    /**
80
     * @param ViewInterface $view
81
     * @param string        $blockName
82
     *
83
     * @return string|null
84
     */
85 5
    private function findBlock(ViewInterface $view, string $blockName): ?string
86
    {
87 5
        if (!$this->listTemplate) {
88 2
            return null;
89
        }
90
91 3
        if ($view instanceof FieldView) {
92 1
            $fieldBlock = sprintf('%s_%s', $blockName, $view->getId());
93 1
            $blocks = [$fieldBlock, $blockName];
94
        } else {
95 2
            $blocks = [$blockName];
96
        }
97
98 3
        foreach ($blocks as $block) {
99 3
            if ($this->listTemplate->hasBlock($block, [])) {
100 2
                return $block;
101
            }
102
        }
103
104 1
        return null;
105
    }
106
107
    /**
108
     * @param array $context
109
     */
110 5
    private function loadTemplates(array $context): void
111
    {
112 5
        if ($this->templatesLoaded) {
113 1
            return;
114
        }
115
116 5
        $this->themeTemplate = $this->twig->load($context['list_data']['theme']);
117
118 5
        if ($this->listTemplateName) {
119 3
            $this->listTemplate = $this->twig->load($this->listTemplateName);
120
        }
121
122 5
        $this->templatesLoaded = true;
123
    }
124
125
    /**
126
     * @param ViewInterface $view
127
     * @param array         $context
128
     *
129
     * @return array
130
     */
131 5
    private function getContext(ViewInterface $view, array $context): array
132
    {
133 5
        return array_merge($context, ['list' => $view]);
134
    }
135
}
136