AggregateThemeFactory::addThemeFactory()   A
last analyzed

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 Psr\Http\Message\StreamInterface;
8
use TheCodingMachine\CMS\Block\BlockInterface;
9
use TheCodingMachine\CMS\Block\BlockRendererInterface;
10
use TheCodingMachine\CMS\RenderableInterface;
11
12
class AggregateThemeFactory implements ThemeFactoryInterface
13
{
14
    /**
15
     * @var ThemeFactoryInterface[]
16
     */
17
    private $themeFactories;
18
19
    /**
20
     * @param ThemeFactoryInterface[] $themeFactories
21
     */
22
    public function __construct(array $themeFactories = [])
23
    {
24
        $this->themeFactories = $themeFactories;
25
    }
26
27
    /**
28
     * @param ThemeFactoryInterface[] $themeFactories
29
     */
30
    public function setThemeFactories(array $themeFactories): void
31
    {
32
        $this->themeFactories = $themeFactories;
33
    }
34
35
    /**
36
     * @param ThemeFactoryInterface $themeFactory
37
     */
38
    public function addThemeFactory(ThemeFactoryInterface $themeFactory): void
39
    {
40
        $this->themeFactories[] = $themeFactory;
41
    }
42
43
44
    /**
45
     * Creates a theme object based on the descriptor object passed in parameter.
46
     * Throws an exception if the factory cannot handle this type of descriptor.
47
     *
48
     * @throws \TheCodingMachine\CMS\Theme\CannotHandleThemeDescriptorExceptionInterface
49
     */
50
    public function createTheme(ThemeDescriptorInterface $descriptor): RenderableInterface
51
    {
52
        foreach ($this->themeFactories as $themeFactory) {
53
            if ($themeFactory->canCreateTheme($descriptor)) {
54
                return $themeFactory->createTheme($descriptor);
55
            }
56
        }
57
        throw CannotHandleThemeDescriptorException::cannotHandleDescriptorClass(get_class($descriptor));
58
    }
59
60
    /**
61
     * Returns true if this factory can handle the descriptor passed in parameter. False otherwise.
62
     */
63
    public function canCreateTheme(ThemeDescriptorInterface $descriptor): bool
64
    {
65
        foreach ($this->themeFactories as $themeFactory) {
66
            if ($themeFactory->canCreateTheme($descriptor)) {
67
                return true;
68
            }
69
        }
70
        return false;
71
    }
72
}
73