From db67ae348d3dd9f5bb98509cb61989e450e988d2 Mon Sep 17 00:00:00 2001 From: Rhys Lees <43909932+RhysLees@users.noreply.github.com> Date: Sat, 26 Oct 2024 19:32:47 +0100 Subject: [PATCH] WIP --- src/Dto/Invoices/PdfDTO.php | 43 +++++++++++++++++++ .../Invoices/CreateAnInvoiceRequest.php | 1 - src/Requests/Invoices/ShowPdfRequest.php | 34 +++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/Dto/Invoices/PdfDTO.php create mode 100644 src/Requests/Invoices/ShowPdfRequest.php diff --git a/src/Dto/Invoices/PdfDTO.php b/src/Dto/Invoices/PdfDTO.php new file mode 100644 index 0000000..74f4091 --- /dev/null +++ b/src/Dto/Invoices/PdfDTO.php @@ -0,0 +1,43 @@ +failed()) { + throw new \Exception('Failed to create DTO from Response'); + } + + $data = $response->json(); + + return self::fromArray($data); + } + + public static function fromArray(array $data): self + { + if (! $data) { + throw new Exception('Unable to create DTO. Data missing from response.'); + } + + return new self( + name: Arr::get($data, 'name'), + size: Arr::get($data, 'size'), + mime: Arr::get($data, 'mime'), + content: Arr::get($data, 'content'), + ); + } +} diff --git a/src/Requests/Invoices/CreateAnInvoiceRequest.php b/src/Requests/Invoices/CreateAnInvoiceRequest.php index b19dcd8..e4f08d4 100644 --- a/src/Requests/Invoices/CreateAnInvoiceRequest.php +++ b/src/Requests/Invoices/CreateAnInvoiceRequest.php @@ -4,7 +4,6 @@ use CodebarAg\Bexio\Dto\Invoices\InvoiceDTO; use Exception; -use Illuminate\Support\Collection; use Saloon\Contracts\Body\HasBody; use Saloon\Enums\Method; use Saloon\Http\Request; diff --git a/src/Requests/Invoices/ShowPdfRequest.php b/src/Requests/Invoices/ShowPdfRequest.php new file mode 100644 index 0000000..129da9d --- /dev/null +++ b/src/Requests/Invoices/ShowPdfRequest.php @@ -0,0 +1,34 @@ +invoice_id.'/pdf'; + } + + public function createDtoFromResponse(Response $response): PdfDTO + { + if (! $response->successful()) { + throw new Exception('Request was not successful. Unable to create DTO.'); + } + + $res = $response->json(); + + return PdfDTO::fromArray($res); + } +}