1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client\Exception; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
9
|
|
|
use LauLamanApps\IzettleApi\Client\Exception\ClientException as IzettleClientException; |
10
|
|
|
use LauLamanApps\IzettleApi\Client\Exception\InvalidClient\InvalidClientIdException; |
11
|
|
|
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrant\InvalidUsernameOrPasswordException; |
12
|
|
|
use LauLamanApps\IzettleApi\Client\Exception\InvalidGrant\TooManyFailedAttemptsException; |
13
|
|
|
|
14
|
|
|
final class GuzzleClientExceptionHandler |
15
|
|
|
{ |
16
|
9 |
|
public static function handleClientException(ClientException $exception): void |
17
|
|
|
{ |
18
|
9 |
|
switch ($exception->getCode()) { |
19
|
9 |
|
case 400: |
20
|
8 |
|
self::handleClient400Exception($exception); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
throw new IzettleClientException($exception->getMessage()); |
24
|
|
|
} |
25
|
|
|
|
26
|
3 |
|
public static function handleRequestException(RequestException $exception): void |
27
|
|
|
{ |
28
|
3 |
|
$responseData = json_decode($exception->getResponse()->getBody()->getContents(), true); |
29
|
3 |
|
switch ($exception->getCode()) { |
30
|
3 |
|
case 404: |
31
|
2 |
|
throw new NotFoundException($responseData['developerMessage']); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
throw new IzettleClientException($exception->getMessage()); |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
private static function handleClient400Exception(ClientException $exception): void |
38
|
|
|
{ |
39
|
8 |
|
$responseData = json_decode($exception->getResponse()->getBody()->getContents(), true); |
40
|
8 |
|
switch ($responseData['error']) { |
41
|
8 |
|
case 'invalid_grant': |
42
|
4 |
|
self::handleInvalidGrantException($responseData); |
43
|
4 |
|
case 'invalid_client': |
44
|
2 |
|
self::handleInvalidClientException($responseData); |
45
|
2 |
|
case 'unauthorized_client': |
46
|
1 |
|
throw new InvalidClientException($responseData['error_description']); |
47
|
|
|
default: |
48
|
1 |
|
throw new InvalidClientException($responseData['error_description']); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
private static function handleInvalidGrantException(array $responseData): void |
53
|
|
|
{ |
54
|
4 |
|
switch ($responseData['error_description']) { |
55
|
4 |
|
case 'INCORRECT_PASSWORD_OR_USERNAME': |
56
|
2 |
|
throw new InvalidUsernameOrPasswordException(); |
57
|
2 |
|
case 'TOO_MANY_FAILED_ATTEMPTS': |
58
|
1 |
|
throw new TooManyFailedAttemptsException(); |
59
|
|
|
default: |
60
|
1 |
|
throw new InvalidGrantException($responseData['error_description']); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
private static function handleInvalidClientException(array $responseData): void |
65
|
|
|
{ |
66
|
2 |
|
switch ($responseData['error_description']) { |
67
|
2 |
|
case 'Invalid client_id': |
68
|
1 |
|
throw new InvalidClientIdException(); |
69
|
|
|
default: |
70
|
1 |
|
throw new InvalidClientException($responseData['error_description']); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|