Completed
Push — master ( 04f83a...b71153 )
by Alex
09:15
created

ViewBase::setErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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
     * Template variable
23
     * 
24
     * @var string
25
     */
26
    const ERROR_CODE = 'error-code';
27
28
    /**
29
     * Template variable
30
     *
31
     * @var string
32
     */
33
    const ERROR_MESSAGE = 'error-message';
34
35
    /**
36
     * Active template
37
     *
38
     * @var HtmlTemplate
39
     */
40
    private $template = null;
41
42
    /**
43
     * Error code
44
     *
45
     * @var integer
46
     */
47
    private $errorCode = 0;
48
49
    /**
50
     * Error message
51
     *
52
     * @var string
53
     */
54
    private $errorMessage = '';
55
56
    /**
57
     * Constructor
58
     *
59
     * @param HtmlTemplate $template
60
     *            template
61
     */
62
    public function __construct(HtmlTemplate $template = null)
63
    {
64
        $this->template = $template;
65
    }
66
67
    /**
68
     * Method returns template
69
     *
70
     * @return HtmlTemplate template
71
     */
72
    public function getTemplate(): HtmlTemplate
73
    {
74
        if ($this->template === null) {
75
            throw (new \Exception('Template was not set for the view', - 1));
76
        }
77
78
        return $this->template;
79
    }
80
81
    /**
82
     * Method returns code of the last error
83
     *
84
     * @return int code of the last error
85
     */
86
    public function getErrorCode(): int
87
    {
88
        return $this->errorCode;
89
    }
90
91
    /**
92
     * Method sets code of the last error
93
     *
94
     * @param int $code
95
     *            code of the last error
96
     */
97
    public function setErrorCode(int $errorCode): void
98
    {
99
        $this->errorCode = $errorCode;
100
101
        $this->getTemplate()->setPageVar(ViewBase::ERROR_CODE, $errorCode);
102
    }
103
104
    /**
105
     * Method return last error description
106
     *
107
     * @return string last error description
108
     */
109
    public function getErrorMessage(): string
110
    {
111
        return $this->errorMessage;
112
    }
113
114
    /**
115
     * Method sets last error description
116
     *
117
     * @param
118
     *            string last error description
119
     */
120
    public function setErrorMessage(string $errorMessage): void
121
    {
122
        $this->errorMessage = $errorMessage;
123
124
        $this->getTemplate()->setPageVar(ViewBase::ERROR_MESSAGE, $errorMessage);
125
    }
126
}
127