Skip to content

Commit 470d7f6

Browse files
authored
Feature Upgrades (#25)
Implemented Laravel Saloon
1 parent cea3799 commit 470d7f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1610
-468
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,44 @@ $content = Zammad::attachment()->download(
294294
);
295295
```
296296

297+
## Expanding response payloads
298+
299+
You can use the `expand()` method to expand the response with additional data.
300+
301+
See documentation on this in the [Zammad API Docs](https://docs.zammad.org/en/latest/api/intro.html?highlight=expand#response-payloads-expand).
302+
303+
```php
304+
$ticket = Zammad::ticket()->expand()->show(20);
305+
$user = Zammad::user()->expand()->show(20);
306+
$me = Zammad::user()->expand()->me();
307+
```
308+
309+
## Limit search response payloads
310+
311+
You can use the `limit(int $limit = 1)` method to expand the response with additional data.
312+
313+
See documentation on this in the [Zammad API Docs](https://docs.zammad.org/en/latest/api/user.html#search).
314+
315+
```php
316+
$ticket = Zammad::ticket()->limit(1)->search();
317+
$user = Zammad::user()->limit(1)->search();
318+
```
319+
320+
## Paginate list response payloads
321+
322+
You can use the `perPage(int $perPage)` and `page(int $page)` methods to set the page and per page values for the response
323+
Alternatively you can use the `paginate(int $page, int $perPage):` method to set both at once.
324+
325+
See documentation on this in the [Zammad API Docs](https://docs.zammad.org/en/latest/api/intro.html#pagination).
326+
327+
```php
328+
$ticket = Zammad::ticket()->perPage(1)->page(1)->list();
329+
$user = Zammad::user()->perPage(1)->page(1)->list();
330+
331+
$ticket = Zammad::ticket()->paginate(1, 1)->list();
332+
$user = Zammad::user()->paginate(1, 1)->list();
333+
```
334+
297335
## 🏋️ DTO showcase
298336

299337
```php
@@ -369,6 +407,7 @@ CodebarAg\Zammad\DTO\ObjectAttribute {
369407
}
370408
```
371409

410+
372411
## 🔧 Configuration file
373412

374413
You can publish the config file with:
@@ -469,6 +508,25 @@ return [
469508
'ticket' => [
470509
// 'note' => 'string',
471510
],
511+
512+
/*
513+
|--------------------------------------------------------------------------
514+
| Ticket States
515+
|--------------------------------------------------------------------------
516+
|
517+
| The ticket states are used to determine if a ticket is open, closed,
518+
| active or inactive. You can use this information to filter tickets
519+
| by their state. The following states are supported by default.
520+
| https://docs.zammad.org/en/latest/api/ticket/states.html
521+
|
522+
*/
523+
524+
'ticket_states' => [
525+
'open' => [1, 2, 3, 7],
526+
'closed' => [4],
527+
'active' => [1, 2, 3, 4, 7],
528+
'inactive' => [5, 6],
529+
],
472530

473531
];
474532
```

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"php": "^8.2",
2222
"guzzlehttp/guzzle": "^7.5",
2323
"illuminate/contracts": "^10.0",
24-
"spatie/laravel-package-tools": "^1.9.2"
24+
"spatie/laravel-package-tools": "^1.9.2",
25+
"sammyjo20/saloon": "^2.0",
26+
"sammyjo20/saloon-laravel": "^2.0"
2527
},
2628
"require-dev": {
2729
"laravel/pint": "^1.0",

config/zammad.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,23 @@
8888
// 'note' => 'string',
8989
],
9090

91+
/*
92+
|--------------------------------------------------------------------------
93+
| Ticket States
94+
|--------------------------------------------------------------------------
95+
|
96+
| The ticket states are used to determine if a ticket is open, closed,
97+
| active or inactive. You can use this information to filter tickets
98+
| by their state. The following states are supported by default.
99+
| https://docs.zammad.org/en/latest/api/ticket/states.html
100+
|
101+
*/
102+
103+
'ticket_states' => [
104+
'open' => [1, 2, 3, 7],
105+
'closed' => [4],
106+
'active' => [1, 2, 3, 4, 7],
107+
'inactive' => [5, 6],
108+
],
109+
91110
];

src/Classes/RequestClass.php

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace CodebarAg\Zammad\Classes;
44

55
use CodebarAg\Zammad\Events\ZammadResponseLog;
6-
use Illuminate\Http\Client\Response;
7-
use Illuminate\Support\Facades\Http;
6+
use CodebarAg\Zammad\ZammadConnector;
87
use Illuminate\Support\Str;
8+
use Saloon\Exceptions\Request\RequestException;
9+
use Saloon\Http\Request;
10+
use Saloon\Http\Response;
11+
use Throwable;
912

