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 abstract class BaseResponse implements ApiResponse
{ {
/**
* @var string
*/
private $address; private $address;
/**
* @var string
*/
private $message; private $message;
/** private function __construct()
* @param string $address
* @param string $message
*/
private function __construct($address, $message)
{ {
$this->address = $address;
$this->message = $message;
} }
/** public static function create(array $data): self
* @return BaseResponse
*/
public static function create(array $data)
{ {
$address = isset($data['address']) ? $data['address'] : ''; $model = new static();
$message = isset($data['message']) ? $data['message'] : ''; $model->address = $data['address'] ?? '';
$model->message = $data['message'] ?? '';
return new static($address, $message); return $model;
} }
/** public function getAddress(): string
* @return string
*/
public function getAddress()
{ {
return $this->address; return $this->address;
} }
/** public function getMessage(): string
* @return string
*/
public function getMessage()
{ {
return $this->message; return $this->message;
} }

View File

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

View File

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

View File

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

View File

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