diff --git a/src/Mailgun/Api/Webhook.php b/src/Mailgun/Api/Webhook.php new file mode 100644 index 0000000..abbe2da --- /dev/null +++ b/src/Mailgun/Api/Webhook.php @@ -0,0 +1,112 @@ + + */ +class Webhook extends HttpApi +{ + /** + * @param string $domain + * + * @return IndexResponse + */ + public function index($domain) + { + Assert::notEmpty($domain); + $response = $this->httpGet(sprintf('/v3/domains/%s/webhooks', $domain)); + + return $this->deserializer->deserialize($response, IndexResponse::class); + } + + /** + * @param string $domain + * @param string $webhook + * + * @return ShowResponse + */ + public function show($domain, $webhook) + { + Assert::notEmpty($domain); + Assert::notEmpty($webhook); + $response = $this->httpGet(sprintf('/v3/domains/%s/webhooks/%s', $domain, $webhook)); + + return $this->deserializer->deserialize($response, ShowResponse::class); + } + + /** + * @param string $domain + * @param string $id + * @param string $url + * + * @return CreateResponse + */ + public function create($domain, $id, $url) + { + Assert::notEmpty($domain); + Assert::notEmpty($id); + Assert::notEmpty($url); + + $params = [ + 'id' => $id, + 'url' => $url, + ]; + + $response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params); + + return $this->deserializer->deserialize($response, CreateResponse::class); + } + + /** + * @param string $domain + * @param string $id + * @param string $url + * + * @return UpdateResponse + */ + public function update($domain, $id, $url) + { + Assert::notEmpty($domain); + Assert::notEmpty($id); + Assert::notEmpty($url); + + $params = [ + 'url' => $url, + ]; + + $response = $this->httpPut(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id), $params); + + return $this->deserializer->deserialize($response, UpdateResponse::class); + } + + /** + * @param string $domain + * @param string $id + * + * @return DeleteResponse + */ + public function delete($domain, $id) + { + Assert::notEmpty($domain); + Assert::notEmpty($id); + + $response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id)); + + return $this->deserializer->deserialize($response, DeleteResponse::class); + } +} diff --git a/src/Mailgun/Resource/Api/Webhook/BaseResponse.php b/src/Mailgun/Resource/Api/Webhook/BaseResponse.php new file mode 100644 index 0000000..f13e628 --- /dev/null +++ b/src/Mailgun/Resource/Api/Webhook/BaseResponse.php @@ -0,0 +1,78 @@ + + */ +abstract class BaseResponse implements ApiResponse +{ + /** + * @var array + */ + private $webhook = []; + + /** + * @var string + */ + private $message; + + /** + * @param array $webhook + * @param string $message + */ + public function __construct(array $webhook, $message) + { + $this->webhook = $webhook; + $this->message = $message; + } + + /** + * @param array $data + * + * @return static + */ + public static function create(array $data) + { + $webhook = []; + $message = ''; + if (isset($data['webhook'])) { + $webhook = $data['webhook']; + } + + if (isset($data['message'])) { + $message = $data['message']; + } + + return new static($webhook, $message); + } + + /** + * @return string|null + */ + public function getWebhookUrl() + { + if (isset($this->webhook['url'])) { + return $this->webhook['url']; + } + } + + /** + * @return string + */ + public function getMessage() + { + return $this->message; + } +} diff --git a/src/Mailgun/Resource/Api/Webhook/CreateResponse.php b/src/Mailgun/Resource/Api/Webhook/CreateResponse.php new file mode 100644 index 0000000..f389740 --- /dev/null +++ b/src/Mailgun/Resource/Api/Webhook/CreateResponse.php @@ -0,0 +1,17 @@ + + */ +class CreateResponse extends BaseResponse +{ +} diff --git a/src/Mailgun/Resource/Api/Webhook/DeleteResponse.php b/src/Mailgun/Resource/Api/Webhook/DeleteResponse.php new file mode 100644 index 0000000..4bcc414 --- /dev/null +++ b/src/Mailgun/Resource/Api/Webhook/DeleteResponse.php @@ -0,0 +1,17 @@ + + */ +class DeleteResponse extends BaseResponse +{ +} diff --git a/src/Mailgun/Resource/Api/Webhook/IndexResponse.php b/src/Mailgun/Resource/Api/Webhook/IndexResponse.php new file mode 100644 index 0000000..05c4460 --- /dev/null +++ b/src/Mailgun/Resource/Api/Webhook/IndexResponse.php @@ -0,0 +1,219 @@ + + */ +class IndexResponse implements ApiResponse +{ + /** + * @var array + */ + private $bounce = []; + + /** + * @var array + */ + private $deliver = []; + + /** + * @var array + */ + private $drop = []; + + /** + * @var array + */ + private $spam = []; + + /** + * @var array + */ + private $unsubscribe = []; + + /** + * @var array + */ + private $click = []; + + /** + * @var array + */ + private $open = []; + + /** + * Do not let this object be creted without the ::create. + */ + private function __construct() + { + } + + /** + * @param array $data + * + * @return IndexResponse + */ + public static function create(array $data) + { + $self = new self(); + if (isset($data['bounce'])) { + $self->setBounce($data['bounce']); + } + if (isset($data['deliver'])) { + $self->setDeliver($data['deliver']); + } + if (isset($data['drop'])) { + $self->setDrop($data['drop']); + } + if (isset($data['spam'])) { + $self->setSpam($data['spam']); + } + if (isset($data['unsubscribe'])) { + $self->setUnsubscribe($data['unsubscribe']); + } + if (isset($data['click'])) { + $self->setClick($data['click']); + } + if (isset($data['open'])) { + $self->setOpen($data['open']); + } + + return $self; + } + + /** + * @return string|null + */ + public function getBounceUrl() + { + if (isset($this->bounce['url'])) { + return $this->bounce['url']; + } + } + + /** + * @param array $bounce + */ + private function setBounce($bounce) + { + $this->bounce = $bounce; + } + + /** + * @return string|null + */ + public function getDeliverUrl() + { + if (isset($this->deliver['url'])) { + return $this->deliver['url']; + } + } + + /** + * @param array $deliver + */ + private function setDeliver($deliver) + { + $this->deliver = $deliver; + } + + /** + * @return string|null + */ + public function getDropUrl() + { + if (isset($this->drop['url'])) { + return $this->drop['url']; + } + } + + /** + * @param array $drop + */ + private function setDrop($drop) + { + $this->drop = $drop; + } + + /** + * @return string|null + */ + public function getSpamUrl() + { + if (isset($this->spam['url'])) { + return $this->spam['url']; + } + } + + /** + * @param array $spam + */ + private function setSpam($spam) + { + $this->spam = $spam; + } + + /** + * @return string|null + */ + public function getUnsubscribeUrl() + { + if (isset($this->unsubscribe['url'])) { + return $this->unsubscribe['url']; + } + } + + /** + * @param array $unsubscribe + */ + private function setUnsubscribe($unsubscribe) + { + $this->unsubscribe = $unsubscribe; + } + + /** + * @return string|null + */ + public function getClickUrl() + { + if (isset($this->click['url'])) { + return $this->click['url']; + } + } + + /** + * @param array $click + */ + private function setClick($click) + { + $this->click = $click; + } + + /** + * @return string|null + */ + public function getOpenUrl() + { + if (isset($this->open['url'])) { + return $this->open['url']; + } + } + + /** + * @param array $open + */ + private function setOpen($open) + { + $this->open = $open; + } +} diff --git a/src/Mailgun/Resource/Api/Webhook/ShowResponse.php b/src/Mailgun/Resource/Api/Webhook/ShowResponse.php new file mode 100644 index 0000000..bbd22ee --- /dev/null +++ b/src/Mailgun/Resource/Api/Webhook/ShowResponse.php @@ -0,0 +1,56 @@ + + */ +class ShowResponse implements ApiResponse +{ + /** + * @var array + */ + private $webhook = []; + + /** + * @param array $webhook + */ + public function __construct(array $webhook) + { + $this->webhook = $webhook; + } + + /** + * @param array $data + * + * @return ShowResponse + */ + public static function create(array $data) + { + $webhook = []; + if (isset($data['webhook'])) { + $webhook = $data['webhook']; + } + + return new self($webhook); + } + + /** + * @return string|null + */ + public function getWebhookUrl() + { + if (isset($this->webhook['url'])) { + return $this->webhook['url']; + } + } +} diff --git a/src/Mailgun/Resource/Api/Webhook/UpdateResponse.php b/src/Mailgun/Resource/Api/Webhook/UpdateResponse.php new file mode 100644 index 0000000..207267f --- /dev/null +++ b/src/Mailgun/Resource/Api/Webhook/UpdateResponse.php @@ -0,0 +1,17 @@ + + */ +class UpdateResponse extends BaseResponse +{ +}