1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Incus |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2014 Warrick Bayman. |
6
|
|
|
* @author Warrick Bayman <[email protected]> |
7
|
|
|
* @license MIT License http://opensource.org/licenses/MIT |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Incus; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Listener |
15
|
|
|
* |
16
|
|
|
* @package Incus |
17
|
|
|
*/ |
18
|
|
|
class Listener implements Contracts\ListenerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* SEND |
22
|
|
|
*/ |
23
|
|
|
const EVENT_SEND = 'send'; |
24
|
|
|
/** |
25
|
|
|
* DEFERRAL |
26
|
|
|
*/ |
27
|
|
|
const EVENT_DEFERRAL = 'deferral'; |
28
|
|
|
/** |
29
|
|
|
* OPEN |
30
|
|
|
*/ |
31
|
|
|
const EVENT_OPEN = 'open'; |
32
|
|
|
/** |
33
|
|
|
* CLICK |
34
|
|
|
*/ |
35
|
|
|
const EVENT_CLICK = 'click'; |
36
|
|
|
/** |
37
|
|
|
* SOFT BOUNCE |
38
|
|
|
*/ |
39
|
|
|
const EVENT_SOFT_BOUNCE = 'soft_bounce'; |
40
|
|
|
/** |
41
|
|
|
* HARD BOUNCE |
42
|
|
|
*/ |
43
|
|
|
const EVENT_HARD_BOUNCE = 'hard_bounce'; |
44
|
|
|
/** |
45
|
|
|
* SPAM |
46
|
|
|
*/ |
47
|
|
|
const EVENT_SPAM = 'spam'; |
48
|
|
|
/** |
49
|
|
|
* UNSUB |
50
|
|
|
*/ |
51
|
|
|
const EVENT_UNSUB = 'unsub'; |
52
|
|
|
/** |
53
|
|
|
* REJECT |
54
|
|
|
*/ |
55
|
|
|
const EVENT_REJECT = 'reject'; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private $eventStore; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Mandrill webhook listener |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function __construct() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->eventStore = []; |
71
|
|
|
if (array_key_exists('mandrill_events', $_POST)) { |
72
|
|
|
|
73
|
|
|
$mandrillEvents = json_decode(stripslashes($_POST['mandrill_events'])); |
74
|
|
|
|
75
|
|
|
if (is_array($mandrillEvents)) { |
76
|
|
|
foreach ($mandrillEvents as $mandrillEvent) { |
77
|
|
|
|
78
|
|
|
$newEvent = new Event(json_encode($mandrillEvent)); |
79
|
|
|
|
80
|
|
|
$this->eventStore[] = $newEvent; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function getEvents() |
88
|
|
|
{ |
89
|
|
|
return $this->eventStore; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* send event handler |
95
|
|
|
* |
96
|
|
|
* @param callable $callback |
97
|
|
|
* |
98
|
|
|
* @return Listener |
99
|
|
|
*/ |
100
|
|
|
public function send(Callable $callback) |
101
|
|
|
{ |
102
|
|
|
foreach ($this->eventStore as $event) { |
103
|
|
|
if ($event->type() === Listener::EVENT_SEND) { |
104
|
|
|
$callback($event); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* deferral event handler |
114
|
|
|
* |
115
|
|
|
* @param callable $callback |
116
|
|
|
* |
117
|
|
|
* @return Listener |
118
|
|
|
*/ |
119
|
|
|
public function deferral(Callable $callback) |
120
|
|
|
{ |
121
|
|
|
foreach ($this->eventStore as $event) { |
122
|
|
|
if ($event->type() === Listener::EVENT_DEFERRAL) { |
123
|
|
|
$callback($event); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* open event handler |
133
|
|
|
* |
134
|
|
|
* @param callable $callback |
135
|
|
|
* |
136
|
|
|
* @return Listener |
137
|
|
|
*/ |
138
|
|
|
public function open(Callable $callback) |
139
|
|
|
{ |
140
|
|
|
foreach ($this->eventStore as $event) { |
141
|
|
|
if ($event->type() === Listener::EVENT_OPEN) { |
142
|
|
|
$callback($event); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* click event handler |
152
|
|
|
* |
153
|
|
|
* @param callable $callback |
154
|
|
|
* |
155
|
|
|
* @return Listener |
156
|
|
|
*/ |
157
|
|
|
public function click(Callable $callback) |
158
|
|
|
{ |
159
|
|
|
foreach ($this->eventStore as $event) { |
160
|
|
|
if ($event->type() === Listener::EVENT_CLICK) { |
161
|
|
|
$callback($event); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* soft bounce event handler |
171
|
|
|
* |
172
|
|
|
* @param callable $callback |
173
|
|
|
* |
174
|
|
|
* @return Listener |
175
|
|
|
*/ |
176
|
|
|
public function softBounce(Callable $callback) |
177
|
|
|
{ |
178
|
|
|
foreach ($this->eventStore as $event) { |
179
|
|
|
if ($event->type() === Listener::EVENT_SOFT_BOUNCE) { |
180
|
|
|
$callback($event); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* hard bounce event handler |
190
|
|
|
* |
191
|
|
|
* @param callable $callback |
192
|
|
|
* |
193
|
|
|
* @return Listener |
194
|
|
|
*/ |
195
|
|
|
public function hardBounce(Callable $callback) |
196
|
|
|
{ |
197
|
|
|
foreach ($this->eventStore as $event) { |
198
|
|
|
if ($event->type() === Listener::EVENT_HARD_BOUNCE) { |
199
|
|
|
$callback($event); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* spam event handler |
209
|
|
|
* |
210
|
|
|
* @param callable $callback |
211
|
|
|
* |
212
|
|
|
* @return Listener |
213
|
|
|
*/ |
214
|
|
|
public function spam(Callable $callback) |
215
|
|
|
{ |
216
|
|
|
foreach ($this->eventStore as $event) { |
217
|
|
|
if ($event->type() === Listener::EVENT_SPAM) { |
218
|
|
|
$callback($event); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* unsub event handler |
228
|
|
|
* |
229
|
|
|
* @param callable $callback |
230
|
|
|
* |
231
|
|
|
* @return Listener |
232
|
|
|
*/ |
233
|
|
|
public function unsub(Callable $callback) |
234
|
|
|
{ |
235
|
|
|
foreach ($this->eventStore as $event) { |
236
|
|
|
if ($event->type() === Listener::EVENT_UNSUB) { |
237
|
|
|
$callback($event); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* reject event handler |
247
|
|
|
* |
248
|
|
|
* @param callable $callback |
249
|
|
|
* |
250
|
|
|
* @return Listener |
251
|
|
|
*/ |
252
|
|
|
public function reject(Callable $callback) |
253
|
|
|
{ |
254
|
|
|
foreach ($this->eventStore as $event) { |
255
|
|
|
if ($event->type() === Listener::EVENT_REJECT) { |
256
|
|
|
$callback($event); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: