|
5 | 5 | use CodebarAg\Zendesk\Dto\Tickets\Comments\CommentDTO;
|
6 | 6 | use CodebarAg\Zendesk\Enums\TicketPriority;
|
7 | 7 | use CodebarAg\Zendesk\Enums\TicketType;
|
| 8 | +use Exception; |
| 9 | +use Illuminate\Support\Arr; |
8 | 10 | use Illuminate\Support\Carbon;
|
9 | 11 | use Saloon\Http\Response;
|
10 | 12 | use Spatie\LaravelData\Data;
|
@@ -68,83 +70,87 @@ public function __construct(
|
68 | 70 |
|
69 | 71 | public static function fromResponse(Response $response): self
|
70 | 72 | {
|
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 | + } |
72 | 78 |
|
73 | 79 | return self::fromArray($data);
|
74 | 80 | }
|
75 | 81 |
|
76 | 82 | public static function fromArray(array $data): self
|
77 | 83 | {
|
78 |
| - $comment = array_key_exists('comment', $data) ? $data['comment'] : null; |
| 84 | + $comment = Arr::get($data, 'comment'); |
79 | 85 |
|
80 | 86 | if ($comment && ! $comment instanceof CommentDTO) {
|
81 | 87 | $comment = CommentDTO::fromArray($comment);
|
82 | 88 | }
|
83 | 89 |
|
84 |
| - $priority = array_key_exists('priority', $data) ? $data['priority'] : null; |
| 90 | + $priority = Arr::get($data, 'priority'); |
85 | 91 |
|
86 | 92 | if ($priority && ! $priority instanceof TicketPriority) {
|
87 | 93 | $priority = TicketPriority::tryFrom($priority);
|
88 | 94 | }
|
89 | 95 |
|
90 |
| - $type = array_key_exists('type', $data) ? $data['type'] : null; |
| 96 | + $type = Arr::get($data, 'type'); |
91 | 97 |
|
92 | 98 | if ($type && ! $type instanceof TicketType) {
|
93 | 99 | $type = TicketType::tryFrom($type);
|
94 | 100 | }
|
95 | 101 |
|
96 | 102 | 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'), |
105 | 111 | 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'), |
126 | 132 | 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'), |
140 | 146 | 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'), |
148 | 154 | );
|
149 | 155 | }
|
150 | 156 | }
|
0 commit comments