-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor API responses handling and implement new response classes
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
Showing
18 changed files
with
339 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.