BlockRenderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A renderBlock() 0 7 1
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