1
|
|
|
<?php namespace Cornford\Alerter; |
2
|
|
|
|
3
|
|
|
use Cornford\Alerter\Contracts\AlertableInterface; |
4
|
|
|
use Cornford\Alerter\Exceptions\AlertContentException; |
5
|
|
|
use Cornford\Alerter\Exceptions\AlertTypeException; |
6
|
|
|
use Cornford\Alerter\Exceptions\AlertVariableException; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
|
9
|
|
|
class Alert extends AlertType implements AlertableInterface { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The content of the alert. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $content; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The type of the alert. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The view of the alert. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $view; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The path for the alert view. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $viewPath; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Construct a new alerter instance. |
41
|
|
|
* |
42
|
|
|
* @param string $type |
43
|
|
|
* @param string $content |
44
|
|
|
* |
45
|
|
|
* @return self |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
private function __construct($type, $content) |
48
|
|
|
{ |
49
|
|
|
$this->type = $type; |
50
|
|
|
$this->content = $content; |
51
|
|
|
|
52
|
|
|
$viewPath = ''; |
53
|
|
|
$view = ''; |
54
|
|
|
|
55
|
|
|
if (class_exists('Config')) { |
56
|
|
|
$viewPath = Config::get('alerter::path'); |
57
|
|
|
$view = Config::get('alerter::alert'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->viewPath = $viewPath ?: dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Views'; |
61
|
|
|
$this->view = $view ?: AlertDisplay::VIEW_SIMPLE; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a new alerter instance. |
66
|
|
|
* |
67
|
|
|
* @param string $type |
68
|
|
|
* @param string $content |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
* |
72
|
|
|
* @throws \Cornford\Alerter\Exceptions\AlertContentException |
73
|
|
|
* @throws \Cornford\Alerter\Exceptions\AlertTypeException |
74
|
|
|
*/ |
75
|
|
|
public static function create($type, $content) |
76
|
|
|
{ |
77
|
|
|
if (empty($type)) { |
78
|
|
|
throw new AlertTypeException('Unable to create message with invalid type.'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (empty($content)) { |
82
|
|
|
throw new AlertContentException('Unable to create message with invalid content.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new self($type, $content); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return the stored alert type. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getType() |
94
|
|
|
{ |
95
|
|
|
return $this->type; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the stored alert content. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getContent() |
104
|
|
|
{ |
105
|
|
|
return $this->content; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the stored alert variable if it exists. |
110
|
|
|
* |
111
|
|
|
* @param string $variable ob |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
* |
115
|
|
|
* @throws \Cornford\Alerter\Exceptions\AlertVariableException |
116
|
|
|
*/ |
117
|
|
|
public function __get($variable) |
118
|
|
|
{ |
119
|
|
|
if (!property_exists($this, $variable)) { |
120
|
|
|
throw new AlertVariableException('Unable to find variable: ' . $variable . '.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->{$variable}; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the alert view. |
128
|
|
|
* |
129
|
|
|
* @param string $view |
130
|
|
|
* |
131
|
|
|
* @return self |
132
|
|
|
*/ |
133
|
|
|
public function useView($view) |
134
|
|
|
{ |
135
|
|
|
$this->view = $view; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the alert view path. |
142
|
|
|
* |
143
|
|
|
* @param string $path |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
|
|
public function useViewPath($path) |
148
|
|
|
{ |
149
|
|
|
$this->viewPath = $path; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return a string of alert display HTML for a set display view. |
156
|
|
|
* |
157
|
|
|
* @param string $view |
158
|
|
|
* @param string $viewPath |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function display($viewPath = null, $view = null) |
163
|
|
|
{ |
164
|
|
|
if (!$viewPath) { |
|
|
|
|
165
|
|
|
$viewPath = $this->viewPath; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!$view) { |
|
|
|
|
169
|
|
|
$view = $this->view; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return (new AlertDisplay($this, $viewPath, $view))->display(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.