@@ -16,6 +16,8 @@ class MailgunApi
16
16
private $ _clicksTrackingMode ;
17
17
private $ _enableOpensTracking ;
18
18
19
+ private $ _enableDryRun = false ;
20
+
19
21
private $ _domain ;
20
22
private $ _key ;
21
23
@@ -50,6 +52,10 @@ public function newMessage()
50
52
*/
51
53
public function sendMessage (MailgunMessage $ message )
52
54
{
55
+ if ($ this ->_enableDryRun ) {
56
+ return '' ;
57
+ }
58
+
53
59
$ response = $ this ->_performRequest ('POST ' , $ this ->_url . $ this ->_domain . '/messages ' , $ message );
54
60
return $ response ['id ' ];
55
61
}
@@ -107,7 +113,7 @@ public function updateMailingList($listAddress, MailgunList $mailingList)
107
113
public function deleteMailingList ($ listAddress )
108
114
{
109
115
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . 'lists/ ' . $ listAddress );
110
- return $ response ['message ' ] == 'Mailing list has been deleted ' ;
116
+ return $ response ['message ' ] === 'Mailing list has been deleted ' ;
111
117
}
112
118
113
119
/**
@@ -161,7 +167,7 @@ public function addMemberToMailingList($listAddress, MailgunListMember $member,
161
167
*/
162
168
public function addMultipleMembersToMailingList ($ listAddress , $ members )
163
169
{
164
- if (count ($ members ) == 0 ) {
170
+ if (count ($ members ) === 0 ) {
165
171
throw new MailgunException ('The members list is empty. ' );
166
172
}
167
173
@@ -206,7 +212,7 @@ public function deleteMailingListMember($listAddress, $memberAddress)
206
212
{
207
213
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . 'lists/ ' . $ listAddress
208
214
. '/members/ ' . $ memberAddress );
209
- return $ response ['message ' ] == 'Mailing list member has been deleted ' ;
215
+ return $ response ['message ' ] === 'Mailing list member has been deleted ' ;
210
216
}
211
217
212
218
/**
@@ -256,7 +262,7 @@ public function getUserUnsubscribes($userAddress)
256
262
public function createUnsubscribe (MailgunUnsubscribe $ unsubscribe )
257
263
{
258
264
$ response = $ this ->_performRequest ('POST ' , $ this ->_url . $ this ->_domain . '/unsubscribes ' , $ unsubscribe );
259
- return $ response ['message ' ] == 'Address has been added to the unsubscribes table ' ;
265
+ return $ response ['message ' ] === 'Address has been added to the unsubscribes table ' ;
260
266
}
261
267
262
268
/**
@@ -266,7 +272,7 @@ public function createUnsubscribe(MailgunUnsubscribe $unsubscribe)
266
272
public function deleteUnsubscribe ($ id )
267
273
{
268
274
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . $ this ->_domain . '/unsubscribes/ ' . $ id );
269
- return $ response ['message ' ] == 'Unsubscribe event has been removed ' ;
275
+ return $ response ['message ' ] === 'Unsubscribe event has been removed ' ;
270
276
}
271
277
272
278
/**
@@ -276,7 +282,7 @@ public function deleteUnsubscribe($id)
276
282
public function deleteUserUnsubscribes ($ userAddress )
277
283
{
278
284
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . $ this ->_domain . '/unsubscribes/ ' . $ userAddress );
279
- return $ response ['message ' ] == 'Unsubscribe event has been removed ' ;
285
+ return $ response ['message ' ] === 'Unsubscribe event has been removed ' ;
280
286
}
281
287
282
288
/**
@@ -312,7 +318,7 @@ public function getComplaint($userAddress)
312
318
public function createComplaint (MailgunComplaint $ complaint )
313
319
{
314
320
$ response = $ this ->_performRequest ('POST ' , $ this ->_url . $ this ->_domain . '/complaints ' , $ complaint );
315
- return $ response ['message ' ] == 'Address has been added to the complaints table ' ;
321
+ return $ response ['message ' ] === 'Address has been added to the complaints table ' ;
316
322
}
317
323
318
324
/**
@@ -322,7 +328,7 @@ public function createComplaint(MailgunComplaint $complaint)
322
328
public function deleteComplaint ($ userAddress )
323
329
{
324
330
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . $ this ->_domain . '/complaints/ ' . $ userAddress );
325
- return $ response ['message ' ] == 'Spam complaint has been removed ' ;
331
+ return $ response ['message ' ] === 'Spam complaint has been removed ' ;
326
332
}
327
333
328
334
/**
@@ -358,7 +364,7 @@ public function getBounce($userAddress)
358
364
public function createBounce (MailgunBounce $ bounce )
359
365
{
360
366
$ response = $ this ->_performRequest ('POST ' , $ this ->_url . $ this ->_domain . '/bounces ' , $ bounce );
361
- return $ response ['message ' ] == 'Address has been added to the bounces table ' ;
367
+ return $ response ['message ' ] === 'Address has been added to the bounces table ' ;
362
368
}
363
369
364
370
/**
@@ -368,7 +374,7 @@ public function createBounce(MailgunBounce $bounce)
368
374
public function deleteBounce ($ userAddress )
369
375
{
370
376
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . $ this ->_domain . '/bounces/ ' . $ userAddress );
371
- return $ response ['message ' ] == 'Bounced address has been removed ' ;
377
+ return $ response ['message ' ] === 'Bounced address has been removed ' ;
372
378
}
373
379
374
380
/**
@@ -424,7 +430,7 @@ public function updateRoute($id, MailgunRoute $route)
424
430
public function deleteRoute ($ id )
425
431
{
426
432
$ response = $ this ->_performRequest ('DELETE ' , $ this ->_url . 'routes/ ' . $ id );
427
- return $ response ['message ' ] == 'Route has been deleted ' ;
433
+ return $ response ['message ' ] === 'Route has been deleted ' ;
428
434
}
429
435
430
436
/**
@@ -587,6 +593,28 @@ public function getIsOpensTrackingEnabled()
587
593
return $ this ->_enableOpensTracking ;
588
594
}
589
595
596
+ /**
597
+ * You can use dry run to test your code without making real API requests
598
+ * Affects only sendMessage()!
599
+ */
600
+ public function enableDryRun ()
601
+ {
602
+ $ this ->_enableDryRun = true ;
603
+ }
604
+
605
+ public function disableDryRun ()
606
+ {
607
+ $ this ->_enableDryRun = false ;
608
+ }
609
+
610
+ /**
611
+ * @return bool
612
+ */
613
+ public function getIsDryRunEnabled ()
614
+ {
615
+ return $ this ->_enableDryRun ;
616
+ }
617
+
590
618
/**
591
619
* @return resource cURL instance
592
620
*/
@@ -636,7 +664,7 @@ protected function _performRequest($method, $url, MailgunObject $object = null,
636
664
throw new MailgunException ('Mailgun server error ' , $ responseStatus );
637
665
} else {
638
666
$ responseArray = json_decode ($ response , true );
639
- if ($ responseStatus == 200 ) {
667
+ if ($ responseStatus === 200 ) {
640
668
return $ responseArray ;
641
669
} else {
642
670
throw new MailgunException (!empty ($ responseArray ['message ' ]) ? $ responseArray ['message ' ] : $ response ,
0 commit comments