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

TwigThemeFactory::canCreateTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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