Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
StanBarrows committed Jul 1, 2023
1 parent 96005c8 commit 5a4a126
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 66 deletions.
3 changes: 1 addition & 2 deletions src/Dto/Tickets/AllTicketsDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CodebarAg\Zendesk\Dto\Tickets;

use Illuminate\Support\Collection;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;

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

return new static(
return new self(
tickets: collect($data['tickets'])->map(function (array $ticket) {
return SingleTicketDTO::fromArray($ticket);
})->toArray(),
Expand Down
4 changes: 1 addition & 3 deletions src/Dto/Tickets/Attachments/AttachmentDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;

use CodebarAg\Zendesk\Enums\MalwareScanResult;
use Illuminate\Support\Carbon;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;

final class AttachmentDTO extends Data
Expand Down Expand Up @@ -37,7 +35,7 @@ public static function fromArray(array $data): self
}
}

return new static(
return new self(
content_type: $data['content_type'] ?? null,
content_url: $data['content_url'] ?? null,
deleted: $data['deleted'] ?? null,
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/Tickets/Attachments/ThumbnailDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(

public static function fromArray(array $data): self
{
return new static(
return new self(
content_type: $data['content_type'] ?? null,
content_url: $data['content_url'] ?? null,
deleted: $data['deleted'] ?? null,
Expand Down
3 changes: 1 addition & 2 deletions src/Dto/Tickets/Attachments/UploadDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CodebarAg\Zendesk\Dto\Tickets\Attachments;

use CodebarAg\Zendesk\Enums\MalwareScanResult;
use Illuminate\Support\Carbon;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;
Expand Down Expand Up @@ -34,7 +33,7 @@ public static function fromArray(array $data): self
}
}

return new static(
return new self(
token: $data['token'] ?? null,
expires_at: Carbon::parse($data['expires_at'] ?? null),
attachments: $attachments,
Expand Down
4 changes: 1 addition & 3 deletions src/Dto/Tickets/Comments/CommentDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace CodebarAg\Zendesk\Dto\Tickets\Comments;

use CodebarAg\Zendesk\Dto\Tickets\Attachments\AttachmentDTO;
use CodebarAg\Zendesk\Dto\Tickets\SingleTicketDTO;
use Illuminate\Support\Carbon;
use Spatie\LaravelData\Data;

Expand All @@ -28,7 +26,7 @@ public function __construct(

public static function fromArray(array $data): self
{
return new static(
return new self(
attachments: $data['attachments'] ?? null,
audit_id: $data['audit_id'] ?? null,
author_id: $data['author_id'] ?? null,
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/Tickets/CountTicketsDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function fromResponse(Response $response): self
{
$data = $response->json()['count'];

return new static(
return new self(
value: $data['value'],
refreshed_at: Carbon::parse($data['refreshed_at']),
);
Expand Down
7 changes: 3 additions & 4 deletions src/Dto/Tickets/SingleTicketDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Carbon;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;
use function Pest\Laravel\instance;

final class SingleTicketDTO extends Data
{
Expand Down Expand Up @@ -82,19 +81,19 @@ public static function fromArray(array $data): self
$comment = CommentDTO::fromArray($comment);
}

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

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

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

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

return new static(
return new self(
allow_attachments: $data['allow_attachments'] ?? null,
allow_channelback: $data['allow_channelback'] ?? null,
assignee_email: $data['assignee_email'] ?? null,
Expand Down
4 changes: 2 additions & 2 deletions src/Requests/CreateAttachmentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CodebarAg\Zendesk\Requests;

use CodebarAg\Zendesk\Dto\Tickets\Attachments\AttachmentDTO;
use CodebarAg\Zendesk\Dto\Tickets\Attachments\UploadDTO;
use Saloon\Contracts\Body\HasBody;
use Saloon\Contracts\Response;
Expand All @@ -25,7 +24,8 @@ public function __construct(
protected string $fileName,
protected string $mimeType,
protected mixed $stream,
) { }
) {
}

protected function defaultHeaders(): array
{
Expand Down
1 change: 0 additions & 1 deletion src/Requests/CreateSingleTicketRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CodebarAg\Zendesk\Requests;

use CodebarAg\Zendesk\Dto\Tickets\CreateTicketDTO;
use CodebarAg\Zendesk\Dto\Tickets\SingleTicketDTO;
use Saloon\Contracts\Body\HasBody;
use Saloon\Contracts\Response;
Expand Down
94 changes: 47 additions & 47 deletions src/ZendeskConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,51 @@

class ZendeskConnector extends Connector
{
public function resolveBaseUrl(): string
{
if (!config('zendesk.subdomain')) {
throw new \Exception('No subdomain provided.', 500);
}

return 'https://' . config('zendesk.subdomain') . '.zendesk.com/api/v2';
}

protected function defaultHeaders(): array
{
return [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
}

protected function defaultAuth(): ?Authenticator
{
if (!config('zendesk.auth.method')) {
throw new \Exception('No authentication method provided.', 500);
}

if (!config('zendesk.auth.email_address')) {
throw new \Exception('No email address provided.', 500);
}

if (config('zendesk.auth.method') === 'basic' && !config('zendesk.auth.password')) {
throw new \Exception('No password provided for basic authentication.', 500);
}

if (config('zendesk.auth.method') === 'basic' && !config('zendesk.auth.password')) {
throw new \Exception('No password provided for basic authentication.', 500);
}

if (config('zendesk.auth.method') === 'token' && !config('zendesk.auth.api_token')) {
throw new \Exception('No API token provided for token authentication.', 500);
}

$authenticationString = match (config('zendesk.auth.method')) {
'basic' => $authenticationString = config('zendesk.auth.email_address') . ':' . config('zendesk.auth.password'),
'token' => $authenticationString = config('zendesk.auth.email_address') . '/token:' . config('zendesk.auth.api_token'),
default => throw new \Exception('Invalid authentication method provided.', 500),
};

return new TokenAuthenticator(base64_encode($authenticationString), 'Basic');
}
public function resolveBaseUrl(): string
{
if (! config('zendesk.subdomain')) {
throw new \Exception('No subdomain provided.', 500);
}

return 'https://'.config('zendesk.subdomain').'.zendesk.com/api/v2';
}

protected function defaultHeaders(): array
{
return [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
}

protected function defaultAuth(): ?Authenticator
{
if (! config('zendesk.auth.method')) {
throw new \Exception('No authentication method provided.', 500);
}

if (! config('zendesk.auth.email_address')) {
throw new \Exception('No email address provided.', 500);
}

if (config('zendesk.auth.method') === 'basic' && ! config('zendesk.auth.password')) {
throw new \Exception('No password provided for basic authentication.', 500);
}

if (config('zendesk.auth.method') === 'basic' && ! config('zendesk.auth.password')) {
throw new \Exception('No password provided for basic authentication.', 500);
}

if (config('zendesk.auth.method') === 'token' && ! config('zendesk.auth.api_token')) {
throw new \Exception('No API token provided for token authentication.', 500);
}

$authenticationString = match (config('zendesk.auth.method')) {
'basic' => $authenticationString = config('zendesk.auth.email_address').':'.config('zendesk.auth.password'),
'token' => $authenticationString = config('zendesk.auth.email_address').'/token:'.config('zendesk.auth.api_token'),
default => throw new \Exception('Invalid authentication method provided.', 500),
};

return new TokenAuthenticator(base64_encode($authenticationString), 'Basic');
}
}

0 comments on commit 5a4a126

Please sign in to comment.