Skip to content
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
43 changes: 43 additions & 0 deletions src/Dto/Invoices/PdfDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace CodebarAg\Bexio\Dto\Invoices;

use Exception;
use Illuminate\Support\Arr;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;

class PdfDTO extends Data
{
public function __construct(
public string $name,
public int $size,
public string $mime,
public string $content,
) {}

public static function fromResponse(Response $response): self
{
if ($response->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'),
);
}
}
1 change: 0 additions & 1 deletion src/Requests/Invoices/CreateAnInvoiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions src/Requests/Invoices/ShowPdfRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CodebarAg\Bexio\Requests\Invoices;

use CodebarAg\Bexio\Dto\Invoices\PdfDTO;
use Exception;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class ShowPdfRequest extends Request
{
protected Method $method = Method::GET;

public function __construct(
public readonly int $invoice_id,
) {}

public function resolveEndpoint(): string
{
return '/2.0/kb_invoice/'.$this->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);
}
}
Loading