Skip to content

Commit

Permalink
Refactor API responses handling and implement new response classes
Browse files Browse the repository at this point in the history
The commit introduced response classes (BaseApiResponse, EmailApiResponse, CreateInvoiceResponse) for better handling and structuring of API responses. It also changed series config references and adjusted return types in the ProformaEndpoint class.
Moreover, the tests are updated to reflect these changes. This will improve the clarity and consistency of response handling across different endpoint methods.
  • Loading branch information
ag84ark committed Jan 27, 2024
1 parent cf35495 commit 7abe0ff
Show file tree
Hide file tree
Showing 18 changed files with 339 additions and 142 deletions.
51 changes: 51 additions & 0 deletions src/ApiResponse/BaseApiResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Ag84ark\SmartBill\ApiResponse;

use Illuminate\Contracts\Support\Arrayable;

class BaseApiResponse implements Arrayable
{
protected string $errorText = '';
protected string $message = '';

protected array $responseData = [];

public static function fromArray(array $data): BaseApiResponse
{
$invoiceResponse = new BaseApiResponse();
$invoiceResponse->errorText = $data['errorText'];
$invoiceResponse->message = $data['message'];

$invoiceResponse->responseData = $data;

return $invoiceResponse;
}

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

public function getErrorText(): string
{
return $this->errorText;
}

public function getMessage(): string
{
return $this->message;
}

/**
* Get all response data
*/
public function getResponseData(): array
{
return $this->responseData;
}
}
24 changes: 4 additions & 20 deletions src/ApiResponse/CreateInvoiceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Ag84ark\SmartBill\ApiResponse;

use Illuminate\Contracts\Support\Arrayable;

class CreateInvoiceResponse implements Arrayable
class CreateInvoiceResponse extends BaseApiResponse
{
private string $errorText = '';
private string $message = '';

private string $number = '';

private string $series = '';
Expand All @@ -23,20 +18,11 @@ public static function fromArray(array $data): CreateInvoiceResponse
$invoiceResponse->number = $data['number'];
$invoiceResponse->series = $data['series'];
$invoiceResponse->url = $data['url'];
$invoiceResponse->responseData = $data;

return $invoiceResponse;
}

public function getErrorText(): string
{
return $this->errorText;
}

public function getMessage(): string
{
return $this->message;
}

public function getNumber(): string
{
return $this->number;
Expand All @@ -54,12 +40,10 @@ public function getUrl(): string

public function toArray(): array
{
return [
'errorText' => $this->errorText,
'message' => $this->message,
return array_merge(parent::toArray(), [
'number' => $this->number,
'series' => $this->series,
'url' => $this->url,
];
]);
}
}
40 changes: 12 additions & 28 deletions src/ApiResponse/CreateProformaInvoiceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Ag84ark\SmartBill\ApiResponse;

use Illuminate\Contracts\Support\Arrayable;

class CreateProformaInvoiceResponse implements Arrayable
class CreateProformaInvoiceResponse extends BaseApiResponse
{
private string $errorText = '';
private string $message = '';

private string $number = '';

private string $series = '';
Expand All @@ -17,24 +12,15 @@ class CreateProformaInvoiceResponse implements Arrayable

public static function fromArray(array $data): CreateProformaInvoiceResponse
{
$invoiceResponse = new CreateProformaInvoiceResponse();
$invoiceResponse->errorText = $data['errorText'];
$invoiceResponse->message = $data['message'];
$invoiceResponse->number = $data['number'];
$invoiceResponse->series = $data['series'];
$invoiceResponse->url = $data['url'];

return $invoiceResponse;
}

public function getErrorText(): string
{
return $this->errorText;
}

public function getMessage(): string
{
return $this->message;
$proformaInvoiceResponse = new CreateProformaInvoiceResponse();
$proformaInvoiceResponse->errorText = $data['errorText'];
$proformaInvoiceResponse->message = $data['message'];
$proformaInvoiceResponse->number = $data['number'];
$proformaInvoiceResponse->series = $data['series'];
$proformaInvoiceResponse->url = $data['url'];
$proformaInvoiceResponse->responseData = $data;

return $proformaInvoiceResponse;
}

public function getNumber(): string
Expand All @@ -54,12 +40,10 @@ public function getUrl(): string

public function toArray(): array
{
return [
'errorText' => $this->errorText,
'message' => $this->message,
return array_merge(parent::toArray(), [
'number' => $this->number,
'series' => $this->series,
'url' => $this->url,
];
]);
}
}
70 changes: 70 additions & 0 deletions src/ApiResponse/EmailApiResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Ag84ark\SmartBill\ApiResponse;

use Illuminate\Contracts\Support\Arrayable;

