SubTheme::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\Theme;
5
6
7
use Psr\Http\Message\StreamInterface;
8
use TheCodingMachine\CMS\RenderableInterface;
9
use TheCodingMachine\CMS\Utils\ContextMerger;
10
use TheCodingMachine\CMS\Utils\ContextMergerInterface;
11
12
/**
13
 * This class is an adapter around a theme that adds additional information in the context.
14
 */
15
class SubTheme implements RenderableInterface
16
{
17
    /**
18
     * @var RenderableInterface
19
     */
20
    private $theme;
21
    /**
22
     * @var mixed[]
23
     */
24
    private $additionalContext;
25
    /**
26
     * @var ContextMergerInterface
27
     */
28
    private $contextMerger;
29
30
    /**
31
     * @param RenderableInterface $theme
32
     * @param mixed[] $additionalContext
33
     */
34
    public function __construct(RenderableInterface $theme, array $additionalContext, ContextMergerInterface $contextMerger = null)
35
    {
36
        $this->theme = $theme;
37
        $this->additionalContext = $additionalContext;
38
        $this->contextMerger = $contextMerger ?: new ContextMerger();
39
    }
40
41
    /**
42
     * Renders (as a stream) the data passed in parameter.
43
     *
44
     * @param mixed[] $context
45
     * @return StreamInterface
46
     */
47
    public function render(array $context): StreamInterface
48
    {
49
        return $this->theme->render($this->contextMerger->mergeContexts($this->additionalContext, $context));
50
    }
51
}
52