1
|
|
|
<?php namespace Cornford\Notifier; |
2
|
|
|
|
3
|
|
|
use Cornford\Notifier\Models\Notification; |
4
|
|
|
use DateTime; |
5
|
|
|
use Illuminate\View\Factory as View; |
6
|
|
|
use Illuminate\Session\Store as Session; |
7
|
|
|
|
8
|
|
|
abstract class NotifierAbstract { |
9
|
|
|
|
10
|
|
|
const JS_JQUERY_CDN = '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js'; |
11
|
|
|
const JS_JQUERY_LOCAL = 'packages/cornford/notifier/assets/js/jquery.min.js'; |
12
|
|
|
|
13
|
|
|
const JS_NOTIFY_CDN = '//cdnjs.cloudflare.com/ajax/libs/notify/0.3.2/notify.min.js'; |
14
|
|
|
const JS_NOTIFY_LOCAL = 'packages/cornford/notifier/assets/js/notify.min.js'; |
15
|
|
|
|
16
|
|
|
const JS_NOTIFYER_CDN = 'packages/cornford/notifier/assets/js/notifier.min.js'; |
17
|
|
|
const JS_NOTIFYER_LOCAL = 'packages/cornford/notifier/assets/js/notifier.min.js'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* View instance. |
21
|
|
|
* |
22
|
|
|
* @var \Illuminate\View\Factory |
23
|
|
|
*/ |
24
|
|
|
protected $view; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Session instance. |
28
|
|
|
* |
29
|
|
|
* @var \Illuminate\Session\Store |
30
|
|
|
*/ |
31
|
|
|
protected $session; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Notification Id. |
35
|
|
|
* |
36
|
|
|
* @var integer |
37
|
|
|
*/ |
38
|
|
|
protected static $notificationId = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Options. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $options; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Notifications array. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected static $notifications = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Construct Notifier. |
56
|
|
|
* |
57
|
|
|
* @param View $view |
58
|
|
|
* @param Session $session |
59
|
|
|
* @param array $options |
60
|
|
|
*/ |
61
|
|
|
public function __construct(View $view, Session $session, array $options = []) |
62
|
|
|
{ |
63
|
|
|
$this->view = $view; |
64
|
|
|
$this->session = $session; |
65
|
|
|
$this->options = $options; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create a notification. |
70
|
|
|
* |
71
|
|
|
* @param string $message |
72
|
|
|
* @param string $type |
73
|
|
|
* @param DateTime|integer $expiry |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function notification($message, $type, $expiry = 0) |
78
|
|
|
{ |
79
|
|
|
self::$notificationId++; |
80
|
|
|
|
81
|
|
|
self::$notifications[] = new Notification(self::$notificationId, $message, $type, new DateTime('now'), $expiry); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a default notification. |
86
|
|
|
* |
87
|
|
|
* @param string $message |
88
|
|
|
* @param DateTime|integer $expiry |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function none($message, $expiry = 0) |
93
|
|
|
{ |
94
|
|
|
$this->notification($message, Notification::NOTIFICATION_TYPE_NONE, $expiry); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create an info notification. |
99
|
|
|
* |
100
|
|
|
* @param string $message |
101
|
|
|
* @param DateTime|integer $expiry |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function info($message, $expiry = 0) |
106
|
|
|
{ |
107
|
|
|
$this->notification($message, Notification::NOTIFICATION_TYPE_INFO, $expiry); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create a success notification. |
112
|
|
|
* |
113
|
|
|
* @param string $message |
114
|
|
|
* @param DateTime|integer $expiry |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function success($message, $expiry = 0) |
119
|
|
|
{ |
120
|
|
|
$this->notification($message, Notification::NOTIFICATION_TYPE_SUCCESS, $expiry); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create a warning notification. |
125
|
|
|
* |
126
|
|
|
* @param string $message |
127
|
|
|
* @param DateTime|integer $expiry |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function warning($message, $expiry = 0) |
132
|
|
|
{ |
133
|
|
|
$this->notification($message, Notification::NOTIFICATION_TYPE_WARNING, $expiry); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create a danger notification. |
138
|
|
|
* |
139
|
|
|
* @param string $message |
140
|
|
|
* @param DateTime|integer $expiry |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function danger($message, $expiry = 0) |
145
|
|
|
{ |
146
|
|
|
$this->notification($message, Notification::NOTIFICATION_TYPE_DANGER, $expiry); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set options. |
151
|
|
|
* |
152
|
|
|
* @param array $value |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function setOptions(array $value) |
157
|
|
|
{ |
158
|
|
|
$this->options = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get options. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getOptions() |
167
|
|
|
{ |
168
|
|
|
return $this->options; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set notifications. |
173
|
|
|
* |
174
|
|
|
* @param array $value |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function setNotifications(array $value) |
179
|
|
|
{ |
180
|
|
|
self::$notifications = $value; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get notifications. |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getNotifications() |
189
|
|
|
{ |
190
|
|
|
return self::$notifications; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Include the CDN JS / Local JS files. |
195
|
|
|
* |
196
|
|
|
* @param string $type |
197
|
|
|
* |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
protected function js($type = 'local') |
201
|
|
|
{ |
202
|
|
|
$return = []; |
203
|
|
|
|
204
|
|
|
switch ($type) { |
205
|
|
View Code Duplication |
case 'cdn': |
|
|
|
|
206
|
|
|
$return[] = self::JS_JQUERY_CDN; |
207
|
|
|
$return[] = self::JS_NOTIFY_CDN; |
208
|
|
|
$return[] = self::JS_NOTIFYER_CDN; |
209
|
|
|
break; |
210
|
|
|
case 'local': |
211
|
|
View Code Duplication |
default: |
|
|
|
|
212
|
|
|
$return[] = self::JS_JQUERY_LOCAL; |
213
|
|
|
$return[] = self::JS_NOTIFY_LOCAL; |
214
|
|
|
$return[] = self::JS_NOTIFYER_LOCAL; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $return; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Render assets. |
222
|
|
|
* |
223
|
|
|
* @param string $type |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function assets($type = 'local') |
228
|
|
|
{ |
229
|
|
|
return $this->view->make('notifier::assets') |
|
|
|
|
230
|
|
|
->withOptions(json_encode($this->options)) |
231
|
|
|
->withJavascripts($this->js($type)) |
232
|
|
|
->render(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.