1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigitalEquation\Teamwork\Services; |
4
|
|
|
|
5
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkHttpException; |
6
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkParameterException; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use GuzzleHttp\Psr7\Stream; |
11
|
|
|
|
12
|
|
|
class Tickets |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \GuzzleHttp\Client |
16
|
|
|
*/ |
17
|
|
|
private Client $client; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Tickets constructor. |
21
|
|
|
* |
22
|
|
|
* @param \GuzzleHttp\Client $client |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Client $client) |
25
|
|
|
{ |
26
|
|
|
$this->client = $client; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get tickets priorities. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
34
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
35
|
|
|
* @throws \JsonException |
36
|
|
|
*/ |
37
|
|
|
public function priorities(): array |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
/** @var Response $response */ |
41
|
|
|
$response = $this->client->get('ticketpriorities.json'); |
42
|
|
|
/** @var Stream $body */ |
43
|
|
|
$body = $response->getBody(); |
44
|
|
|
|
45
|
|
|
return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR); |
46
|
|
|
} catch (ClientException $e) { |
47
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get a list of tickets for a customer. |
53
|
|
|
* |
54
|
|
|
* @param int $customerId |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
58
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
59
|
|
|
* @throws \JsonException |
60
|
|
|
*/ |
61
|
|
|
public function customer(int $customerId): array |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
/** @var Response $response */ |
65
|
|
|
$response = $this->client->get(sprintf('customers/%s/previoustickets.json', $customerId)); |
66
|
|
|
/** @var Stream $body */ |
67
|
|
|
$body = $response->getBody(); |
68
|
|
|
|
69
|
|
|
return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR); |
70
|
|
|
} catch (ClientException $e) { |
71
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Send a ticket to teamwork desk. |
77
|
|
|
* |
78
|
|
|
* @param array $data |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
82
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
83
|
|
|
* @throws \JsonException |
84
|
|
|
*/ |
85
|
|
|
public function post(array $data): array |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
/** @var Response $response */ |
89
|
|
|
$response = $this->client->post('tickets.json', [ |
90
|
|
|
'form_params' => $data, |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
/** @var Stream $body */ |
94
|
|
|
$body = $response->getBody(); |
95
|
|
|
|
96
|
|
|
return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR); |
97
|
|
|
} catch (ClientException $e) { |
98
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Post a reply to a ticket. |
104
|
|
|
* |
105
|
|
|
* @param array $data |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
109
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkParameterException |
110
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
111
|
|
|
* @throws \JsonException |
112
|
|
|
*/ |
113
|
|
|
public function reply(array $data): array |
114
|
|
|
{ |
115
|
|
|
if (empty($data['ticketId'])) { |
116
|
|
|
throw new TeamworkParameterException('The `reply` method expects the passed array param to contain `ticketId`', 400); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
/** @var Response $response */ |
121
|
|
|
$response = $this->client->post(sprintf('tickets/%s.json', $data['ticketId']), [ |
122
|
|
|
'form_params' => $data, |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
/** @var Stream $body */ |
126
|
|
|
$body = $response->getBody(); |
127
|
|
|
|
128
|
|
|
return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR); |
129
|
|
|
} catch (ClientException $e) { |
130
|
|
|
throw new TeamworkHttpException($e->getMessage()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get ticket by id. |
136
|
|
|
* |
137
|
|
|
* @param int $ticketId |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
141
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
142
|
|
|
* @throws \JsonException |
143
|
|
|
*/ |
144
|
|
|
public function ticket(int $ticketId): array |
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
/** @var Response $response */ |
148
|
|
|
$response = $this->client->get(sprintf('tickets/%s.json', $ticketId)); |
149
|
|
|
/** @var Stream $body */ |
150
|
|
|
$body = $response->getBody(); |
151
|
|
|
|
152
|
|
|
return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR); |
153
|
|
|
} catch (ClientException $e) { |
154
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|