File tree Expand file tree Collapse file tree 3 files changed +77
-1
lines changed Expand file tree Collapse file tree 3 files changed +77
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
5
5
use CodebarAg \Bexio \Dto \Invoices \InvoiceDTO ;
6
6
use Exception ;
7
- use Illuminate \Support \Collection ;
8
7
use Saloon \Contracts \Body \HasBody ;
9
8
use Saloon \Enums \Method ;
10
9
use Saloon \Http \Request ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments