TemplateBuilder::get()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
3
namespace T4web\Mail;
4
5
use Zend\View\Renderer\RendererInterface;
6
use Zend\View\Model\ViewModel;
7
8
class TemplateBuilder
9
{
10
    /**
11
     * @var array
12
     */
13
    private $config;
14
15
    /**
16
     * @var RendererInterface
17
     */
18
    private $renderer;
19
20
    public function __construct(
21
        array $config,
22
        RendererInterface $renderer
23
    ) {
24
        $this->config = $config;
25
        $this->renderer = $renderer;
26
    }
27
28
    public function get($templateId)
29
    {
30
        if (!isset($this->config['layout'])) {
31
            throw new Exception\TemplateNotCreatedException("Layouts not describes in config.");
32
        }
33
        if (!isset($this->config['templates'])) {
34
            throw new Exception\TemplateNotExistsException("Templates not describes in config.");
35
        }
36
        if (!isset($this->config['templates'][$templateId])) {
37
            throw new Exception\TemplateNotExistsException("Template $templateId does not exists.");
38
        }
39
        if (!isset($this->config['layout'][$this->config['templates'][$templateId]['layout']])) {
40
            throw new Exception\TemplateNotCreatedException("Layout {$this->config['templates'][$templateId]['layout']} not describes in config.");
41
        }
42
43
        $viewLayout = new ViewModel();
44
        $viewLayout->setTemplate($this->config['layout'][$this->config['templates'][$templateId]['layout']]);
45
46
        return new Template($templateId, $this->config['templates'][$templateId], $this->renderer, $viewLayout);
47
    }
48
}