Skip to content

Commit

Permalink
Merge pull request #5 from codebar-ag/feature-tickets
Browse files Browse the repository at this point in the history
Feature Tickets
  • Loading branch information
StanBarrows authored Jul 4, 2023
2 parents 305f09a + ff6c98d commit c934b59
Show file tree
Hide file tree
Showing 27 changed files with 496 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/codebar-ag/laravel-zendesk/discussions/new?category=q-a
url: https://github.com/codebar-ag/laravel-zendesk/issues/new
about: Ask the community for help
- name: Request a feature
url: https://github.com/codebar-ag/laravel-zendesk/discussions/new?category=ideas
url: https://github.com/codebar-ag/laravel-zendesk/issues/new
about: Share ideas for new features
- name: Report a bug
url: https://github.com/codebar-ag/laravel-zendesk/issues/new
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ composer require codebar-ag/laravel-zendesk
Optionally, you can publish the config file with:

```bash
php artisan vendor:publish --provider="CodebarAg\Zendesk\ZendeskServiceProvider" --tag="config"
php artisan vendor:publish --provider="CodebarAg\Zendesk\ZendeskServiceProvider"
```

You can add the following env variables to your `.env` file:
Expand Down Expand Up @@ -207,6 +207,8 @@ $uploadResponse = $connector->send(

$token = $uploadResponse->dto()->token;

// And to pass to your ticket request

$ticketResponse = $connector->send(
new CreateSingleTicketRequest(
SingleTicketDTO::fromArray([
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"allow-plugins": {
"composer/package-versions-deprecated": false,
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
Expand Down
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;

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;

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
15 changes: 10 additions & 5 deletions src/ZendeskConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ protected function defaultHeaders(): array
}

protected function defaultAuth(): ?Authenticator
{
$authenticationString = $this->setAuth();

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

public function setAuth(): string
{
if (! config('zendesk.auth.method')) {
throw new \Exception('No authentication method provided.', 500);
Expand All @@ -47,12 +54,10 @@ protected function defaultAuth(): ?Authenticator
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'),
return match (config('zendesk.auth.method')) {
'basic' => config('zendesk.auth.email_address').':'.config('zendesk.auth.password'),
'token' => 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');
}
}
163 changes: 163 additions & 0 deletions tests/Connectors/ZendeskConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

use CodebarAg\Zendesk\ZendeskConnector;

it('will throw an exception if a subdomain is not set', closure: function () {
$connector = new ZendeskConnector;
$connector->resolveBaseUrl();

})->throws('No subdomain provided.', 500);

it('will not throw an exception if a subdomain is set', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
]);

$connector = new ZendeskConnector;
$connector->resolveBaseUrl();

})->expectNotToPerformAssertions();

it('will return the base path', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
]);

$connector = new ZendeskConnector;
$path = $connector->resolveBaseUrl();

expect($path)->toBe('https://codebarsolutionsag.zendesk.com/api/v2');

});

it('will throw an exception if an auth method is not set', closure: function () {
config([
'zendesk.auth.method' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('No authentication method provided.', 500);

it('will throw an exception if an auth method invalid', closure: function () {
config([
'zendesk.auth.method' => 'not-a-valid-method',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('Invalid authentication method provided.', 500);

it('will not throw an exception if an auth method valid', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => 'test-token',
'zendesk.auth.password' => 'test-password',
]);

config([
'zendesk.auth.method' => 'token',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

config([
'zendesk.auth.method' => 'basic',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->expectNotToPerformAssertions();

it('will throw an exception if a token is not provided when using the token method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'token',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('No API token provided for token authentication.', 500);

it('will not throw an exception if a token is provided when using the token method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'token',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => 'test-token',
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->expectNotToPerformAssertions();

it('will throw an exception if a password is not provided when using the basic method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'basic',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('No password provided for basic authentication.', 500);

it('will not throw an exception if a password is provided when using the password method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'basic',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => 'test-password',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->expectNotToPerformAssertions();

it('will compile the correct authentication string for token method', function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'token',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => 'test-token',
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;

$token = $connector->setAuth();

expect($token)->toBe('[email protected]/token:test-token');
});

it('will compile the correct authentication string for basic method', function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'basic',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => 'test-password',
]);

$connector = new ZendeskConnector;

$token = $connector->setAuth();

expect($token)->toBe('[email protected]:test-password');
});
Binary file added tests/Fixtures/Files/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c934b59

Please sign in to comment.