Update Suppression models to php7 (#547)

* PHP7 code in Suppression/Bounce

* PHP7 code in Suppression/Complaint and some fixes

* PHP7 code in Suppression/Unsubscribe and some fixes

* Minor

* Empty constructors, changed DateTimeImmutable instead of DateTime

* Code format

* Refactor and consistency fixes

* Nullable getter return values, nullable address fixes
This commit is contained in:
Radoje Albijanic 2019-01-22 16:43:20 +01:00 committed by Tobias Nyholm
parent 2fe264038b
commit 259d91cb75
7 changed files with 67 additions and 248 deletions

View File

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

View File

@ -16,105 +16,44 @@ namespace Mailgun\Model\Suppression\Bounce;
*/
class Bounce
{
/**
* @var string
*/
private $address;
/**
* @var string
*/
private $code;
/**
* @var string
*/
private $error;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @param string $address
*/
private function __construct($address)
private function __construct()
{
$this->address = $address;
$this->createdAt = new \DateTime();
}
/**
* @return Bounce
*/
public static function create(array $data)
public static function create(array $data): self
{
$bounce = new self($data['address']);
$model = new self();
if (isset($data['code'])) {
$bounce->setCode($data['code']);
}
if (isset($data['error'])) {
$bounce->setError($data['error']);
}
if (isset($data['created_at'])) {
$bounce->setCreatedAt(new \DateTime($data['created_at']));
$model->address = $data['address'] ?? null;
$model->code = $data['code'] ?? null;
$model->error = $data['error'] ?? null;
$model->createdAt = isset($data['created_at']) ? new \DateTimeImmutable($data['created_at']) : null;
return $model;
}
return $bounce;
}
/**
* @return string
*/
public function getAddress()
public function getAddress(): ?string
{
return $this->address;
}
/**
* @return string
*/
public function getCode()
public function getCode(): ?string
{
return $this->code;
}
/**
* @param string $code
*/
private function setCode($code)
{
$this->code = $code;
}
/**
* @return string
*/
public function getError()
public function getError(): ?string
{
return $this->error;
}
/**
* @param string $error
*/
private function setError($error)
{
$this->error = $error;
}
/**
* @return \DateTime
*/
public function getCreatedAt()
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
private function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
}

View File

@ -22,24 +22,13 @@ final class IndexResponse implements ApiResponse, PagingProvider
{
use PaginationResponse;
/**
* @var Bounce[]
*/
private $items;
/**
* @param Bounce[] $items
*/
private function __construct(array $items, array $paging)
private function __construct()
{
$this->items = $items;
$this->paging = $paging;
}
/**
* @return IndexResponse
*/
public static function create(array $data)
public static function create(array $data): self
{
$bounces = [];
if (isset($data['items'])) {
@ -48,13 +37,18 @@ final class IndexResponse implements ApiResponse, PagingProvider
}
}
return new self($bounces, $data['paging']);
$model = new self();
$model->items = $bounces;
$model->paging = $data['paging'];
return $model;
}
/**
* @return Bounce[]
*/
public function getItems()
public function getItems(): array
{
return $this->items;
}

View File

@ -16,57 +16,30 @@ namespace Mailgun\Model\Suppression\Complaint;
*/
class Complaint
{
/**
* @var string
*/
private $address;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @param string $address
*/
private function __construct($address)
private function __construct()
{
$this->address = $address;
$this->createdAt = new \DateTime();
}
/**
* @return Complaint
*/
public static function create(array $data)
public static function create(array $data): self
{
$complaint = new self($data['address']);
$model = new self();
if (isset($data['created_at'])) {
$complaint->setCreatedAt(new \DateTime($data['created_at']));
$model->address = $data['address'] ?? null;
$model->createdAt = isset($data['created_at']) ? new \DateTimeImmutable($data['created_at']) : null;
return $model;
}
return $complaint;
}
/**
* @return string
*/
public function getAddress()
public function getAddress(): ?string
{
return $this->address;
}
/**
* @return \DateTime
*/
public function getCreatedAt()
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
private function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
}

View File

@ -22,24 +22,13 @@ final class IndexResponse implements ApiResponse, PagingProvider
{
use PaginationResponse;
/**
* @var Complaint[]
*/
private $items;
/**
* @param Complaint[] $items
*/
private function __construct(array $items, array $paging)
private function __construct()
{
$this->items = $items;
$this->paging = $paging;
}
/**
* @return IndexResponse
*/
public static function create(array $data)
public static function create(array $data): self
{
$complaints = [];
if (isset($data['items'])) {
@ -48,13 +37,18 @@ final class IndexResponse implements ApiResponse, PagingProvider
}
}
return new self($complaints, $data['paging']);
$model = new self();
$model->items = $complaints;
$model->paging = $data['paging'];
return $model;
}
/**
* @return Complaint[]
*/
public function getItems()
public function getItems(): array
{
return $this->items;
}

View File

@ -41,24 +41,11 @@ final class IndexResponse implements ApiResponse, PagingProvider
*/
private $totalCount;
/**
* @see Mailgun/Model/Suppression/Unsubscribe/Unsubscribe
*
* @param Unsubscribe[] $items
*/
private function __construct(array $items, array $paging)
private function __construct()
{
$this->items = $items;
$this->paging = $paging;
}
/**
* Allow create the unsubscribe items with paging.
*
*
* @return IndexResponse
*/
public static function create(array $data)
public static function create(array $data): self
{
$unsubscribes = [];
if (isset($data['items'])) {
@ -67,29 +54,23 @@ final class IndexResponse implements ApiResponse, PagingProvider
}
}
return new self($unsubscribes, $data['paging']);
$model = new self();
$model->items = $unsubscribes;
$model->paging = $data['paging'];
return $model;
}
/**
* Get the Unsusbscribe item models from the response.
*
* @see Mailgun/Model/Suppression/Unsubscribe/Unsubscribe
*
* @return Unsubscribe[]
*/
public function getItems()
public function getItems(): array
{
return $this->items;
}
/**
* Get the total count of Unsusbscribe in index response.
*
* @see Mailgun/Model/Suppression/Unsubscribe/Unsubscribe
*
* @return int
*/
public function getTotalCount()
public function getTotalCount(): int
{
if (null === $this->totalCount) {
$this->totalCount = count($this->items);

View File

@ -16,80 +16,39 @@ namespace Mailgun\Model\Suppression\Unsubscribe;
*/
class Unsubscribe
{
/**
* @var string
*/
private $address;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var array
*/
private $tags = [];
/**
* @param string $address
*/
private function __construct($address)
private function __construct()
{
$this->address = $address;
$this->createdAt = new \DateTime();
}
/**
* @return Unsubscribe
*/
public static function create(array $data)
public static function create(array $data): self
{
$unsubscribe = new self($data['address']);
$model = new self();
$model->address = $data['address'] ?? null;
$model->createdAt = isset($data['created_at']) ? new \DateTimeImmutable($data['created_at']) : null;
if (isset($data['tags'])) {
$unsubscribe->setTags($data['tags']);
}
if (isset($data['created_at'])) {
$unsubscribe->setCreatedAt(new \DateTime($data['created_at']));
$model->tags = $data['tags'];
}
return $unsubscribe;
return $model;
}
/**
* @return string
*/
public function getAddress()
public function getAddress(): ?string
{
return $this->address;
}
/**
* @return \DateTime
*/
public function getCreatedAt()
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
private function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
/**
* @param array $tags
*/
private function setTags($tags)
{
$this->tags = $tags;
}
/**
* @return array
*/
public function getTags()
public function getTags(): array
{
return $this->tags;
}