Refactor models to php7

This commit is contained in:
Radoje Albijanic 2019-01-22 18:55:32 +01:00 committed by David Garcia
parent 272033dff2
commit 58a353e5f3
3 changed files with 43 additions and 217 deletions

View File

@ -20,57 +20,28 @@ use Mailgun\Model\ApiResponse;
*/ */
abstract class BaseResponse implements ApiResponse abstract class BaseResponse implements ApiResponse
{ {
/**
* @var array
*/
private $webhook = []; private $webhook = [];
/**
* @var string
*/
private $message; private $message;
/** private function __construct()
* @param string $message
*/
private function __construct(array $webhook, $message)
{ {
$this->webhook = $webhook;
$this->message = $message;
} }
/** public static function create(array $data): self
* @return static
*/
public static function create(array $data)
{ {
$webhook = []; $model = new static();
$message = ''; $model->webhook = $data['webhook'] ?? [];
if (isset($data['webhook'])) { $model->message = $data['message'] ?? '';
$webhook = $data['webhook'];
return $model;
} }
if (isset($data['message'])) { public function getWebhookUrl(): ?string
$message = $data['message'];
}
return new static($webhook, $message);
}
/**
* @return string|null
*/
public function getWebhookUrl()
{ {
if (isset($this->webhook['url'])) { return $this->webhook['url'] ?? null;
return $this->webhook['url'];
}
} }
/** public function getMessage(): string
* @return string
*/
public function getMessage()
{ {
return $this->message; return $this->message;
} }

View File

@ -18,203 +18,68 @@ use Mailgun\Model\ApiResponse;
*/ */
final class IndexResponse implements ApiResponse final class IndexResponse implements ApiResponse
{ {
/**
* @var array
*/
private $bounce = []; private $bounce = [];
/**
* @var array
*/
private $deliver = []; private $deliver = [];
/**
* @var array
*/
private $drop = []; private $drop = [];
/**
* @var array
*/
private $spam = []; private $spam = [];
/**
* @var array
*/
private $unsubscribe = []; private $unsubscribe = [];
/**
* @var array
*/
private $click = []; private $click = [];
/**
* @var array
*/
private $open = []; private $open = [];
/**
* Do not let this object be creted without the ::create.
*/
private function __construct() private function __construct()
{ {
} }
/** public static function create(array $data): self
* @return IndexResponse
*/
public static function create(array $data)
{ {
$self = new self(); $model = new self();
$data = isset($data['webhooks']) ? $data['webhooks'] : $data;
if (isset($data['bounce'])) { $data = $data['webhooks'] ?? $data;
$self->setBounce($data['bounce']);
} $model->bounce = $data['bounce'] ?? [];
if (isset($data['deliver'])) { $model->deliver = $data['deliver'] ?? [];
$self->setDeliver($data['deliver']); $model->drop = $data['drop'] ?? [];
} $model->spam = $data['spam'] ?? [];
if (isset($data['drop'])) { $model->unsubscribe = $data['unsubscribe'] ?? [];
$self->setDrop($data['drop']); $model->click = $data['click'] ?? [];
} $model->open = $data['open'] ?? [];
if (isset($data['spam'])) {
$self->setSpam($data['spam']); return $model;
}
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; public function getBounceUrl(): ?string
}
/**
* @return string|null
*/
public function getBounceUrl()
{ {
if (isset($this->bounce['url'])) { return $this->bounce['url'] ?? null;
return $this->bounce['url'];
}
} }
/** public function getDeliverUrl(): ?string
* @param array $bounce
*/
private function setBounce($bounce)
{ {
$this->bounce = $bounce; return $this->deliver['url'] ?? null;
} }
/** public function getDropUrl(): ?string
* @return string|null
*/
public function getDeliverUrl()
{ {
if (isset($this->deliver['url'])) { return $this->drop['url'] ?? null;
return $this->deliver['url'];
}
} }
/** public function getSpamUrl(): ?string
* @param array $deliver
*/
private function setDeliver($deliver)
{ {
$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() public function getUnsubscribeUrl()
{ {
if (isset($this->unsubscribe['url'])) { return $this->unsubscribe['url'] ?? null;
return $this->unsubscribe['url'];
}
} }
/** public function getClickUrl(): ?string
* @param array $unsubscribe
*/
private function setUnsubscribe($unsubscribe)
{ {
$this->unsubscribe = $unsubscribe; return $this->click['url'] ?? null;
} }
/** public function getOpenUrl(): ?string
* @return string|null
*/
public function getClickUrl()
{ {
if (isset($this->click['url'])) { return $this->open['url'] ?? null;
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

@ -18,36 +18,26 @@ use Mailgun\Model\ApiResponse;
*/ */
class ShowResponse implements ApiResponse class ShowResponse implements ApiResponse
{ {
/**
* @var array
*/
private $webhook = []; private $webhook = [];
private function __construct(array $webhook) private function __construct()
{ {
$this->webhook = $webhook;
} }
/** public static function create(array $data): self
* @return ShowResponse
*/
public static function create(array $data)
{ {
$webhook = []; $model = new self();
if (isset($data['webhook'])) {
$webhook = $data['webhook'];
}
return new self($webhook); $model->webhook = $data['webhook'] ?? [];
return $model;
} }
/** /**
* @return string|null * @return string|null
*/ */
public function getWebhookUrl() public function getWebhookUrl(): ?string
{ {
if (isset($this->webhook['url'])) { return $this->webhook['url'] ?? null;
return $this->webhook['url'];
}
} }
} }