Completed
Push — master ( 21ef4f...df9e52 )
by Alex
04:31
created

View::getPresenter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
namespace Mezon\Application;
3
4
use Mezon\HtmlTemplate\HtmlTemplate;
5
6
/**
7
 * Class View
8
 *
9
 * @package Mezon
10
 * @subpackage View
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/08/06)
13
 * @copyright Copyright (c) 2019, aeon.org
14
 */
15
16
/**
17
 * Base class for all views
18
 */
19
class View extends ViewBase
20
{
21
22
    /**
23
     * Presenter
24
     *
25
     * @var Presenter
26
     */
27
    private $presenter = null;
28
29
    /**
30
     * View's name
31
     *
32
     * @var string
33
     */
34
    private $viewName = '';
35
36
    /**
37
     * Constructor
38
     *
39
     * @param ?HtmlTemplate $template
40
     *            template
41
     * @param string $viewName
42
     *            View name to be rendered
43
     * @param
44
     *            ?Presenter presenter
45
     */
46
    public function __construct(HtmlTemplate $template = null, string $viewName = '', Presenter $presenter = null)
47
    {
48
        parent::__construct($template);
49
50
        $this->presenter = $presenter;
51
52
        $this->viewName = $viewName;
53
    }
54
55
    /**
56
     * Method renders content from view
57
     *
58
     * @param string $viewName
59
     *            View name to be rendered
60
     * @return string Generated content
61
     */
62
    public function render(string $viewName = ''): string
63
    {
64
        if ($viewName === '') {
65
            $viewName = $this->viewName;
66
        }
67
68
        if ($viewName === '') {
69
            $viewName = 'Default';
70
        }
71
72
        if (method_exists($this, 'view' . $viewName)) {
73
            return call_user_func([
74
                $this,
75
                'view' . $viewName
76
            ]);
77
        }
78
79
        throw (new \Exception('View ' . $viewName . ' was not found'));
80
    }
81
82
    /**
83
     * Method returns view name
84
     *
85
     * @return string view name
86
     */
87
    public function getViewName(): string
88
    {
89
        return $this->viewName;
90
    }
91
92
    /**
93
     * Method return presenter or throws exception if it was not setup
94
     *
95
     * @return Presenter presenter
96
     */
97
    public function getPresenter(): Presenter
98
    {
99
        if ($this->presenter === null) {
100
            throw (new \Exception('Presenter was not setup', - 1));
101
        }
102
103
        return $this->presenter;//@codeCoverageIgnoreStart
104
    }//@codeCoverageIgnoreEnd
105
}
106