BlockRenderer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TheCodingMachine\CMS\Block;
3
4
5
use Psr\Http\Message\StreamInterface;
6
use TheCodingMachine\CMS\Theme\ThemeFactoryInterface;
7
use TheCodingMachine\CMS\Utils\ContextMerger;
8
use TheCodingMachine\CMS\Utils\ContextMergerInterface;
9
10
class BlockRenderer implements BlockRendererInterface
11
{
12
    /**
13
     * @var ThemeFactoryInterface
14
     */
15
    private $themeFactory;
16
    /**
17
     * @var ContextMergerInterface
18
     */
19
    private $contextMerger;
20
21
    public function __construct(ThemeFactoryInterface $themeFactory, ContextMergerInterface $contextMerger = null)
22
    {
23
        $this->themeFactory = $themeFactory;
24
        $this->contextMerger = $contextMerger ?: new ContextMerger();
25
    }
26
27
28
    /**
29
     * Renders a block.
30
     *
31
     * @param BlockInterface $page
32
     * @param mixed[] $additionalContext
33
     * @return StreamInterface
34
     */
35
    public function renderBlock(BlockInterface $page, array $additionalContext = []): StreamInterface
36
    {
37
        $theme = $this->themeFactory->createTheme($page->getThemeDescriptor());
38
39
        $context = $this->contextMerger->mergeContexts($page->getContext(), $additionalContext);
40
41
        return $theme->render($context);
42
    }
43
}
44