Completed
Push — master ( 0dd5a4...93d3aa )
by Alex
02:49
created

ViewBase::templateWasSetup()   A

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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Application;
3
4
use Mezon\HtmlTemplate\HtmlTemplate;
5
6
/**
7
 * Class ViewBase
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
abstract class ViewBase implements \Mezon\Application\ViewInterface
20
{
21
22
    /**
23
     * Template variable
24
     *
25
     * @var string
26
     */
27
    const ERROR_CODE = 'error-code';
28
29
    /**
30
     * Template variable
31
     *
32
     * @var string
33
     */
34
    const ERROR_MESSAGE = 'error-message';
35
36
    /**
37
     * Active template
38
     *
39
     * @var HtmlTemplate
40
     */
41
    private $template = null;
42
43
    /**
44
     * Error code
45
     *
46
     * @var integer
47
     */
48
    private $errorCode = 0;
49
50
    /**
51
     * Error message
52
     *
53
     * @var string
54
     */
55
    private $errorMessage = '';
56
57
    /**
58
     * Constructor
59
     *
60
     * @param HtmlTemplate $template
61
     *            template
62
     */
63
    public function __construct(HtmlTemplate $template = null)
64
    {
65
        $this->template = $template;
66
    }
67
68
    /**
69
     * Method returns template
70
     *
71
     * @return HtmlTemplate template
72
     */
73
    public function getTemplate(): HtmlTemplate
74
    {
75
        if ($this->template === null) {
76
            throw (new \Exception('Template was not set for the view', - 1));
77
        }
78
79
        return $this->template;
80
    }
81
82
    /**
83
     * Method returns code of the last error
84
     *
85
     * @return int code of the last error
86
     */
87
    public function getErrorCode(): int
88
    {
89
        return $this->errorCode;
90
    }
91
92
    /**
93
     * Method checks if the template was setup
94
     *
95
     * @return bool true if the method was setup, false otherwise
96
     */
97
    public function templateWasSetup(): bool
98
    {
99
        return $this->template !== null;
100
    }
101
102
    /**
103
     * Method sets code of the last error
104
     *
105
     * @param int $code
106
     *            code of the last error
107
     */
108
    public function setErrorCode(int $errorCode): void
109
    {
110
        $this->errorCode = $errorCode;
111
112
        if ($this->templateWasSetup()) {
113
            $this->getTemplate()->setPageVar(ViewBase::ERROR_CODE, $errorCode);
114
        }
115
    }
116
117
    /**
118
     * Method return last error description
119
     *
120
     * @return string last error description
121
     */
122
    public function getErrorMessage(): string
123
    {
124
        return $this->errorMessage;
125
    }
126
127
    /**
128
     * Method sets last error description
129
     *
130
     * @param
131
     *            string last error description
132
     */
133
    public function setErrorMessage(string $errorMessage): void
134
    {
135
        $this->errorMessage = $errorMessage;
136
137
        if ($this->templateWasSetup()) {
138
            $this->getTemplate()->setPageVar(ViewBase::ERROR_MESSAGE, $errorMessage);
139
        }
140
    }
141
}
142