Completed
Push — develop ( f3c189...e92436 )
by Alejandro
08:55
created

DefaultLayout   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 67
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processLayout() 0 20 4
A getModel() 0 4 1
A hasModel() 0 4 1
A getTemplateCaptureTo() 0 4 1
A __construct() 0 5 1
1
<?php
2
namespace AcMailer\View;
3
4
use Zend\Stdlib\ArrayUtils;
5
use Zend\View\Model\ViewModel;
6
7
/**
8
 * Class DefaultLayout
9
 * @author Alejandro Celaya Alastrué
10
 * @link http://www.alejandrocelaya.com
11
 */
12
class DefaultLayout implements DefaultLayoutInterface
13
{
14
    /**
15
     * @var ViewModel
16
     */
17
    protected $model;
18
    /**
19
     * @var string
20
     */
21
    protected $templateCaptureTo;
22
23
    /**
24
     * @param string|ViewModel|null $layout
25
     * @param array $params
26
     * @param string $templateCaptureTo
27
     */
28 36
    public function __construct($layout = null, array $params = [], $templateCaptureTo = 'content')
29
    {
30 36
        $this->processLayout($layout, $params);
31 36
        $this->templateCaptureTo = $templateCaptureTo;
32 36
    }
33
34 36
    private function processLayout($layout, array $params)
35
    {
36 36
        if ($layout instanceof ViewModel) {
37
            // Set the model as is when a ViewModel has been set
38 2
            $currentVariables = $layout->getVariables();
39 2
            if ($currentVariables instanceof \Traversable) {
40 1
                $currentVariables = ArrayUtils::iteratorToArray($currentVariables);
41 1
            }
42
43 2
            $layout->setVariables(array_merge($currentVariables, $params));
44 2
            $this->model = $layout;
45 36
        } elseif (is_string($layout)) {
46
            // Create a new ViewModel when a string is provided
47 2
            $this->model = new ViewModel($params);
48 2
            $this->model->setTemplate($layout);
49 2
        } else {
50
            // Unset the model in any other case
51 34
            $this->model = null;
52
        }
53 36
    }
54
55
    /**
56
     * @return ViewModel
57
     */
58 5
    public function getModel()
59
    {
60 5
        return $this->model;
61
    }
62
63
    /**
64
     * @return boolean
65
     */
66 8
    public function hasModel()
67
    {
68 8
        return isset($this->model);
69
    }
70
71
    /**
72
     * @return string
73
     */
74 3
    public function getTemplateCaptureTo()
75
    {
76 3
        return $this->templateCaptureTo;
77
    }
78
}
79