1013
abstract class RequestClass
1114
{
@@ -17,65 +20,46 @@ abstract class RequestClass
1720

1821
protected $objectHasReferenceError;
1922

23+
protected $connector;
24+
2025
public function __construct()
2126
{
2227
$this->httpRetryMaxium = config('zammad.http_retry_maximum');
2328
$this->httpRetryDelay = config('zammad.http_retry_delay');
2429

2530
$this->ignoreReferenceErrorIngore = config('zammad.object_reference_error_ignore');
2631
$this->objectHasReferenceError = config('zammad.objet_reference_error');
27-
}
2832

29-
/**
30-
* @throws \Illuminate\Http\Client\RequestException
31-
*/
32-
public function getRequest($url): Response
33-
{
34-
$response = Http::withToken(config('zammad.token'))
35-
->retry($this->httpRetryMaxium, $this->httpRetryDelay)
36-
->get($url);
37-
38-
event(new ZammadResponseLog($response));
39-
40-
return $response->throw();
33+
$this->connector = new ZammadConnector();
4134
}
4235

43-
/**
44-
* @throws \Illuminate\Http\Client\RequestException
45-
*/
46-
public function postRequest($url, $data = null): Response
36+
private function performRequest(Request $request): Response
4737
{
48-
$response = Http::withToken(config('zammad.token'))
49-
->retry($this->httpRetryMaxium, $this->httpRetryDelay)
50-
->post($url, $data);
51-
52-
event(new ZammadResponseLog($response));
53-
54-
return $response->throw();
38+
return $this->connector->sendAndRetry(
39+
$request,
40+
$this->httpRetryMaxium,
41+
$this->httpRetryDelay,
42+
);
5543
}
5644

5745
/**
58-
* @throws \Illuminate\Http\Client\RequestException
46+
* @throws RequestException|Throwable
5947
*/
60-
public function putRequest($url, $data): Response
48+
public function request(Request $request): Response
6149
{
62-
$response = Http::withToken(config('zammad.token'))
63-
->retry($this->httpRetryMaxium, $this->httpRetryDelay)
64-
->put($url, $data);
50+
$response = $this->performRequest($request);
6551

6652
event(new ZammadResponseLog($response));
6753

68-
return $response->throw();
54+
return $response;
6955
}
7056

7157
/**
72-
* @throws \Illuminate\Http\Client\RequestException
58+
* @throws RequestException|Throwable
7359
*/
74-
public function deleteRequest($url): Response
60+
public function deleteRequest(Request $request): Response
7561
{
76-
$response = Http::withToken(config('zammad.token'))
77-
//->retry($this->httpRetryMaxium, $this->httpRetryDelay)
78-
->delete($url);
62+
$response = $this->performRequest($request);
7963

8064
$ignoreReferenceError = [
8165
'ignore' => $this->ignoreReferenceErrorIngore ? true : false,

src/DTO/Attachment.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function url(): string
4343
}
4444

4545
public static function fake(
46-
?int $id = null,
47-
?int $ticketId = null,
48-
?int $commentId = null,
49-
?int $size = null,
50-
?string $name = null,
51-
?string $type = null,
46+
int $id = null,
47+
int $ticketId = null,
48+
int $commentId = null,
49+
int $size = null,
50+
string $name = null,
51+
string $type = null,
5252
): self {
5353
return new self(
5454
id: $id ?? random_int(1, 9999),

src/DTO/Comment.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,22 @@ public function __construct(
9393
}
9494

9595
public static function fake(
96-
?int $id = null,
97-
?int $type_id = null,
98-
?int $ticket_id = null,
99-
?int $sender_id = null,
100-
?string $sender = null,
101-
?string $subject = null,
102-
?string $body = null,
103-
?string $content_type = null,
104-
?string $from = null,
105-
?string $to = null,
106-
?bool $internal = null,
107-
?int $created_by_id = null,
108-
?int $updated_by_id = null,
109-
?int $origin_by_id = null,
110-
?Carbon $updated_at = null,
111-
?Carbon $created_at = null,
96+
int $id = null,
97+
int $type_id = null,
98+
int $ticket_id = null,
99+
int $sender_id = null,
100+
string $sender = null,
101+
string $subject = null,
102+
string $body = null,
103+
string $content_type = null,
104+
string $from = null,
105+
string $to = null,
106+
bool $internal = null,
107+
int $created_by_id = null,
108+
int $updated_by_id = null,
109+
int $origin_by_id = null,
110+
Carbon $updated_at = null,
111+
Carbon $created_at = null,
112112
): self {
113113
return new self(
114114
id: $id ?? random_int(1, 1000),
@@ -131,4 +131,14 @@ public static function fake(
131131
created_at: $created_at ?? now()->subDay(),
132132
);
133133
}
134+
135+
public function fromName(): string
136+
{
137+
return Str::before(Str::between($this->from, '"', '"'), '<');
138+
}
139+
140+
public function fromEmail(): string
141+
{
142+
return Str::between($this->from, '<', '>');
143+
}
134144
}

src/DTO/ObjectAttribute.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public function __construct(
3535
}
3636

3737
public static function fake(
38-
?int $id = null,
39-
?int $name = null,
40-
?int $object_lookup_id = null,
41-
?string $display = null,
42-
?string $data_type = null,
43-
?int $position = null,
44-
?array $data_option = null,
45-
?array $data_option_new = null,
38+
int $id = null,
39+
int $name = null,
40+
int $object_lookup_id = null,
41+
string $display = null,
42+
string $data_type = null,
43+
int $position = null,
44+
array $data_option = null,
45+
array $data_option_new = null,
4646
): self {
4747
return new self(
4848
id: $id ?? random_int(1, 1000),
@@ -61,7 +61,7 @@ public static function fakeCreateToArray(
6161
?string $object = 'Ticket',
6262
?string $display = 'Sample Object',
6363
?bool $active = true,
64-
?int $position = null,
64+
int $position = null,
6565
?string $data_type = 'select',
6666
?array $data_options = [
6767
'options' => [

0 commit comments

Comments
 (0)