1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Matt |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Freshdesk\Exceptions; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use GuzzleHttp\Exception\RequestException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* General Exception |
14
|
|
|
* |
15
|
|
|
* Thrown when the Freshdesk API returns an HTTP error code that isn't handled by other exceptions |
16
|
|
|
* |
17
|
|
|
* @package Exceptions |
18
|
|
|
* @author Matthew Clarkson <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ApiException extends Exception |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
* @param RequestException $e |
26
|
|
|
* @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException| |
27
|
|
|
* MethodNotAllowedException|NotFoundException|RateLimitExceededException|UnsupportedAcceptHeaderException| |
28
|
|
|
* UnsupportedContentTypeException|ValidationException |
29
|
|
|
*/ |
30
|
|
|
static function create(RequestException $e) { |
|
|
|
|
31
|
|
|
switch ($e->getResponse()->getStatusCode()) { |
32
|
|
|
case 400: |
33
|
|
|
return new ValidationException($e); |
34
|
|
|
case 401: |
35
|
|
|
return new AuthenticationException($e); |
36
|
|
|
case 403: |
37
|
|
|
return new AccessDeniedException($e); |
38
|
|
|
case 404: |
39
|
|
|
return new NotFoundException($e); |
40
|
|
|
case 405: |
41
|
|
|
return new MethodNotAllowedException($e); |
42
|
|
|
case 406: |
43
|
|
|
return new UnsupportedAcceptHeaderException($e); |
44
|
|
|
case 409: |
45
|
|
|
return new ConflictingStateException($e); |
46
|
|
|
case 415: |
47
|
|
|
return new UnsupportedContentTypeException($e); |
48
|
|
|
case 429: |
49
|
|
|
return new RateLimitExceededException($e); |
50
|
|
|
default: |
51
|
|
|
return new ApiException($e); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var RequestException |
57
|
|
|
* @internal |
58
|
|
|
*/ |
59
|
|
|
private $exception; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the Request Exception |
63
|
|
|
* |
64
|
|
|
* A Guzzle Request Exception is returned |
65
|
|
|
* |
66
|
|
|
* @return RequestException |
67
|
|
|
*/ |
68
|
|
|
public function getRequestException() |
69
|
|
|
{ |
70
|
|
|
return $this->exception; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Exception constructor |
75
|
|
|
* |
76
|
|
|
* Constructs a new exception. |
77
|
|
|
* |
78
|
|
|
* @param RequestException $e |
79
|
|
|
* @internal |
80
|
|
|
*/ |
81
|
|
|
public function __construct(RequestException $e) |
82
|
|
|
{ |
83
|
|
|
$this->exception = $e; |
84
|
|
|
parent::__construct(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.