Passed
Pull Request — master (#2)
by David
02:02
created

TwigThemeFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createTheme() 0 6 2
A canCreateTheme() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
4
namespace TheCodingMachine\CMS\Theme;
5
6
7
use TheCodingMachine\CMS\Block\BlockRendererInterface;
8
use TheCodingMachine\CMS\RenderableInterface;
9
10
class TwigThemeFactory implements ThemeFactoryInterface
11
{
12
    /**
13
     * @var \Twig_Environment
14
     */
15
    private $twig;
16
    /**
17
     * @var BlockRendererInterface
18
     */
19
    private $blockRenderer;
20
21
    public function __construct(\Twig_Environment $twig, BlockRendererInterface $blockRenderer)
22
    {
23
        $this->twig = $twig;
24
        $this->blockRenderer = $blockRenderer;
25
    }
26
27
    /**
28
     * Creates a theme object based on the descriptor object passed in parameter.
29
     *
30
     * @throws \TheCodingMachine\CMS\Theme\CannotHandleThemeDescriptorExceptionInterface Throws an exception if the factory cannot handle this descriptor.
31
     */
32
    public function createTheme(ThemeDescriptorInterface $descriptor): RenderableInterface
33
    {
34
        if (!$descriptor instanceof TwigThemeDescriptor) {
35
            throw CannotHandleThemeDescriptorException::cannotHandleDescriptorClass(get_class($descriptor));
36
        }
37
        return new TwigTheme($this->twig, $descriptor->getTemplate(), $this->blockRenderer);
38
    }
39
40
    /**
41
     * Returns true if this factory can handle the descriptor passed in parameter. False otherwise.
42
     */
43
    public function canCreateTheme(ThemeDescriptorInterface $descriptor): bool
44
    {
45
        return $descriptor instanceof TwigThemeDescriptor;
46
    }
47
}
48