Completed
Push — master ( 73b08b...9131b2 )
by Laurens
02:28
created

handleRequestException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
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