From 58a353e5f33aa7e12d115c0a6854446203cf692f Mon Sep 17 00:00:00 2001 From: Radoje Albijanic Date: Tue, 22 Jan 2019 18:55:32 +0100 Subject: [PATCH] Refactor models to php7 --- src/Model/Webhook/BaseResponse.php | 47 ++----- src/Model/Webhook/IndexResponse.php | 187 ++++------------------------ src/Model/Webhook/ShowResponse.php | 26 ++-- 3 files changed, 43 insertions(+), 217 deletions(-) diff --git a/src/Model/Webhook/BaseResponse.php b/src/Model/Webhook/BaseResponse.php index 92b3862..9a00ca3 100644 --- a/src/Model/Webhook/BaseResponse.php +++ b/src/Model/Webhook/BaseResponse.php @@ -20,57 +20,28 @@ use Mailgun\Model\ApiResponse; */ abstract class BaseResponse implements ApiResponse { - /** - * @var array - */ private $webhook = []; - - /** - * @var string - */ private $message; - /** - * @param string $message - */ - private function __construct(array $webhook, $message) + private function __construct() { - $this->webhook = $webhook; - $this->message = $message; } - /** - * @return static - */ - public static function create(array $data) + public static function create(array $data): self { - $webhook = []; - $message = ''; - if (isset($data['webhook'])) { - $webhook = $data['webhook']; - } + $model = new static(); + $model->webhook = $data['webhook'] ?? []; + $model->message = $data['message'] ?? ''; - if (isset($data['message'])) { - $message = $data['message']; - } - - return new static($webhook, $message); + return $model; } - /** - * @return string|null - */ - public function getWebhookUrl() + public function getWebhookUrl(): ?string { - if (isset($this->webhook['url'])) { - return $this->webhook['url']; - } + return $this->webhook['url'] ?? null; } - /** - * @return string - */ - public function getMessage() + public function getMessage(): string { return $this->message; } diff --git a/src/Model/Webhook/IndexResponse.php b/src/Model/Webhook/IndexResponse.php index b7dc528..b3bde1b 100644 --- a/src/Model/Webhook/IndexResponse.php +++ b/src/Model/Webhook/IndexResponse.php @@ -18,203 +18,68 @@ use Mailgun\Model\ApiResponse; */ final 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() { } - /** - * @return IndexResponse - */ - public static function create(array $data) + public static function create(array $data): self { - $self = new self(); - $data = isset($data['webhooks']) ? $data['webhooks'] : $data; - 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']); - } + $model = new self(); - return $self; + $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'] ?? []; + + return $model; } - /** - * @return string|null - */ - public function getBounceUrl() + public function getBounceUrl(): ?string { - if (isset($this->bounce['url'])) { - return $this->bounce['url']; - } + return $this->bounce['url'] ?? null; } - /** - * @param array $bounce - */ - private function setBounce($bounce) + public function getDeliverUrl(): ?string { - $this->bounce = $bounce; + return $this->deliver['url'] ?? null; } - /** - * @return string|null - */ - public function getDeliverUrl() + public function getDropUrl(): ?string { - if (isset($this->deliver['url'])) { - return $this->deliver['url']; - } + return $this->drop['url'] ?? null; } - /** - * @param array $deliver - */ - private function setDeliver($deliver) + public function getSpamUrl(): ?string { - $this->deliver = $deliver; + return $this->spam['url'] ?? null; } - /** - * @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']; - } + return $this->unsubscribe['url'] ?? null; } - /** - * @param array $unsubscribe - */ - private function setUnsubscribe($unsubscribe) + public function getClickUrl(): ?string { - $this->unsubscribe = $unsubscribe; + return $this->click['url'] ?? null; } - /** - * @return string|null - */ - public function getClickUrl() + public function getOpenUrl(): ?string { - if (isset($this->click['url'])) { - return $this->click['url']; - } + return $this->open['url'] ?? null; } - /** - * @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/Model/Webhook/ShowResponse.php b/src/Model/Webhook/ShowResponse.php index cd3eb4f..2a0d930 100644 --- a/src/Model/Webhook/ShowResponse.php +++ b/src/Model/Webhook/ShowResponse.php @@ -18,36 +18,26 @@ use Mailgun\Model\ApiResponse; */ class ShowResponse implements ApiResponse { - /** - * @var array - */ private $webhook = []; - private function __construct(array $webhook) + private function __construct() { - $this->webhook = $webhook; } - /** - * @return ShowResponse - */ - public static function create(array $data) + public static function create(array $data): self { - $webhook = []; - if (isset($data['webhook'])) { - $webhook = $data['webhook']; - } + $model = new self(); - return new self($webhook); + $model->webhook = $data['webhook'] ?? []; + + return $model; } /** * @return string|null */ - public function getWebhookUrl() + public function getWebhookUrl(): ?string { - if (isset($this->webhook['url'])) { - return $this->webhook['url']; - } + return $this->webhook['url'] ?? null; } }