Added Webhook API (#246)

* Added Webhook API

* code style
This commit is contained in:
Tobias Nyholm 2016-12-07 20:42:24 +01:00 committed by GitHub
parent de13ff66ce
commit b1f949a925
7 changed files with 516 additions and 0 deletions

112
src/Mailgun/Api/Webhook.php Normal file
View File

@ -0,0 +1,112 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Api;
use Mailgun\Assert;
use Mailgun\Resource\Api\Webhook\CreateResponse;
use Mailgun\Resource\Api\Webhook\DeleteResponse;
use Mailgun\Resource\Api\Webhook\IndexResponse;
use Mailgun\Resource\Api\Webhook\ShowResponse;
use Mailgun\Resource\Api\Webhook\UpdateResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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);
}
}

View File

@ -0,0 +1,78 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Webhook;
use Mailgun\Resource\ApiResponse;
/**
* This is only mean to be the base response for Webhook API.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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;
}
}

View File

@ -0,0 +1,17 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Webhook;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class CreateResponse extends BaseResponse
{
}

View File

@ -0,0 +1,17 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Webhook;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class DeleteResponse extends BaseResponse
{
}

View File

@ -0,0 +1,219 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Webhook;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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;
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Webhook;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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'];
}
}
}

View File

@ -0,0 +1,17 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Webhook;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class UpdateResponse extends BaseResponse
{
}