1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Matt |
5
|
|
|
* Date: 20/04/2016 |
6
|
|
|
* Time: 2:32 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Freshdesk; |
10
|
|
|
|
11
|
|
|
use Freshdesk\Exceptions\AccessDeniedException; |
12
|
|
|
use Freshdesk\Exceptions\ApiException; |
13
|
|
|
use Freshdesk\Exceptions\AuthenticationException; |
14
|
|
|
use Freshdesk\Exceptions\ConflictingStateException; |
15
|
|
|
use Freshdesk\Exceptions\MethodNotAllowedException; |
16
|
|
|
use Freshdesk\Exceptions\NotFoundException; |
17
|
|
|
use Freshdesk\Exceptions\RateLimitExceededException; |
18
|
|
|
use Freshdesk\Exceptions\UnsupportedAcceptHeaderException; |
19
|
|
|
use Freshdesk\Exceptions\UnsupportedContentTypeException; |
20
|
|
|
use Freshdesk\Exceptions\ValidationException; |
21
|
|
|
use Freshdesk\Resources\Agent; |
22
|
|
|
use Freshdesk\Resources\BusinessHour; |
23
|
|
|
use Freshdesk\Resources\Category; |
24
|
|
|
use Freshdesk\Resources\Comment; |
25
|
|
|
use Freshdesk\Resources\Company; |
26
|
|
|
use Freshdesk\Resources\Contact; |
27
|
|
|
use Freshdesk\Resources\Conversation; |
28
|
|
|
use Freshdesk\Resources\EmailConfig; |
29
|
|
|
use Freshdesk\Resources\Forum; |
30
|
|
|
use Freshdesk\Resources\Group; |
31
|
|
|
use Freshdesk\Resources\Product; |
32
|
|
|
use Freshdesk\Resources\SLAPolicy; |
33
|
|
|
use Freshdesk\Resources\Ticket; |
34
|
|
|
use Freshdesk\Resources\TimeEntry; |
35
|
|
|
use Freshdesk\Resources\Topic; |
36
|
|
|
use GuzzleHttp\Client; |
37
|
|
|
use GuzzleHttp\Exception\RequestException; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class for interacting with the Freshdesk Api |
41
|
|
|
* |
42
|
|
|
* This is the only class that should be instantiated directly. All API resources are available |
43
|
|
|
* via the relevant public properties |
44
|
|
|
* |
45
|
|
|
* @package Api |
46
|
|
|
* @author Matthew Clarkson <[email protected]> |
47
|
|
|
*/ |
48
|
|
|
class Api |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Agent resources |
52
|
|
|
* |
53
|
|
|
* @api |
54
|
|
|
* @var Agent |
55
|
|
|
*/ |
56
|
|
|
public $agents; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Company resources |
60
|
|
|
* |
61
|
|
|
* @api |
62
|
|
|
* @var Company |
63
|
|
|
*/ |
64
|
|
|
public $companies; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Contact resources |
68
|
|
|
* |
69
|
|
|
* @api |
70
|
|
|
* @var Contact |
71
|
|
|
*/ |
72
|
|
|
public $contacts; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Group resources |
76
|
|
|
* |
77
|
|
|
* @api |
78
|
|
|
* @var Group |
79
|
|
|
*/ |
80
|
|
|
public $groups; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Ticket resources |
84
|
|
|
* |
85
|
|
|
* @api |
86
|
|
|
* @var Ticket |
87
|
|
|
*/ |
88
|
|
|
public $tickets; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* TimeEntry resources |
92
|
|
|
* |
93
|
|
|
* @api |
94
|
|
|
* @var TimeEntry |
95
|
|
|
*/ |
96
|
|
|
public $timeEntries; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Conversation resources |
100
|
|
|
* |
101
|
|
|
* @api |
102
|
|
|
* @var Conversation |
103
|
|
|
*/ |
104
|
|
|
public $conversations; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Category resources |
108
|
|
|
* |
109
|
|
|
* @api |
110
|
|
|
* @var Category |
111
|
|
|
*/ |
112
|
|
|
public $categories; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Forum resources |
116
|
|
|
* |
117
|
|
|
* @api |
118
|
|
|
* @var Forum |
119
|
|
|
*/ |
120
|
|
|
public $forums; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Topic resources |
124
|
|
|
* |
125
|
|
|
* @api |
126
|
|
|
* @var Topic |
127
|
|
|
*/ |
128
|
|
|
public $topics; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Comment resources |
132
|
|
|
* |
133
|
|
|
* @api |
134
|
|
|
* @var Comment |
135
|
|
|
*/ |
136
|
|
|
public $comments; |
137
|
|
|
|
138
|
|
|
//Admin |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Email Config resources |
142
|
|
|
* |
143
|
|
|
* @api |
144
|
|
|
* @var EmailConfig |
145
|
|
|
*/ |
146
|
|
|
public $emailConfigs; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Access Product resources |
150
|
|
|
* |
151
|
|
|
* @api |
152
|
|
|
* @var Product |
153
|
|
|
*/ |
154
|
|
|
public $products; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Business Hours resources |
158
|
|
|
* |
159
|
|
|
* @api |
160
|
|
|
* @var BusinessHour |
161
|
|
|
*/ |
162
|
|
|
public $businessHours; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* SLA Policy resources |
166
|
|
|
* |
167
|
|
|
* @api |
168
|
|
|
* @var SLAPolicy |
169
|
|
|
*/ |
170
|
|
|
public $slaPolicies; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @internal |
174
|
|
|
* @var Client |
175
|
|
|
*/ |
176
|
|
|
protected $client; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @internal |
180
|
|
|
* @var string |
181
|
|
|
*/ |
182
|
|
|
private $baseUrl; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Constructs a new api instance |
186
|
|
|
* |
187
|
|
|
* @api |
188
|
|
|
* @param $apiKey |
189
|
|
|
* @param $domain |
190
|
|
|
* @throws Exceptions\InvalidConfigurationException |
191
|
|
|
*/ |
192
|
|
|
public function __construct($apiKey, $domain) |
193
|
|
|
{ |
194
|
|
|
$this->validateConstructorArgs($apiKey, $domain); |
195
|
|
|
|
196
|
|
|
$this->baseUrl = sprintf('https://%s.freshdesk.com/api/v2', $domain); |
197
|
|
|
|
198
|
|
|
$this->client = new Client([ |
199
|
|
|
'defaults' => [ |
200
|
|
|
'auth' => [$apiKey, 'X'] |
201
|
|
|
] |
202
|
|
|
] |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$this->setupResources(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Internal method for handling requests |
211
|
|
|
* |
212
|
|
|
* @internal |
213
|
|
|
* @param $method |
214
|
|
|
* @param $endpoint |
215
|
|
|
* @param array|null $data |
216
|
|
|
* @param array|null $query |
217
|
|
|
* @return mixed|null |
218
|
|
|
* @throws ApiException |
219
|
|
|
* @throws ConflictingStateException |
220
|
|
|
* @throws RateLimitExceededException |
221
|
|
|
* @throws UnsupportedContentTypeException |
222
|
|
|
*/ |
223
|
|
|
public function request($method, $endpoint, array $data = null, array $query = null) |
224
|
|
|
{ |
225
|
|
|
$options = ['json' => $data]; |
226
|
|
|
|
227
|
|
|
if (isset($query)) { |
228
|
|
|
$options['query'] = $query; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$url = $this->baseUrl . $endpoint; |
232
|
|
|
|
233
|
|
|
return $this->performRequest($method, $url, $options); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Performs the request |
238
|
|
|
* |
239
|
|
|
* @internal |
240
|
|
|
* |
241
|
|
|
* @param $method |
242
|
|
|
* @param $url |
243
|
|
|
* @param $options |
244
|
|
|
* @return mixed|null |
245
|
|
|
* @throws AccessDeniedException |
246
|
|
|
* @throws ApiException |
247
|
|
|
* @throws AuthenticationException |
248
|
|
|
* @throws ConflictingStateException |
249
|
|
|
*/ |
250
|
|
|
private function performRequest($method, $url, $options) { |
251
|
|
|
|
252
|
|
|
try { |
253
|
|
|
switch ($method) { |
254
|
|
|
case 'GET': |
255
|
|
|
return $this->client->get($url, $options)->json(); |
256
|
|
|
case 'POST': |
257
|
|
|
return $this->client->post($url, $options)->json(); |
258
|
|
|
case 'PUT': |
259
|
|
|
return $this->client->put($url, $options)->json(); |
260
|
|
|
case 'DELETE': |
261
|
|
|
return $this->client->delete($url, $options)->json(); |
262
|
|
|
default: |
263
|
|
|
return null; |
264
|
|
|
} |
265
|
|
|
} catch (RequestException $e) { |
266
|
|
|
throw ApiException::create($e); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param $apiKey |
273
|
|
|
* @param $domain |
274
|
|
|
* @throws Exceptions\InvalidConfigurationException |
275
|
|
|
* @internal |
276
|
|
|
* |
277
|
|
|
*/ |
278
|
|
|
private function validateConstructorArgs($apiKey, $domain) |
279
|
|
|
{ |
280
|
|
|
if (!isset($apiKey)) { |
281
|
|
|
throw new Exceptions\InvalidConfigurationException("API key is empty."); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if (!isset($domain)) { |
285
|
|
|
throw new Exceptions\InvalidConfigurationException("Domain is empty."); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @internal |
291
|
|
|
*/ |
292
|
|
|
private function setupResources() |
293
|
|
|
{ |
294
|
|
|
//People |
295
|
|
|
$this->agents = new Agent($this); |
296
|
|
|
$this->companies = new Company($this); |
297
|
|
|
$this->contacts = new Contact($this); |
298
|
|
|
$this->groups = new Group($this); |
299
|
|
|
|
300
|
|
|
//Tickets |
301
|
|
|
$this->tickets = new Ticket($this); |
302
|
|
|
$this->timeEntries = new TimeEntry($this); |
303
|
|
|
$this->conversations = new Conversation($this); |
304
|
|
|
|
305
|
|
|
//Discussions |
306
|
|
|
$this->categories = new Category($this); |
307
|
|
|
$this->forums = new Forum($this); |
308
|
|
|
$this->topics = new Topic($this); |
309
|
|
|
$this->comments = new Comment($this); |
310
|
|
|
|
311
|
|
|
//Admin |
312
|
|
|
$this->products = new Product($this); |
313
|
|
|
$this->emailConfigs = new EmailConfig($this); |
314
|
|
|
$this->slaPolicies = new SLAPolicy($this); |
315
|
|
|
$this->businessHours = new BusinessHour($this); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
} |