/**
*
* This class represents an API response for email communication.
* For now, it's only used when the operation is successful.
*
*/
class EmailApiResponse implements Arrayable
{
private string $message;
private int $code;

public function __construct(string $message, int $code)
{
$this->message = $message;
$this->code = $code;
}

public static function fromArray(array $data): EmailApiResponse
{
if (array_key_exists('status', $data)) {
return new EmailApiResponse($data['status']['message'], $data['status']['code']);
}

return new EmailApiResponse($data['message'], $data['code']);
}

public function getMessage(): string
{
return $this->message;
}

public function getCode(): int
{
return $this->code;
}

public function getStatus(): string
{
switch ($this->code) {
case 0:
return 'Success';
case 1:
return 'Eroare de date';
case 2:
return 'Eroare pe server-ul SmartBill Cloud';
default:
return 'Unknown';
}

}

public function wasSuccessful(): bool
{
return $this->code === 0;
}

public function toArray(): array
{
return [
'message' => $this->message,
'code' => $this->code,
];
}
}
5 changes: 3 additions & 2 deletions src/Endpoints/EmailEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ag84ark\SmartBill\Endpoints;

use Ag84ark\SmartBill\ApiResponse\EmailApiResponse;
use Ag84ark\SmartBill\Resources\SendEmail;
use Ag84ark\SmartBill\SmartBillCloudRestClient;

Expand All @@ -12,8 +13,8 @@ public function __construct(SmartBillCloudRestClient $restClient)
parent::__construct($restClient);
}

public function sendEmail(SendEmail $email)
public function sendEmail(SendEmail $email): EmailApiResponse
{
return $this->rest_create(self::EMAIL_URL, $email->toArray());
return EmailApiResponse::fromArray($this->rest_create(self::EMAIL_URL, $email->toArray()));
}
}
33 changes: 21 additions & 12 deletions src/Endpoints/InvoiceEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ag84ark\SmartBill\Endpoints;

use Ag84ark\SmartBill\ApiResponse\BaseApiResponse;
use Ag84ark\SmartBill\ApiResponse\CreateInvoiceResponse;
use Ag84ark\SmartBill\Resources\Invoice;
use Ag84ark\SmartBill\Resources\ReverseInvoices;
Expand Down Expand Up @@ -32,47 +33,55 @@ public function createInvoice(Invoice $invoice): CreateInvoiceResponse
return $this->createInvoiceFromArray($invoice->toArray());
}

public function createReverseInvoice(ReverseInvoices $reverseInvoices)
public function createReverseInvoice(ReverseInvoices $reverseInvoices): BaseApiResponse
{
return $this->createReverseInvoiceFromArray($reverseInvoices->toArray());
}

public function deleteInvoice($number)
public function deleteInvoice($number): BaseApiResponse
{
$url = sprintf(self::INVOICE_URL.self::PARAMS_DELETE, $this->companyVatCode, $this->getEncodedSeriesName(), $number);
$response = $this->rest_delete($url);

return $this->rest_delete($url);
return BaseApiResponse::fromArray($response);
}

public function cancelInvoice($number)
public function cancelInvoice($number): BaseApiResponse
{
$url = sprintf(self::INVOICE_URL.self::PARAMS_CANCEL, $this->companyVatCode, $this->getEncodedSeriesName(), $number);
$response = $this->rest_update($url, '');

return $this->rest_update($url, '');
return BaseApiResponse::fromArray($response);
}

public function restoreInvoice($number)
public function restoreInvoice($number): BaseApiResponse
{
$url = sprintf(self::INVOICE_URL.self::PARAMS_RESTORE, $this->companyVatCode, $this->getEncodedSeriesName(), $number);
$response = $this->rest_update($url, '');

return $this->rest_update($url, '');
return BaseApiResponse::fromArray($response);
}

public function getStatusInvoicePayments($number)
public function getStatusInvoicePayments($number): BaseApiResponse
{
$url = sprintf(self::STATUS_INVOICE_URL.self::PARAMS_STATUS, $this->companyVatCode, $this->getEncodedSeriesName(), $number);
$response = $this->rest_read($url);

return $this->rest_read($url);
return BaseApiResponse::fromArray($response);
}

public function createInvoiceFromArray(array $data): CreateInvoiceResponse
{
return CreateInvoiceResponse::fromArray($this->rest_create(self::INVOICE_URL, $data));
$response = $this->rest_create(self::INVOICE_URL, $data);

return CreateInvoiceResponse::fromArray($response);
}

public function createReverseInvoiceFromArray(array $data)
public function createReverseInvoiceFromArray(array $data): BaseApiResponse
{
return $this->rest_create(self::INVOICE_REVERSE_URL, $data);
$response = $this->rest_create(self::INVOICE_REVERSE_URL, $data);

return BaseApiResponse::fromArray($response);
}

public function setSeriesName(string $seriesName): void
Expand Down
Loading

0 comments on commit 7abe0ff

Please sign in to comment.