Skip to content

Commit bd6f2fb

Browse files
authored
Merge pull request #18 from codebar-ag/feature-new-requests
Feature New Requests
2 parents b9700cd + db67ae3 commit bd6f2fb

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

src/Dto/Invoices/PdfDTO.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\Invoices;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class PdfDTO extends Data
11+
{
12+
public function __construct(
13+
public string $name,
14+
public int $size,
15+
public string $mime,
16+
public string $content,
17+
) {}
18+
19+
public static function fromResponse(Response $response): self
20+
{
21+
if ($response->failed()) {
22+
throw new \Exception('Failed to create DTO from Response');
23+
}
24+
25+
$data = $response->json();
26+
27+
return self::fromArray($data);
28+
}
29+
30+
public static function fromArray(array $data): self
31+
{
32+
if (! $data) {
33+
throw new Exception('Unable to create DTO. Data missing from response.');
34+
}
35+
36+
return new self(
37+
name: Arr::get($data, 'name'),
38+
size: Arr::get($data, 'size'),
39+
mime: Arr::get($data, 'mime'),
40+
content: Arr::get($data, 'content'),
41+
);
42+
}
43+
}

src/Requests/Invoices/CreateAnInvoiceRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use CodebarAg\Bexio\Dto\Invoices\InvoiceDTO;
66
use Exception;
7-
use Illuminate\Support\Collection;
87
use Saloon\Contracts\Body\HasBody;
98
use Saloon\Enums\Method;
109
use Saloon\Http\Request;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices;
4+
5+
use CodebarAg\Bexio\Dto\Invoices\PdfDTO;
6+
use Exception;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Http\Response;
10+
11+
class ShowPdfRequest extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function __construct(
16+
public readonly int $invoice_id,
17+
) {}
18+
19+
public function resolveEndpoint(): string
20+
{
21+
return '/2.0/kb_invoice/'.$this->invoice_id.'/pdf';
22+
}
23+
24+
public function createDtoFromResponse(Response $response): PdfDTO
25+
{
26+
if (! $response->successful()) {
27+
throw new Exception('Request was not successful. Unable to create DTO.');
28+
}
29+
30+
$res = $response->json();
31+
32+
return PdfDTO::fromArray($res);
33+
}
34+
}

0 commit comments

Comments
 (0)