Skip to content

Commit

Permalink
Merge pull request #8 from codebar-ag/feature-updates
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
StanBarrows authored Aug 8, 2023
2 parents e85009e + 14ee920 commit 6f59224
Show file tree
Hide file tree
Showing 28 changed files with 682 additions and 107 deletions.
14 changes: 10 additions & 4 deletions src/Dto/Tickets/AllTicketsDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CodebarAg\Zendesk\Dto\Tickets;

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

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

if (! $data) {
throw new Exception('Unable to create DTO. Data missing from response.');
}

return new self(
tickets: collect($data['tickets'])->map(function (array $ticket) {
tickets: collect(Arr::get($data, 'tickets'))->map(function (array $ticket) {
return SingleTicketDTO::fromArray($ticket);
})->toArray(),
count: $data['count'],
next_page_url: $data['next_page'],
previous_page_url: $data['previous_page'],
count: Arr::get($data, 'count'),
next_page_url: Arr::get($data, 'next_page'),
previous_page_url: Arr::get($data, 'previous_page'),
);
}
}
29 changes: 15 additions & 14 deletions src/Dto/Tickets/Attachments/AttachmentDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;

use CodebarAg\Zendesk\Enums\MalwareScanResult;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Data;

class AttachmentDTO extends Data
Expand All @@ -27,7 +28,7 @@ public function __construct(

public static function fromArray(array $data): self
{
$thumbnails = $data['thumbnails'] ?? null;
$thumbnails = Arr::get($data, 'thumbnails');

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

return new self(
content_type: $data['content_type'] ?? null,
content_url: $data['content_url'] ?? null,
deleted: $data['deleted'] ?? null,
file_name: $data['file_name'] ?? null,
height: $data['height'] ?? null,
id: $data['id'] ?? null,
inline: $data['inline'] ?? null,
malware_access_override: $data['malware_access_override'] ?? null,
malware_scan_result: MalwareScanResult::tryFrom($data['malware_scan_result'] ?? null),
mapped_content_url: $data['mapped_content_url'] ?? null,
size: $data['size'] ?? null,
content_type: Arr::get($data, 'content_type'),
content_url: Arr::get($data, 'content_url'),
deleted: Arr::get($data, 'deleted'),
file_name: Arr::get($data, 'file_name'),
height: Arr::get($data, 'height'),
id: Arr::get($data, 'id'),
inline: Arr::get($data, 'inline'),
malware_access_override: Arr::get($data, 'malware_access_override'),
malware_scan_result: MalwareScanResult::tryFrom(Arr::get($data, 'malware_scan_result')),
mapped_content_url: Arr::get($data, 'mapped_content_url'),
size: Arr::get($data, 'size'),
thumbnails: $thumbnails ?? null,
url: $data['url'] ?? null,
width: $data['width'] ?? null,
url: Arr::get($data, 'url'),
width: Arr::get($data, 'width'),
);
}

Expand Down
27 changes: 14 additions & 13 deletions src/Dto/Tickets/Attachments/ThumbnailDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;

use CodebarAg\Zendesk\Enums\MalwareScanResult;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Data;

class ThumbnailDTO extends Data
Expand All @@ -27,19 +28,19 @@ public function __construct(
public static function fromArray(array $data): self
{
return new self(
content_type: $data['content_type'] ?? null,
content_url: $data['content_url'] ?? null,
deleted: $data['deleted'] ?? null,
file_name: $data['file_name'] ?? null,
height: $data['height'] ?? null,
id: $data['id'] ?? null,
inline: $data['inline'] ?? null,
malware_access_override: $data['malware_access_override'] ?? null,
malware_scan_result: MalwareScanResult::tryFrom($data['malware_scan_result'] ?? null),
mapped_content_url: $data['mapped_content_url'] ?? null,
size: $data['size'] ?? null,
url: $data['url'] ?? null,
width: $data['width'] ?? null,
content_type: Arr::get($data, 'content_type'),
content_url: Arr::get($data, 'content_url'),
deleted: Arr::get($data, 'deleted'),
file_name: Arr::get($data, 'file_name'),
height: Arr::get($data, 'height'),
id: Arr::get($data, 'id'),
inline: Arr::get($data, 'inline'),
malware_access_override: Arr::get($data, 'malware_access_override'),
malware_scan_result: MalwareScanResult::tryFrom(Arr::get($data, 'malware_scan_result')),
mapped_content_url: Arr::get($data, 'mapped_content_url'),
size: Arr::get($data, 'size'),
url: Arr::get($data, 'url'),
width: Arr::get($data, 'width'),
);
}
}
11 changes: 6 additions & 5 deletions src/Dto/Tickets/Attachments/UploadDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;

use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;
Expand All @@ -18,14 +19,14 @@ public function __construct(

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

return self::fromArray($data);
}

public static function fromArray(array $data): self
{
$attachments = $data['attachments'] ?? null;
$attachments = Arr::get($data, 'attachments');

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

return new self(
token: $data['token'] ?? null,
expires_at: Carbon::parse($data['expires_at'] ?? null),
token: Arr::get($data, 'token'),
expires_at: Carbon::parse(Arr::get($data, 'expires_at')),
attachments: $attachments,
attachment: self::getAttachment($data['attachment'] ?? null),
attachment: self::getAttachment(Arr::get($data, 'attachment')),
);
}

Expand Down
27 changes: 14 additions & 13 deletions src/Dto/Tickets/Comments/CommentDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodebarAg\Zendesk\Dto\Tickets\Comments;

use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Spatie\LaravelData\Data;

Expand All @@ -27,19 +28,19 @@ public function __construct(
public static function fromArray(array $data): self
{
return new self(
attachments: $data['attachments'] ?? null,
audit_id: $data['audit_id'] ?? null,
author_id: $data['author_id'] ?? null,
body: $data['body'] ?? null,
created_at: $data['created_at'] ?? null,
html_body: $data['html_body'] ?? null,
id: $data['id'] ?? null,
metadata: $data['metadata'] ?? null,
plain_body: $data['plain_body'] ?? null,
public: $data['public'] ?? null,
type: $data['type'] ?? null,
uploads: $data['uploads'] ?? null,
via: $data['via'] ?? null,
attachments: Arr::get($data, 'attachments'),
audit_id: Arr::get($data, 'audit_id'),
author_id: Arr::get($data, 'author_id'),
body: Arr::get($data, 'body'),
created_at: Arr::get($data, 'created_at'),
html_body: Arr::get($data, 'html_body'),
id: Arr::get($data, 'id'),
metadata: Arr::get($data, 'metadata'),
plain_body: Arr::get($data, 'plain_body'),
public: Arr::get($data, 'public'),
type: Arr::get($data, 'type'),
uploads: Arr::get($data, 'uploads'),
via: Arr::get($data, 'via'),
);
}
}
8 changes: 7 additions & 1 deletion src/Dto/Tickets/CountTicketsDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CodebarAg\Zendesk\Dto\Tickets;

use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;
Expand All @@ -16,7 +18,11 @@ public function __construct(

public static function fromResponse(Response $response): self
{
$data = $response->json()['count'];
$data = Arr::get($response->json(), 'count');

if (! $data) {
throw new Exception('Unable to create DTO. Data missing from response.');
}

return new self(
value: $data['value'],
Expand Down
110 changes: 58 additions & 52 deletions src/Dto/Tickets/SingleTicketDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use CodebarAg\Zendesk\Dto\Tickets\Comments\CommentDTO;
use CodebarAg\Zendesk\Enums\TicketPriority;
use CodebarAg\Zendesk\Enums\TicketType;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;
Expand Down Expand Up @@ -68,83 +70,87 @@ public function __construct(

public static function fromResponse(Response $response): self
{
$data = $response->json()['ticket'];
$data = Arr::get($response->json(), 'ticket');

if (! $data) {
throw new Exception('Unable to create DTO. Data missing from response.');
}

return self::fromArray($data);
}

public static function fromArray(array $data): self
{
$comment = array_key_exists('comment', $data) ? $data['comment'] : null;
$comment = Arr::get($data, 'comment');

if ($comment && ! $comment instanceof CommentDTO) {
$comment = CommentDTO::fromArray($comment);
}

$priority = array_key_exists('priority', $data) ? $data['priority'] : null;
$priority = Arr::get($data, 'priority');

if ($priority && ! $priority instanceof TicketPriority) {
$priority = TicketPriority::tryFrom($priority);
}

$type = array_key_exists('type', $data) ? $data['type'] : null;
$type = Arr::get($data, 'type');

if ($type && ! $type instanceof TicketType) {
$type = TicketType::tryFrom($type);
}

return new self(
allow_attachments: $data['allow_attachments'] ?? null,
allow_channelback: $data['allow_channelback'] ?? null,
assignee_email: $data['assignee_email'] ?? null,
assignee_id: $data['assignee_id'] ?? null,
attribute_value_ids: $data['attribute_value_ids'] ?? null,
brand_id: $data['brand_id'] ?? null,
collaborator_ids: $data['collaborator_ids'] ?? null,
collaborators: $data['collaborators'] ?? null,
allow_attachments: Arr::get($data, 'allow_attachments'),
allow_channelback: Arr::get($data, 'allow_channelback'),
assignee_email: Arr::get($data, 'assignee_email'),
assignee_id: Arr::get($data, 'assignee_id'),
attribute_value_ids: Arr::get($data, 'attribute_value_ids'),
brand_id: Arr::get($data, 'brand_id'),
collaborator_ids: Arr::get($data, 'collaborator_ids'),
collaborators: Arr::get($data, 'collaborators'),
comment: $comment,
created_at: Carbon::parse($data['created_at'] ?? null),
custom_fields: $data['custom_fields'] ?? null,
description: $data['description'] ?? null,
due_at: Carbon::parse($data['due_at'] ?? null),
email_cc_ids: $data['email_cc_ids'] ?? null,
email_ccs: $data['email_ccs'] ?? null,
external_id: $data['external_id'] ?? null,
follower_ids: $data['follower_ids'] ?? null,
followers: $data['followers'] ?? null,
followup_ids: $data['followup_ids'] ?? null,
forum_topic_id: $data['forum_topic_id'] ?? null,
from_messaging_channel: $data['from_messaging_channel'] ?? null,
group_id: $data['group_id'] ?? null,
has_incidents: $data['has_incidents'] ?? null,
id: $data['id'] ?? null,
is_public: $data['is_public'] ?? null,
macro_id: $data['macro_id'] ?? null,
macro_ids: $data['macro_ids'] ?? null,
metadata: $data['metadata'] ?? null,
organization_id: $data['organization_id'] ?? null,
created_at: Carbon::parse(Arr::get($data, 'created_at')),
custom_fields: Arr::get($data, 'custom_fields'),
description: Arr::get($data, 'description'),
due_at: Carbon::parse(Arr::get($data, 'due_at')),
email_cc_ids: Arr::get($data, 'email_cc_ids'),
email_ccs: Arr::get($data, 'email_ccs'),
external_id: Arr::get($data, 'external_id'),
follower_ids: Arr::get($data, 'follower_ids'),
followers: Arr::get($data, 'followers'),
followup_ids: Arr::get($data, 'followup_ids'),
forum_topic_id: Arr::get($data, 'forum_topic_id'),
from_messaging_channel: Arr::get($data, 'from_messaging_channel'),
group_id: Arr::get($data, 'group_id'),
has_incidents: Arr::get($data, 'has_incidents'),
id: Arr::get($data, 'id'),
is_public: Arr::get($data, 'is_public'),
macro_id: Arr::get($data, 'macro_id'),
macro_ids: Arr::get($data, 'macro_ids'),
metadata: Arr::get($data, 'metadata'),
organization_id: Arr::get($data, 'organization_id'),
priority: $priority,
problem_id: $data['problem_id'] ?? null,
raw_subject: $data['raw_subject'] ?? null,
recipient: $data['recipient'] ?? null,
requester: $data['requester'] ?? null,
requester_id: $data['requester_id'] ?? null,
self_update: $data['self_update'] ?? null,
satisfaction_rating: $data['satisfaction_rating'] ?? null,
sharing_agreement_ids: $data['sharing_agreement_ids'] ?? null,
status: $data['status'] ?? null,
subject: $data['subject'] ?? null,
submitter_id: $data['submitter_id'] ?? null,
tags: $data['tags'] ?? null,
ticket_form_id: $data['ticket_form_id'] ?? null,
problem_id: Arr::get($data, 'problem_id'),
raw_subject: Arr::get($data, 'raw_subject'),
recipient: Arr::get($data, 'recipient'),
requester: Arr::get($data, 'requester'),
requester_id: Arr::get($data, 'requester_id'),
self_update: Arr::get($data, 'self_update'),
satisfaction_rating: Arr::get($data, 'satisfaction_rating'),
sharing_agreement_ids: Arr::get($data, 'sharing_agreement_ids'),
status: Arr::get($data, 'status'),
subject: Arr::get($data, 'subject'),
submitter_id: Arr::get($data, 'submitter_id'),
tags: Arr::get($data, 'tags'),
ticket_form_id: Arr::get($data, 'ticket_form_id'),
type: $type,
updated_at: Carbon::parse($data['updated_at'] ?? null),
updated_stamp: $data['updated_stamp'] ?? null,
url: $data['url'] ?? null,
via: $data['via'] ?? null,
via_followup_source_id: $data['via_followup_source_id'] ?? null,
via_id: $data['via_id'] ?? null,
voice_comment: $data['voice_comment'] ?? null,
updated_at: Carbon::parse(Arr::get($data, 'updated_at')),
updated_stamp: Arr::get($data, 'updated_stamp'),
url: Arr::get($data, 'url'),
via: Arr::get($data, 'via'),
via_followup_source_id: Arr::get($data, 'via_followup_source_id'),
via_id: Arr::get($data, 'via_id'),
voice_comment: Arr::get($data, 'voice_comment'),
);
}
}
5 changes: 5 additions & 0 deletions src/Requests/AllTicketsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodebarAg\Zendesk\Requests;

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

public function createDtoFromResponse(Response $response): mixed
{
if (! $response->successful()) {
throw new Exception('Request was not successful. Unable to create DTO.');
}

return AllTicketsDTO::fromResponse($response);
}
}
Loading

0 comments on commit 6f59224

Please sign in to comment.