getSinchExceptionForBadRequest()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
/*
3
 * This file is part of the FreshSinchBundle
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\SinchBundle\Service;
14
15
use Fresh\SinchBundle\Exception\BadRequest\SinchInvalidRequestException;
16
use Fresh\SinchBundle\Exception\BadRequest\SinchMissingParameterException;
17
use Fresh\SinchBundle\Exception\BadRequest\SinchParameterValidationException;
18
use Fresh\SinchBundle\Exception\Forbidden\SinchForbiddenRequestException;
19
use Fresh\SinchBundle\Exception\Forbidden\SinchInvalidAuthorizationSchemeException;
20
use Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException;
21
use Fresh\SinchBundle\Exception\InternalServerError\SinchInternalErrorException;
22
use Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException;
23
use Fresh\SinchBundle\Exception\SinchException;
24
use Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException;
25
use Fresh\SinchBundle\Helper\SinchErrorCode;
26
use GuzzleHttp\Exception\ClientException;
27
use Symfony\Component\HttpFoundation\Response;
28
29
/**
30
 * SinchExceptionResolver.
31
 *
32
 * @author Artem Henvald <[email protected]>
33
 */
34
class SinchExceptionResolver
35
{
36
    /**
37
     * @param ClientException $e
38
     *
39
     * @return \Exception|SinchException
40
     */
41
    public static function createAppropriateSinchException(ClientException $e): \Exception
42
    {
43
        $response = \json_decode($e->getResponse()->getBody()->getContents(), true);
44
        $responseStatusCode = $e->getCode();
45
46
        $errorCode = (int) $response['errorCode'];
47
        $errorMessage = $response['message'];
48
49
        $exception = null;
50
51
        switch ($responseStatusCode) {
52
            case Response::HTTP_BAD_REQUEST:
53
                $exception = self::getSinchExceptionForBadRequest($errorCode, $errorMessage);
54
55
                break;
56
            case Response::HTTP_UNAUTHORIZED:
57
                $exception = self::getSinchExceptionForUnauthorized($errorCode, $errorMessage);
58
59
                break;
60
            case Response::HTTP_PAYMENT_REQUIRED:
61
                $exception = self::getSinchExceptionForPaymentRequired($errorCode, $errorMessage);
62
63
                break;
64
            case Response::HTTP_FORBIDDEN:
65
                $exception = self::getSinchExceptionForForbidden($errorCode, $errorMessage);
66
67
                break;
68
            case Response::HTTP_INTERNAL_SERVER_ERROR:
69
                $exception = self::getSinchExceptionForInternalServerError($errorCode, $errorMessage);
70
71
                break;
72
        }
73
74
        if (null === $exception) {
75
            $exception = new \Exception('Unknown Sinch Error Code');
76
        }
77
78
        return $exception;
79
    }
80
81
    /**
82
     * @param int    $errorCode
83
     * @param string $errorMessage
84
     *
85
     * @return SinchException|null
86
     */
87
    private static function getSinchExceptionForBadRequest(int $errorCode, string $errorMessage): ?SinchException
88
    {
89
        $exception = null;
90
91
        switch ($errorCode) {
92
            case SinchErrorCode::PARAMETER_VALIDATION:
93
                $exception = new SinchParameterValidationException($errorMessage);
94
95
                break;
96
            case SinchErrorCode::MISSING_PARAMETER:
97
                $exception = new SinchMissingParameterException($errorMessage);
98
99
                break;
100
            case SinchErrorCode::INVALID_REQUEST:
101
                $exception = new SinchInvalidRequestException($errorMessage);
102
103
                break;
104
        }
105
106
        return $exception;
107
    }
108
109
    /**
110
     * @param int    $errorCode
111
     * @param string $errorMessage
112
     *
113
     * @return SinchException|null
114
     */
115
    private static function getSinchExceptionForUnauthorized(int $errorCode, string $errorMessage): ?SinchException
116
    {
117
        $exception = null;
118
119
        if (SinchErrorCode::ILLEGAL_AUTHORIZATION_HEADER === $errorCode) {
120
            $exception = new SinchIllegalAuthorizationHeaderException($errorMessage);
121
        }
122
123
        return $exception;
124
    }
125
126
    /**
127
     * Sinch returns 402 code when application runs out of money.
128
     *
129
     * @param int    $errorCode
130
     * @param string $errorMessage
131
     *
132
     * @return SinchException|null
133
     */
134
    private static function getSinchExceptionForPaymentRequired(int $errorCode, string $errorMessage): ?SinchException
135
    {
136
        $exception = null;
137
138
        if (SinchErrorCode::THERE_IS_NOT_ENOUGH_FUNDS_TO_SEND_THE_MESSAGE === $errorCode) {
139
            $exception = new SinchPaymentRequiredException($errorMessage);
140
        }
141
142
        return $exception;
143
    }
144
145
    /**
146
     * @param int    $errorCode
147
     * @param string $errorMessage
148
     *
149
     * @return SinchException|null
150
     */
151
    private static function getSinchExceptionForForbidden(int $errorCode, string $errorMessage): ?SinchException
152
    {
153
        $exception = null;
154
155
        switch ($errorCode) {
156
            case SinchErrorCode::FORBIDDEN_REQUEST:
157
                $exception = new SinchForbiddenRequestException($errorMessage);
158
159
                break;
160
            case SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD:
161
                $exception = new SinchInvalidAuthorizationSchemeException($errorMessage);
162
163
                break;
164
            case SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT:
165
            case SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS:
166
                $exception = new SinchNoVerifiedPhoneNumberException($errorMessage);
167
168
                break;
169
        }
170
171
        return $exception;
172
    }
173
174
    /**
175
     * @param int    $errorCode
176
     * @param string $errorMessage
177
     *
178
     * @return SinchException|null
179
     */
180
    private static function getSinchExceptionForInternalServerError(int $errorCode, string $errorMessage): ?SinchException
181
    {
182
        $exception = null;
183
184
        if (SinchErrorCode::INTERNAL_ERROR === $errorCode) {
185
            $exception = new SinchInternalErrorException($errorMessage);
186
        }
187
188
        return $exception;
189
    }
190
191
    // endregion
192
}
193