From 26a4f870ab9c01c77f65ed42810c630b93610707 Mon Sep 17 00:00:00 2001 From: Sven Rymenants Date: Thu, 20 Jun 2019 21:26:56 +0200 Subject: [PATCH] Support for new webhook endpoints --- src/Model/Webhook/IndexResponse.php | 94 ++++++++++++---- tests/Model/Webhook/IndexResponseTest.php | 130 ++++++++++++++++++++++ 2 files changed, 203 insertions(+), 21 deletions(-) create mode 100644 tests/Model/Webhook/IndexResponseTest.php diff --git a/src/Model/Webhook/IndexResponse.php b/src/Model/Webhook/IndexResponse.php index 4fcb08f..a22f0fb 100644 --- a/src/Model/Webhook/IndexResponse.php +++ b/src/Model/Webhook/IndexResponse.php @@ -18,13 +18,21 @@ use Mailgun\Model\ApiResponse; */ final class IndexResponse implements ApiResponse { - private $bounce = []; - private $deliver = []; - private $drop = []; - private $spam = []; - private $unsubscribe = []; - private $click = []; - private $open = []; + private $legacy_bounce = []; + private $legacy_deliver = []; + private $legacy_drop = []; + private $legacy_spam = []; + private $legacy_unsubscribe = []; + private $legacy_click = []; + private $legacy_open = []; + + private $clicked = []; + private $complained = []; + private $delivered = []; + private $opened = []; + private $permanent_fail = []; + private $temporary_fail = []; + private $unsubscribed = []; private function __construct() { @@ -36,49 +44,93 @@ final class IndexResponse implements ApiResponse $data = $data['webhooks'] ?? $data; - $model->bounce = $data['bounce'] ?? []; - $model->deliver = $data['deliver'] ?? []; - $model->drop = $data['drop'] ?? []; - $model->spam = $data['spam'] ?? []; - $model->unsubscribe = $data['unsubscribe'] ?? []; - $model->click = $data['click'] ?? []; - $model->open = $data['open'] ?? []; + $model->legacy_bounce = $data['bounce'] ?? []; + $model->legacy_deliver = $data['deliver'] ?? []; + $model->legacy_drop = $data['drop'] ?? []; + $model->legacy_spam = $data['spam'] ?? []; + $model->legacy_unsubscribe = $data['unsubscribe'] ?? []; + $model->legacy_click = $data['click'] ?? []; + $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; } public function getBounceUrl(): ?string { - return $this->bounce['url'] ?? null; + return $this->legacy_bounce['url'] ?? null; } public function getDeliverUrl(): ?string { - return $this->deliver['url'] ?? null; + return $this->legacy_deliver['url'] ?? null; } public function getDropUrl(): ?string { - return $this->drop['url'] ?? null; + return $this->legacy_drop['url'] ?? null; } public function getSpamUrl(): ?string { - return $this->spam['url'] ?? null; + return $this->legacy_spam['url'] ?? null; } public function getUnsubscribeUrl() { - return $this->unsubscribe['url'] ?? null; + return $this->legacy_unsubscribe['url'] ?? null; } public function getClickUrl(): ?string { - return $this->click['url'] ?? null; + return $this->legacy_click['url'] ?? null; } 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; } } diff --git a/tests/Model/Webhook/IndexResponseTest.php b/tests/Model/Webhook/IndexResponseTest.php new file mode 100644 index 0000000..9a94626 --- /dev/null +++ b/tests/Model/Webhook/IndexResponseTest.php @@ -0,0 +1,130 @@ +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()); + } +}