Support for new webhook endpoints

This commit is contained in:
Sven Rymenants 2019-06-20 21:26:56 +02:00 committed by David Garcia
parent 196be9f833
commit 26a4f870ab
2 changed files with 203 additions and 21 deletions

View File

@ -18,13 +18,21 @@ use Mailgun\Model\ApiResponse;
*/ */
final class IndexResponse implements ApiResponse final class IndexResponse implements ApiResponse
{ {
private $bounce = []; private $legacy_bounce = [];
private $deliver = []; private $legacy_deliver = [];
private $drop = []; private $legacy_drop = [];
private $spam = []; private $legacy_spam = [];
private $unsubscribe = []; private $legacy_unsubscribe = [];
private $click = []; private $legacy_click = [];
private $open = []; private $legacy_open = [];
private $clicked = [];
private $complained = [];
private $delivered = [];
private $opened = [];
private $permanent_fail = [];
private $temporary_fail = [];
private $unsubscribed = [];
private function __construct() private function __construct()
{ {
@ -36,49 +44,93 @@ final class IndexResponse implements ApiResponse
$data = $data['webhooks'] ?? $data; $data = $data['webhooks'] ?? $data;
$model->bounce = $data['bounce'] ?? []; $model->legacy_bounce = $data['bounce'] ?? [];
$model->deliver = $data['deliver'] ?? []; $model->legacy_deliver = $data['deliver'] ?? [];
$model->drop = $data['drop'] ?? []; $model->legacy_drop = $data['drop'] ?? [];
$model->spam = $data['spam'] ?? []; $model->legacy_spam = $data['spam'] ?? [];
$model->unsubscribe = $data['unsubscribe'] ?? []; $model->legacy_unsubscribe = $data['unsubscribe'] ?? [];
$model->click = $data['click'] ?? []; $model->legacy_click = $data['click'] ?? [];
$model->open = $data['open'] ?? []; $model->legacy_open = $data['open'] ?? [];
$model->clicked = $data['clicked'] ?? [];
$model->complained = $data['complained'] ?? [];
$model->delivered = $data['delivered'] ?? [];
$model->opened = $data['opened'] ?? [];
$model->permanent_fail = $data['permanent_fail'] ?? [];
$model->temporary_fail = $data['temporary_fail'] ?? [];
$model->unsubscribed = $data['unsubscribed'] ?? [];
return $model; return $model;
} }
public function getBounceUrl(): ?string public function getBounceUrl(): ?string
{ {
return $this->bounce['url'] ?? null; return $this->legacy_bounce['url'] ?? null;
} }
public function getDeliverUrl(): ?string public function getDeliverUrl(): ?string
{ {
return $this->deliver['url'] ?? null; return $this->legacy_deliver['url'] ?? null;
} }
public function getDropUrl(): ?string public function getDropUrl(): ?string
{ {
return $this->drop['url'] ?? null; return $this->legacy_drop['url'] ?? null;
} }
public function getSpamUrl(): ?string public function getSpamUrl(): ?string
{ {
return $this->spam['url'] ?? null; return $this->legacy_spam['url'] ?? null;
} }
public function getUnsubscribeUrl() public function getUnsubscribeUrl()
{ {
return $this->unsubscribe['url'] ?? null; return $this->legacy_unsubscribe['url'] ?? null;
} }
public function getClickUrl(): ?string public function getClickUrl(): ?string
{ {
return $this->click['url'] ?? null; return $this->legacy_click['url'] ?? null;
} }
public function getOpenUrl(): ?string public function getOpenUrl(): ?string
{ {
return $this->open['url'] ?? null; return $this->legacy_open['url'] ?? null;
}
public function getClickedUrls(): ?array
{
return $this->clicked['urls'] ?? null;
}
public function getComplainedUrls(): ?array
{
return $this->complained['urls'] ?? null;
}
public function getDeliveredUrls(): ?array
{
return $this->delivered['urls'] ?? null;
}
public function getOpenedUrls(): ?array
{
return $this->opened['urls'] ?? null;
}
public function getPermanentFailUrls(): ?array
{
return $this->permanent_fail['urls'] ?? null;
}
public function getTemporaryFailUrls(): ?array
{
return $this->temporary_fail['urls'] ?? null;
}
public function getUnsubscribeUrls(): ?array
{
return $this->unsubscribed['urls'] ?? null;
} }
} }

