Layout::attach()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * MtMail - e-mail module for Zend Framework
4
 *
5
 * @link      http://github.com/mtymek/MtMail
6
 * @copyright Copyright (c) 2013-2017 Mateusz Tymek
7
 * @license   BSD 2-Clause
8
 */
9
10
namespace MtMail\ComposerPlugin;
11
12
use MtMail\Event\ComposerEvent;
13
use MtMail\Template\LayoutProviderInterface;
14
use Zend\EventManager\EventManagerInterface;
15
use Zend\EventManager\AbstractListenerAggregate;
16
use Zend\View\Model\ViewModel;
17
18
class Layout extends AbstractListenerAggregate implements PluginInterface
19
{
20
21
    /**
22
     * @var string
23
     */
24
    protected $layoutTemplate = 'mail/layout.phtml';
25
26
    /**
27
     * @param ComposerEvent $event
28
     */
29 3
    public function injectLayoutViewModel(ComposerEvent $event)
30
    {
31 3
        $currentViewModel = $event->getViewModel();
32
        // don't render layout if ViewModel says so
33 3
        if ($currentViewModel->terminate()) {
34 1
            return;
35
        }
36 2
        $layoutModel = new ViewModel();
37 2
        $layoutModel->addChild($currentViewModel);
38
39 2
        if ($event->getTemplate() instanceof LayoutProviderInterface) {
40 1
            $layout = $event->getTemplate()->getLayout();
41
        } else {
42 1
            $layout = $this->layoutTemplate;
43
        }
44
45 2
        $layoutModel->setTemplate($layout);
46 2
        $event->setViewModel($layoutModel);
47 2
    }
48
49
    /**
50
     * Attach one or more listeners
51
     *
52
     * Implementors may add an optional $priority argument; the EventManager
53
     * implementation will pass this to the aggregate.
54
     *
55
     * @param EventManagerInterface $events
56
     *
57
     * @return void
58
     */
59
    public function attach(EventManagerInterface $events, $priority = 1)
60
    {
61
        $this->listeners[] = $events->attach(ComposerEvent::EVENT_HTML_BODY_PRE, [$this, 'injectLayoutViewModel']);
62
    }
63
64
    /**
65
     * @param  string $layoutTemplate
66
     * @return self
67
     */
68 2
    public function setLayoutTemplate($layoutTemplate)
69
    {
70 2
        $this->layoutTemplate = $layoutTemplate;
71
72 2
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 2
    public function getLayoutTemplate()
79
    {
80 2
        return $this->layoutTemplate;
81
    }
82
}
83