|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Freshdesk; |
|
4
|
|
|
|
|
5
|
|
|
use Freshdesk\Exceptions\AccessDeniedException; |
|
6
|
|
|
use Freshdesk\Exceptions\ApiException; |
|
7
|
|
|
use Freshdesk\Exceptions\AuthenticationException; |
|
8
|
|
|
use Freshdesk\Exceptions\ConflictingStateException; |
|
9
|
|
|
use Freshdesk\Exceptions\MethodNotAllowedException; |
|
10
|
|
|
use Freshdesk\Exceptions\NotFoundException; |
|
11
|
|
|
use Freshdesk\Exceptions\RateLimitExceededException; |
|
12
|
|
|
use Freshdesk\Exceptions\UnsupportedAcceptHeaderException; |
|
13
|
|
|
use Freshdesk\Exceptions\UnsupportedContentTypeException; |
|
14
|
|
|
use Freshdesk\Exceptions\ValidationException; |
|
15
|
|
|
use Freshdesk\Resources\Agent; |
|
16
|
|
|
use Freshdesk\Resources\Company; |
|
17
|
|
|
use Freshdesk\Resources\Contact; |
|
18
|
|
|
use Freshdesk\Resources\Group; |
|
19
|
|
|
use Freshdesk\Resources\Ticket; |
|
20
|
|
|
use GuzzleHttp\Client; |
|
21
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class for interacting with the the Freshdesk Api (v2). |
|
25
|
|
|
* |
|
26
|
|
|
* @package Freshdesk |
|
27
|
|
|
* @category Freshdesk |
|
28
|
|
|
* @author Matthew Clarkson <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Api |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Access Agent resources |
|
34
|
|
|
* |
|
35
|
|
|
* @var Agent |
|
36
|
|
|
*/ |
|
37
|
|
|
public $agents; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Access Company resources |
|
41
|
|
|
* |
|
42
|
|
|
* @var Company |
|
43
|
|
|
*/ |
|
44
|
|
|
public $companies; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Access Contact resources |
|
48
|
|
|
* |
|
49
|
|
|
* @var Contact |
|
50
|
|
|
*/ |
|
51
|
|
|
public $contacts; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Access the Group resources |
|
55
|
|
|
* |
|
56
|
|
|
* @var Group |
|
57
|
|
|
*/ |
|
58
|
|
|
public $groups; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Access the Ticket resources |
|
62
|
|
|
* |
|
63
|
|
|
* @var Ticket |
|
64
|
|
|
*/ |
|
65
|
|
|
public $tickets; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @internal |
|
69
|
|
|
* @var Client |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $client; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var |
|
75
|
|
|
*/ |
|
76
|
|
|
private $baseUrl; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Constructs a new api instance |
|
80
|
|
|
* |
|
81
|
|
|
* @param $apiKey |
|
82
|
|
|
* @param $domain |
|
83
|
|
|
* @throws Exceptions\InvalidConfigurationException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __construct($apiKey, $domain) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!isset($apiKey)) { |
|
88
|
|
|
throw new Exceptions\InvalidConfigurationException("API key is empty."); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (!isset($domain)) { |
|
92
|
|
|
throw new Exceptions\InvalidConfigurationException("Domain is empty."); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->baseUrl = sprintf('https://%s.freshdesk.com/api/v2', $domain); |
|
96
|
|
|
|
|
97
|
|
|
$this->client = new Client([ |
|
98
|
|
|
'defaults' => [ |
|
99
|
|
|
'auth' => [$apiKey, 'X'] |
|
100
|
|
|
] |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$this->agents = new Agent($this); |
|
105
|
|
|
$this->companies = new Company($this); |
|
106
|
|
|
$this->contacts = new Contact($this); |
|
107
|
|
|
$this->groups = new Group($this); |
|
108
|
|
|
$this->tickets = new Ticket($this); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Internal method for handling all requests |
|
113
|
|
|
* |
|
114
|
|
|
* @internal |
|
115
|
|
|
* @param $method |
|
116
|
|
|
* @param $endpoint |
|
117
|
|
|
* @param array|null $data |
|
118
|
|
|
* @param array|null $query |
|
119
|
|
|
* @return mixed|null |
|
120
|
|
|
* @throws ApiException |
|
121
|
|
|
* @throws ConflictingStateException |
|
122
|
|
|
* @throws RateLimitExceededException |
|
123
|
|
|
* @throws UnsupportedContentTypeException |
|
124
|
|
|
*/ |
|
125
|
|
|
public function request($method, $endpoint, array $data = null, array $query = null) |
|
126
|
|
|
{ |
|
127
|
|
|
|
|
128
|
|
|
$options = ['json' => $data]; |
|
129
|
|
|
|
|
130
|
|
|
if (isset($query)) { |
|
131
|
|
|
$options['query'] = $query; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$url = $this->baseUrl . $endpoint; |
|
135
|
|
|
|
|
136
|
|
|
try { |
|
137
|
|
|
switch ($method) { |
|
138
|
|
|
case 'GET': |
|
139
|
|
|
return $this->client->get($url, $options)->json(); |
|
140
|
|
|
case 'POST': |
|
141
|
|
|
return $this->client->post($url, $options)->json(); |
|
142
|
|
|
case 'PUT': |
|
143
|
|
|
return $this->client->put($url, $options)->json(); |
|
144
|
|
|
case 'DELETE': |
|
145
|
|
|
return $this->client->delete($url, $options)->json(); |
|
146
|
|
|
default: |
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
} catch (RequestException $e) { |
|
150
|
|
|
throw $this->handleException($e); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param RequestException $e |
|
156
|
|
|
* @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException|NotFoundException| |
|
157
|
|
|
* RateLimitExceededException|UnsupportedContentTypeException|ValidationException |
|
158
|
|
|
*/ |
|
159
|
|
|
private function handleException(RequestException $e) |
|
160
|
|
|
{ |
|
161
|
|
|
switch ($e->getResponse()->getStatusCode()) { |
|
162
|
|
|
case 400: |
|
163
|
|
|
return new ValidationException($e); |
|
164
|
|
|
case 401: |
|
165
|
|
|
return new AuthenticationException($e); |
|
166
|
|
|
case 403: |
|
167
|
|
|
return new AccessDeniedException($e); |
|
168
|
|
|
case 404: |
|
169
|
|
|
return new NotFoundException($e); |
|
170
|
|
|
case 405: |
|
171
|
|
|
return new MethodNotAllowedException($e); |
|
172
|
|
|
case 406: |
|
173
|
|
|
return new UnsupportedAcceptHeaderException($e); |
|
174
|
|
|
case 409: |
|
175
|
|
|
return new ConflictingStateException($e); |
|
176
|
|
|
case 415: |
|
177
|
|
|
return new UnsupportedContentTypeException($e); |
|
178
|
|
|
case 429: |
|
179
|
|
|
return new RateLimitExceededException($e); |
|
180
|
|
|
default: |
|
181
|
|
|
return new ApiException($e); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |