Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quote response to the appropriate methods #77

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 63 additions & 15 deletions src/Api/DomainsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace SandwaveIo\RealtimeRegister\Api;

use DateTime;
use Exception;
use SandwaveIo\RealtimeRegister\Domain\BillableCollection;
use SandwaveIo\RealtimeRegister\Domain\DomainAvailability;
use SandwaveIo\RealtimeRegister\Domain\DomainContactCollection;
use SandwaveIo\RealtimeRegister\Domain\DomainDetails;
use SandwaveIo\RealtimeRegister\Domain\DomainDetailsCollection;
use SandwaveIo\RealtimeRegister\Domain\DomainQuote;
use SandwaveIo\RealtimeRegister\Domain\DomainRegistration;
use SandwaveIo\RealtimeRegister\Domain\DomainTransferStatus;
use SandwaveIo\RealtimeRegister\Domain\DomainZone;
Expand All @@ -34,6 +36,7 @@ public function list(
?array $parameters = null
): DomainDetailsCollection {
$query = [];

if (! is_null($limit)) {
$query['limit'] = $limit;
}
Expand Down Expand Up @@ -114,7 +117,7 @@ public function register(
?KeyDataCollection $keyData = null,
?BillableCollection $billables = null,
bool $isQuote = false
): DomainRegistration {
): DomainRegistration|DomainQuote {
$payload = [
'customer' => $customer,
'registrant' => $registrant,
Expand Down Expand Up @@ -151,6 +154,10 @@ public function register(
'quote' => $isQuote,
]);

if ($isQuote) {
return DomainQuote::fromArray($response->json());
}

return DomainRegistration::fromArray($response->json());
}

Expand All @@ -176,7 +183,7 @@ public function update(
?KeyDataCollection $keyData = null,
?BillableCollection $billables = null,
bool $isQuote = false
): void {
): DomainQuote|null {
$payload = [];

if (is_string($registrant)) {
Expand Down Expand Up @@ -235,12 +242,22 @@ public function update(
$payload['billables'] = $billables->toArray();
}

$this->client->post("v2/domains/{$domainName}/update", $payload, [
$response = $this->client->post("v2/domains/{$domainName}/update", $payload, [
'quote' => $isQuote,
]);

if ($isQuote) {
return DomainQuote::fromArray($response->json());
}

return null;
}

/* @see https://dm.realtimeregister.com/docs/api/domains/update */
/**
* @see https://dm.realtimeregister.com/docs/api/domains/update
*
* @throws Exception
*/
public function transfer(
string $domainName,
string $customer,
Expand All @@ -257,7 +274,7 @@ public function transfer(
?KeyDataCollection $keyData = null,
?BillableCollection $billables = null,
?bool $isQuote = null
): DomainTransferStatus {
): DomainTransferStatus|DomainQuote {
$payload = [
'customer' => $customer,
'registrant' => $registrant,
Expand Down Expand Up @@ -311,6 +328,11 @@ public function transfer(
$response = $this->client->post("v2/domains/{$domainName}/transfer", $payload, [
'quote' => $isQuote,
]);

if ($isQuote) {
return DomainQuote::fromArray($response->json());
}

return DomainTransferStatus::fromArray($response->json());
}

Expand All @@ -322,20 +344,30 @@ public function pushTransfer(string $domain, string $recipient): void
]);
}

/** @see https://dm.realtimeregister.com/docs/api/domains/transferinfo */
/**
* @see https://dm.realtimeregister.com/docs/api/domains/transferinfo
*
* @throws Exception
*/
public function transferInfo(string $domain, ?string $processId = null): DomainTransferStatus
{
if (null === $processId) {
$endpoint = sprintf('v2/domains/%s/transfer', $domain);
} else {
$endpoint = sprintf('v2/domains/%s/transfer/%s', $domain, $processId);
}

$response = $this->client->get($endpoint);

return DomainTransferStatus::fromArray($response->json());
}

/* @see https://dm.realtimeregister.com/docs/api/domains/transferinfo */
public function renew(string $domain, int $period, ?BillableCollection $billables = null, ?bool $quote = null): DateTime
/**
* @see https://dm.realtimeregister.com/docs/api/domains/transferinfo
*
* @throws Exception
*/
public function renew(string $domain, int $period, ?BillableCollection $billables = null, ?bool $isQuote = null): DateTime|DomainQuote
{
$payload = [
'period' => $period,
Expand All @@ -345,10 +377,14 @@ public function renew(string $domain, int $period, ?BillableCollection $billable
$payload['billables'] = $billables;
}

$response = $this->client->post("v2/domains/{$domain}/renew", $payload, is_null($quote) ? [] : [
'quote' => $quote,
$response = $this->client->post("v2/domains/{$domain}/renew", $payload, is_null($isQuote) ? [] : [
'quote' => $isQuote,
]);

if ($isQuote) {
return DomainQuote::fromArray($response->json());
}

return new DateTime($response->json()['expiryDate']);
}

Expand All @@ -358,9 +394,17 @@ public function delete(string $domain): void
$this->client->delete("v2/domains/{$domain}");
}

/** @see https://dm.realtimeregister.com/docs/api/domains/restore */
public function restore(string $domain, string $reason, ?BillableCollection $billables = null, ?bool $quote = null): DateTime
{
/**
* @see https://dm.realtimeregister.com/docs/api/domains/restore
*
* @throws Exception
*/
public function restore(
string $domain,
string $reason,
?BillableCollection $billables = null,
?bool $isQuote = null
): DateTime|DomainQuote {
$payload = [
'reason' => $reason,
];
Expand All @@ -369,10 +413,14 @@ public function restore(string $domain, string $reason, ?BillableCollection $bil
$payload['billables'] = $billables;
}

$response = $this->client->post("v2/domains/{$domain}/restore", $payload, is_null($quote) ? [] : [
'quote' => $quote,
$response = $this->client->post("v2/domains/{$domain}/restore", $payload, is_null($isQuote) ? [] : [
'quote' => $isQuote,
]);

if ($isQuote) {
return DomainQuote::fromArray($response->json());
}

return new DateTime($response->json()['expiryDate']);
}
}
49 changes: 19 additions & 30 deletions src/Domain/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,39 @@

final class Billable implements DomainObjectInterface
{
public string $product;

public string $action;

public int $quantity;

public ?int $amount;

public ?string $providerName;

private function __construct(
string $product,
string $action,
int $quantity,
?int $amount,
?string $providerName
public function __construct(
public readonly string $product,
public readonly BillableActionEnum $action,
public readonly int $quantity,
public readonly ?int $amount,
public readonly ?bool $refundable,
public readonly ?int $total,
public readonly ?string $providerName
) {
$this->product = $product;
$this->action = $action;
$this->quantity = $quantity;
$this->amount = $amount;
$this->providerName = $providerName;
}

public static function fromArray(array $data): Billable
public static function fromArray(array $json): Billable
{
BillableActionEnum::validate($data['action']);

return new Billable(
$data['product'],
$data['action'],
$data['quantity'],
$data['amount'] ?? null,
$data['providerName'] ?? null
product: $json['product'],
action: BillableActionEnum::from($json['action']),
quantity: $json['quantity'],
amount: $json['amount'] ?? null,
refundable: $json['refundable'] ?? null,
total: $json['total'] ?? null,
providerName: $json['providerName'] ?? null
);
}

public function toArray(): array
{
return array_filter([
'product' => $this->product,
'action' => $this->action,
'action' => $this->action->value,
'quantity' => $this->quantity,
'amount' => $this->amount,
'refundable' => $this->refundable,
'total' => $this->total,
'providerName' => $this->providerName,
], function ($x) {
return ! is_null($x);
Expand Down
28 changes: 28 additions & 0 deletions src/Domain/DomainQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

final class DomainQuote implements DomainObjectInterface
{
public function __construct(
public readonly string $command,
public readonly Quote $quote,
) {
}

public function toArray(): array
{
return [
'command' => $this->command,
'quote' => $this->quote->toArray(),
];
}

public static function fromArray(array $json): DomainQuote
{
return new DomainQuote(
command: $json['command'],
quote: Quote::fromArray($json['quote'])
);
}
}
53 changes: 15 additions & 38 deletions src/Domain/Enum/BillableActionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,20 @@

namespace SandwaveIo\RealtimeRegister\Domain\Enum;

class BillableActionEnum extends AbstractEnum
enum BillableActionEnum: string
{
const ACTION_CREATE = 'CREATE';
const ACTION_REQUEST = 'REQUEST';
const ACTION_TRANSFER = 'TRANSFER';
const ACTION_RENEW = 'RENEW';
const ACTION_RESTORE = 'RESTORE';
const ACTION_TRANSFER_RESTORE = 'TRANSFER_RESTORE';
const ACTION_UPDATE = 'UPDATE';
const ACTION_REGISTRANT_CHANGE = 'REGISTRANT_CHANGE';
const ACTION_LOCAL_CONTACT = 'LOCAL_CONTACT';
const ACTION_NEGATIVE_MARKUP = 'NEGATIVE_MARKUP';
const ACTION_PRIVACY_PROTECT = 'PRIVACY_PROTECT';
const ACTION_EXTRA_WILDCARD = 'EXTRA_WILDCARD';
const ACTION_EXTRA_DOMAIN = 'EXTRA_DOMAIN';
const ACTION_REGISTRY_LOCK = 'REGISTRY_LOCK';

protected static array $values = [
BillableActionEnum::ACTION_CREATE,
BillableActionEnum::ACTION_REQUEST,
BillableActionEnum::ACTION_TRANSFER,
BillableActionEnum::ACTION_RENEW,
BillableActionEnum::ACTION_RESTORE,
BillableActionEnum::ACTION_TRANSFER_RESTORE,
BillableActionEnum::ACTION_UPDATE,
BillableActionEnum::ACTION_REGISTRANT_CHANGE,
BillableActionEnum::ACTION_LOCAL_CONTACT,
BillableActionEnum::ACTION_NEGATIVE_MARKUP,
BillableActionEnum::ACTION_PRIVACY_PROTECT,
BillableActionEnum::ACTION_EXTRA_WILDCARD,
BillableActionEnum::ACTION_EXTRA_DOMAIN,
BillableActionEnum::ACTION_REGISTRY_LOCK,
];

/** @param string $value */
public static function validate($value): void
{
BillableActionEnum::assertValueValid($value);
}
case ACTION_CREATE = 'CREATE';
case ACTION_REQUEST = 'REQUEST';
case ACTION_TRANSFER = 'TRANSFER';
case ACTION_RENEW = 'RENEW';
case ACTION_RESTORE = 'RESTORE';
case ACTION_TRANSFER_RESTORE = 'TRANSFER_RESTORE';
case ACTION_UPDATE = 'UPDATE';
case ACTION_REGISTRANT_CHANGE = 'REGISTRANT_CHANGE';
case ACTION_LOCAL_CONTACT = 'LOCAL_CONTACT';
case ACTION_NEGATIVE_MARKUP = 'NEGATIVE_MARKUP';
case ACTION_PRIVACY_PROTECT = 'PRIVACY_PROTECT';
case ACTION_EXTRA_WILDCARD = 'EXTRA_WILDCARD';
case ACTION_EXTRA_DOMAIN = 'EXTRA_DOMAIN';
case ACTION_REGISTRY_LOCK = 'REGISTRY_LOCK';
}
31 changes: 31 additions & 0 deletions src/Domain/Quote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace SandwaveIo\RealtimeRegister\Domain;

final class Quote implements DomainObjectInterface
{
public function __construct(
public readonly string $currency,
public readonly int $total,
public readonly BillableCollection $billables
) {
}

public function toArray(): array
{
return [
'currency' => $this->currency,
'total' => $this->total,
'billables' => $this->billables->toArray(),
];
}

public static function fromArray(array $json): Quote
{
return new Quote(
currency: $json['currency'],
total: $json['total'],
billables: BillableCollection::fromArray($json['billables'])
);
}
}
Loading
Loading