View File

@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Webhook;
use Mailgun\Model\Webhook\IndexResponse;
use Mailgun\Tests\Model\BaseModelTest;
class IndexResponseTest extends BaseModelTest
{
public function testCurrent()
{
$json =
<<<'JSON'
{
"webhooks": {
"clicked": {
"urls": [
"http:\/\/example.com\/clicked_1"
]
},
"complained": {
"urls": [
"http:\/\/example.com\/complained_1"
]
},
"delivered": {
"urls": [
"http:\/\/example.com\/delivered_1"
]
},
"opened": {
"urls": [
"http:\/\/example.com\/opened_1"
]
},
"permanent_fail": {
"urls": [
"http:\/\/example.com\/permanent_fail_1"
]
},
"temporary_fail": {
"urls": [
"http:\/\/example.com\/temporary_fail_1"
]
},
"unsubscribed": {
"urls": [
"http:\/\/example.com\/unsubscribed_1"
]
}
}
}
JSON;
$model = IndexResponse::create(json_decode($json, true));
$this->assertContains('http://example.com/clicked_1', $model->getClickedUrls());
$this->assertContains('http://example.com/complained_1', $model->getComplainedUrls());
$this->assertContains('http://example.com/delivered_1', $model->getDeliveredUrls());
$this->assertContains('http://example.com/opened_1', $model->getOpenedUrls());
$this->assertContains('http://example.com/permanent_fail_1', $model->getPermanentFailUrls());
$this->assertContains('http://example.com/temporary_fail_1', $model->getTemporaryFailUrls());
$this->assertContains('http://example.com/unsubscribed_1', $model->getUnsubscribeUrls());
$this->assertNull($model->getBounceUrl());
$this->assertNull($model->getDeliverUrl());
$this->assertNull($model->getDropUrl());
$this->assertNull($model->getSpamUrl());
$this->assertNull($model->getUnsubscribeUrl());
$this->assertNull($model->getClickUrl());
$this->assertNull($model->getOpenUrl());
}
public function testLegacy()
{
$json =
<<<'JSON'
{
"webhooks": {
"click": {
"url": "http:\/\/example.com\/click_1"
},
"bounce": {
"url": "http:\/\/example.com\/bounce_1"
},
"deliver": {
"url": "http:\/\/example.com\/deliver_1"
},
"drop": {
"url": "http:\/\/example.com\/drop_1"
},
"open": {
"url": "http:\/\/example.com\/open_1"
},
"spam": {
"url": "http:\/\/example.com\/spam_1"
},
"unsubscribe": {
"url": "http:\/\/example.com\/unsubscribe_1"
}
}
}
JSON;
$model = IndexResponse::create(json_decode($json, true));
$this->assertEquals('http://example.com/click_1', $model->getClickUrl());
$this->assertEquals('http://example.com/bounce_1', $model->getBounceUrl());
$this->assertEquals('http://example.com/deliver_1', $model->getDeliverUrl());
$this->assertEquals('http://example.com/drop_1', $model->getDropUrl());
$this->assertEquals('http://example.com/open_1', $model->getOpenUrl());
$this->assertEquals('http://example.com/spam_1', $model->getSpamUrl());
$this->assertEquals('http://example.com/unsubscribe_1', $model->getUnsubscribeUrl());
$this->assertNull($model->getClickedUrls());
$this->assertNull($model->getComplainedUrls());
$this->assertNull($model->getDeliveredUrls());
$this->assertNull($model->getOpenedUrls());
$this->assertNull($model->getPermanentFailUrls());
$this->assertNull($model->getTemporaryFailUrls());
$this->assertNull($model->getUnsubscribeUrls());
}
}