Skip to content

Commit 6f59224

Browse files
authored
Merge pull request #8 from codebar-ag/feature-updates
WIP
2 parents e85009e + 14ee920 commit 6f59224

28 files changed

+682
-107
lines changed

src/Dto/Tickets/AllTicketsDTO.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CodebarAg\Zendesk\Dto\Tickets;
44

5+
use Exception;
6+
use Illuminate\Support\Arr;
57
use Saloon\Http\Response;
68
use Spatie\LaravelData\Data;
79

@@ -19,13 +21,17 @@ public static function fromResponse(Response $response): self
1921
{
2022
$data = $response->json();
2123

24+
if (! $data) {
25+
throw new Exception('Unable to create DTO. Data missing from response.');
26+
}
27+
2228
return new self(
23-
tickets: collect($data['tickets'])->map(function (array $ticket) {
29+
tickets: collect(Arr::get($data, 'tickets'))->map(function (array $ticket) {
2430
return SingleTicketDTO::fromArray($ticket);
2531
})->toArray(),
26-
count: $data['count'],
27-
next_page_url: $data['next_page'],
28-
previous_page_url: $data['previous_page'],
32+
count: Arr::get($data, 'count'),
33+
next_page_url: Arr::get($data, 'next_page'),
34+
previous_page_url: Arr::get($data, 'previous_page'),
2935
);
3036
}
3137
}

src/Dto/Tickets/Attachments/AttachmentDTO.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;
44

55
use CodebarAg\Zendesk\Enums\MalwareScanResult;
6+
use Illuminate\Support\Arr;
67
use Spatie\LaravelData\Data;
78

89
class AttachmentDTO extends Data
@@ -27,7 +28,7 @@ public function __construct(
2728

2829
public static function fromArray(array $data): self
2930
{
30-
$thumbnails = $data['thumbnails'] ?? null;
31+
$thumbnails = Arr::get($data, 'thumbnails');
3132

3233
if ($thumbnails) {
3334
foreach ($thumbnails as $key => $thumbnail) {
@@ -36,20 +37,20 @@ public static function fromArray(array $data): self
3637
}
3738

3839
return new self(
39-
content_type: $data['content_type'] ?? null,
40-
content_url: $data['content_url'] ?? null,
41-
deleted: $data['deleted'] ?? null,
42-
file_name: $data['file_name'] ?? null,
43-
height: $data['height'] ?? null,
44-
id: $data['id'] ?? null,
45-
inline: $data['inline'] ?? null,
46-
malware_access_override: $data['malware_access_override'] ?? null,
47-
malware_scan_result: MalwareScanResult::tryFrom($data['malware_scan_result'] ?? null),
48-
mapped_content_url: $data['mapped_content_url'] ?? null,
49-
size: $data['size'] ?? null,
40+
content_type: Arr::get($data, 'content_type'),
41+
content_url: Arr::get($data, 'content_url'),
42+
deleted: Arr::get($data, 'deleted'),
43+
file_name: Arr::get($data, 'file_name'),
44+
height: Arr::get($data, 'height'),
45+
id: Arr::get($data, 'id'),
46+
inline: Arr::get($data, 'inline'),
47+
malware_access_override: Arr::get($data, 'malware_access_override'),
48+
malware_scan_result: MalwareScanResult::tryFrom(Arr::get($data, 'malware_scan_result')),
49+
mapped_content_url: Arr::get($data, 'mapped_content_url'),
50+
size: Arr::get($data, 'size'),
5051
thumbnails: $thumbnails ?? null,
51-
url: $data['url'] ?? null,
52-
width: $data['width'] ?? null,
52+
url: Arr::get($data, 'url'),
53+
width: Arr::get($data, 'width'),
5354
);
5455
}
5556

src/Dto/Tickets/Attachments/ThumbnailDTO.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;
44

55
use CodebarAg\Zendesk\Enums\MalwareScanResult;
6+
use Illuminate\Support\Arr;
67
use Spatie\LaravelData\Data;
78

89
class ThumbnailDTO extends Data
@@ -27,19 +28,19 @@ public function __construct(
2728
public static function fromArray(array $data): self
2829
{
2930
return new self(
30-
content_type: $data['content_type'] ?? null,
31-
content_url: $data['content_url'] ?? null,
32-
deleted: $data['deleted'] ?? null,
33-
file_name: $data['file_name'] ?? null,
34-
height: $data['height'] ?? null,
35-
id: $data['id'] ?? null,
36-
inline: $data['inline'] ?? null,
37-
malware_access_override: $data['malware_access_override'] ?? null,
38-
malware_scan_result: MalwareScanResult::tryFrom($data['malware_scan_result'] ?? null),
39-
mapped_content_url: $data['mapped_content_url'] ?? null,
40-
size: $data['size'] ?? null,
41-
url: $data['url'] ?? null,
42-
width: $data['width'] ?? null,
31+
content_type: Arr::get($data, 'content_type'),
32+
content_url: Arr::get($data, 'content_url'),
33+
deleted: Arr::get($data, 'deleted'),
34+
file_name: Arr::get($data, 'file_name'),
35+
height: Arr::get($data, 'height'),
36+
id: Arr::get($data, 'id'),
37+
inline: Arr::get($data, 'inline'),
38+
malware_access_override: Arr::get($data, 'malware_access_override'),
39+
malware_scan_result: MalwareScanResult::tryFrom(Arr::get($data, 'malware_scan_result')),
40+
mapped_content_url: Arr::get($data, 'mapped_content_url'),
41+
size: Arr::get($data, 'size'),
42+
url: Arr::get($data, 'url'),
43+
width: Arr::get($data, 'width'),
4344
);
4445
}
4546
}

src/Dto/Tickets/Attachments/UploadDTO.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Carbon;
67
use Saloon\Http\Response;
78
use Spatie\LaravelData\Data;
@@ -18,14 +19,14 @@ public function __construct(
1819

1920
public static function fromResponse(Response $response): self
2021
{
21-
$data = $response->json()['upload'];
22+
$data = Arr::get($response->json(), 'upload');
2223

2324
return self::fromArray($data);
2425
}
2526

2627
public static function fromArray(array $data): self
2728
{
28-
$attachments = $data['attachments'] ?? null;
29+
$attachments = Arr::get($data, 'attachments');
2930

3031
if ($attachments) {
3132
foreach ($attachments as $key => $attachment) {
@@ -34,10 +35,10 @@ public static function fromArray(array $data): self
3435
}
3536

3637
return new self(
37-
token: $data['token'] ?? null,
38-
expires_at: Carbon::parse($data['expires_at'] ?? null),
38+
token: Arr::get($data, 'token'),
39+
expires_at: Carbon::parse(Arr::get($data, 'expires_at')),
3940
attachments: $attachments,
40-
attachment: self::getAttachment($data['attachment'] ?? null),
41+
attachment: self::getAttachment(Arr::get($data, 'attachment')),
4142
);
4243
}
4344

src/Dto/Tickets/Comments/CommentDTO.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodebarAg\Zendesk\Dto\Tickets\Comments;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Carbon;
67
use Spatie\LaravelData\Data;
78

@@ -27,19 +28,19 @@ public function __construct(
2728
public static function fromArray(array $data): self
2829
{
2930
return new self(
30-
attachments: $data['attachments'] ?? null,
31-
audit_id: $data['audit_id'] ?? null,
32-
author_id: $data['author_id'] ?? null,
33-
body: $data['body'] ?? null,
34-
created_at: $data['created_at'] ?? null,
35-
html_body: $data['html_body'] ?? null,
36-
id: $data['id'] ?? null,
37-
metadata: $data['metadata'] ?? null,
38-
plain_body: $data['plain_body'] ?? null,
39-
public: $data['public'] ?? null,
40-
type: $data['type'] ?? null,
41-
uploads: $data['uploads'] ?? null,
42-
via: $data['via'] ?? null,
31+
attachments: Arr::get($data, 'attachments'),
32+
audit_id: Arr::get($data, 'audit_id'),
33+
author_id: Arr::get($data, 'author_id'),
34+
body: Arr::get($data, 'body'),
35+
created_at: Arr::get($data, 'created_at'),
36+
html_body: Arr::get($data, 'html_body'),
37+
id: Arr::get($data, 'id'),
38+
metadata: Arr::get($data, 'metadata'),
39+
plain_body: Arr::get($data, 'plain_body'),
40+
public: Arr::get($data, 'public'),
41+
type: Arr::get($data, 'type'),
42+
uploads: Arr::get($data, 'uploads'),
43+
via: Arr::get($data, 'via'),
4344
);
4445
}
4546
}

src/Dto/Tickets/CountTicketsDTO.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CodebarAg\Zendesk\Dto\Tickets;
44

5+
use Exception;
6+
use Illuminate\Support\Arr;
57
use Illuminate\Support\Carbon;
68
use Saloon\Http\Response;
79
use Spatie\LaravelData\Data;
@@ -16,7 +18,11 @@ public function __construct(
1618

1719
public static function fromResponse(Response $response): self
1820
{
19-
$data = $response->json()['count'];
21+
$data = Arr::get($response->json(), 'count');
22+
23+
if (! $data) {
24+
throw new Exception('Unable to create DTO. Data missing from response.');
25+
}
2026

2127
return new self(
2228
value: $data['value'],

src/Dto/Tickets/SingleTicketDTO.php

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use CodebarAg\Zendesk\Dto\Tickets\Comments\CommentDTO;
66
use CodebarAg\Zendesk\Enums\TicketPriority;
77
use CodebarAg\Zendesk\Enums\TicketType;
8+
use Exception;
9+
use Illuminate\Support\Arr;
810
use Illuminate\Support\Carbon;
911
use Saloon\Http\Response;
1012
use Spatie\LaravelData\Data;
@@ -68,83 +70,87 @@ public function __construct(
6870

6971
public static function fromResponse(Response $response): self
7072
{
71-
$data = $response->json()['ticket'];
73+
$data = Arr::get($response->json(), 'ticket');
74+
75+
if (! $data) {
76+
throw new Exception('Unable to create DTO. Data missing from response.');
77+
}
7278

7379
return self::fromArray($data);
7480
}
7581

7682
public static function fromArray(array $data): self
7783
{
78-
$comment = array_key_exists('comment', $data) ? $data['comment'] : null;
84+
$comment = Arr::get($data, 'comment');
7985

8086
if ($comment && ! $comment instanceof CommentDTO) {
8187
$comment = CommentDTO::fromArray($comment);
8288
}
8389

84-
$priority = array_key_exists('priority', $data) ? $data['priority'] : null;
90+
$priority = Arr::get($data, 'priority');
8591

8692
if ($priority && ! $priority instanceof TicketPriority) {
8793
$priority = TicketPriority::tryFrom($priority);
8894
}
8995

90-
$type = array_key_exists('type', $data) ? $data['type'] : null;
96+
$type = Arr::get($data, 'type');
9197

9298
if ($type && ! $type instanceof TicketType) {
9399
$type = TicketType::tryFrom($type);
94100
}
95101

96102
return new self(
97-
allow_attachments: $data['allow_attachments'] ?? null,
98-
allow_channelback: $data['allow_channelback'] ?? null,
99-
assignee_email: $data['assignee_email'] ?? null,
100-
assignee_id: $data['assignee_id'] ?? null,
101-
attribute_value_ids: $data['attribute_value_ids'] ?? null,
102-
brand_id: $data['brand_id'] ?? null,
103-
collaborator_ids: $data['collaborator_ids'] ?? null,
104-
collaborators: $data['collaborators'] ?? null,
103+
allow_attachments: Arr::get($data, 'allow_attachments'),
104+
allow_channelback: Arr::get($data, 'allow_channelback'),
105+
assignee_email: Arr::get($data, 'assignee_email'),
106+
assignee_id: Arr::get($data, 'assignee_id'),
107+
attribute_value_ids: Arr::get($data, 'attribute_value_ids'),
108+
brand_id: Arr::get($data, 'brand_id'),
109+
collaborator_ids: Arr::get($data, 'collaborator_ids'),
110+
collaborators: Arr::get($data, 'collaborators'),
105111
comment: $comment,
106-
created_at: Carbon::parse($data['created_at'] ?? null),
107-
custom_fields: $data['custom_fields'] ?? null,
108-
description: $data['description'] ?? null,
109-
due_at: Carbon::parse($data['due_at'] ?? null),
110-
email_cc_ids: $data['email_cc_ids'] ?? null,
111-
email_ccs: $data['email_ccs'] ?? null,
112-
external_id: $data['external_id'] ?? null,
113-
follower_ids: $data['follower_ids'] ?? null,
114-
followers: $data['followers'] ?? null,
115-
followup_ids: $data['followup_ids'] ?? null,
116-
forum_topic_id: $data['forum_topic_id'] ?? null,
117-
from_messaging_channel: $data['from_messaging_channel'] ?? null,
118-
group_id: $data['group_id'] ?? null,
119-
has_incidents: $data['has_incidents'] ?? null,
120-
id: $data['id'] ?? null,
121-
is_public: $data['is_public'] ?? null,
122-
macro_id: $data['macro_id'] ?? null,
123-
macro_ids: $data['macro_ids'] ?? null,
124-
metadata: $data['metadata'] ?? null,
125-
organization_id: $data['organization_id'] ?? null,
112+
created_at: Carbon::parse(Arr::get($data, 'created_at')),
113+
custom_fields: Arr::get($data, 'custom_fields'),
114+
description: Arr::get($data, 'description'),
115+
due_at: Carbon::parse(Arr::get($data, 'due_at')),
116+
email_cc_ids: Arr::get($data, 'email_cc_ids'),
117+
email_ccs: Arr::get($data, 'email_ccs'),
118+
external_id: Arr::get($data, 'external_id'),
119+
follower_ids: Arr::get($data, 'follower_ids'),
120+
followers: Arr::get($data, 'followers'),
121+
followup_ids: Arr::get($data, 'followup_ids'),
122+
forum_topic_id: Arr::get($data, 'forum_topic_id'),
123+
from_messaging_channel: Arr::get($data, 'from_messaging_channel'),
124+
group_id: Arr::get($data, 'group_id'),
125+
has_incidents: Arr::get($data, 'has_incidents'),
126+
id: Arr::get($data, 'id'),
127+
is_public: Arr::get($data, 'is_public'),
128+
macro_id: Arr::get($data, 'macro_id'),
129+
macro_ids: Arr::get($data, 'macro_ids'),
130+
metadata: Arr::get($data, 'metadata'),
131+
organization_id: Arr::get($data, 'organization_id'),
126132
priority: $priority,
127-
problem_id: $data['problem_id'] ?? null,
128-
raw_subject: $data['raw_subject'] ?? null,
129-
recipient: $data['recipient'] ?? null,
130-
requester: $data['requester'] ?? null,
131-
requester_id: $data['requester_id'] ?? null,
132-
self_update: $data['self_update'] ?? null,
133-
satisfaction_rating: $data['satisfaction_rating'] ?? null,
134-
sharing_agreement_ids: $data['sharing_agreement_ids'] ?? null,
135-
status: $data['status'] ?? null,
136-
subject: $data['subject'] ?? null,
137-
submitter_id: $data['submitter_id'] ?? null,
138-
tags: $data['tags'] ?? null,
139-
ticket_form_id: $data['ticket_form_id'] ?? null,
133+
problem_id: Arr::get($data, 'problem_id'),
134+
raw_subject: Arr::get($data, 'raw_subject'),
135+
recipient: Arr::get($data, 'recipient'),
136+
requester: Arr::get($data, 'requester'),
137+
requester_id: Arr::get($data, 'requester_id'),
138+
self_update: Arr::get($data, 'self_update'),
139+
satisfaction_rating: Arr::get($data, 'satisfaction_rating'),
140+
sharing_agreement_ids: Arr::get($data, 'sharing_agreement_ids'),
141+
status: Arr::get($data, 'status'),
142+
subject: Arr::get($data, 'subject'),
143+
submitter_id: Arr::get($data, 'submitter_id'),
144+
tags: Arr::get($data, 'tags'),
145+
ticket_form_id: Arr::get($data, 'ticket_form_id'),
140146
type: $type,
141-
updated_at: Carbon::parse($data['updated_at'] ?? null),
142-
updated_stamp: $data['updated_stamp'] ?? null,
143-
url: $data['url'] ?? null,
144-
via: $data['via'] ?? null,
145-
via_followup_source_id: $data['via_followup_source_id'] ?? null,
146-
via_id: $data['via_id'] ?? null,
147-
voice_comment: $data['voice_comment'] ?? null,
147+
updated_at: Carbon::parse(Arr::get($data, 'updated_at')),
148+
updated_stamp: Arr::get($data, 'updated_stamp'),
149+
url: Arr::get($data, 'url'),
150+
via: Arr::get($data, 'via'),
151+
via_followup_source_id: Arr::get($data, 'via_followup_source_id'),
152+
via_id: Arr::get($data, 'via_id'),
153+
voice_comment: Arr::get($data, 'voice_comment'),
148154
);
149155
}
150156
}

src/Requests/AllTicketsRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodebarAg\Zendesk\Requests;
44

55
use CodebarAg\Zendesk\Dto\Tickets\AllTicketsDTO;
6+
use Exception;
67
use Saloon\Contracts\Response;
78
use Saloon\Enums\Method;
89
use Saloon\Http\Request;
@@ -18,6 +19,10 @@ public function resolveEndpoint(): string
1819

1920
public function createDtoFromResponse(Response $response): mixed
2021
{
22+
if (! $response->successful()) {
23+
throw new Exception('Request was not successful. Unable to create DTO.');
24+
}
25+
2126
return AllTicketsDTO::fromResponse($response);
2227
}
2328
}

0 commit comments

Comments
 (0)