1
<?php
2

3
declare(strict_types=1);
4

5
namespace Kreait\Firebase\Exception;
6

7
use GuzzleHttp\Exception\ConnectException;
8
use GuzzleHttp\Exception\RequestException;
9
use Kreait\Firebase\Exception\Auth\ApiConnectionFailed;
10
use Kreait\Firebase\Exception\Auth\AuthError;
11
use Kreait\Firebase\Exception\Auth\CredentialsMismatch;
12
use Kreait\Firebase\Exception\Auth\EmailExists;
13
use Kreait\Firebase\Exception\Auth\EmailNotFound;
14
use Kreait\Firebase\Exception\Auth\ExpiredOobCode;
15
use Kreait\Firebase\Exception\Auth\InvalidCustomToken;
16
use Kreait\Firebase\Exception\Auth\InvalidOobCode;
17
use Kreait\Firebase\Exception\Auth\InvalidPassword;
18
use Kreait\Firebase\Exception\Auth\MissingPassword;
19
use Kreait\Firebase\Exception\Auth\OperationNotAllowed;
20
use Kreait\Firebase\Exception\Auth\PhoneNumberExists;
21
use Kreait\Firebase\Exception\Auth\ProviderLinkFailed;
22
use Kreait\Firebase\Exception\Auth\UserDisabled;
23
use Kreait\Firebase\Exception\Auth\UserNotFound;
24
use Kreait\Firebase\Exception\Auth\WeakPassword;
25
use Kreait\Firebase\Http\ErrorResponseParser;
26
use Throwable;
27

28
/**
29
 * @internal
30
 */
31
class AuthApiExceptionConverter
32
{
33
    /** @var ErrorResponseParser */
34
    private $responseParser;
35

36
    /**
37
     * @internal
38
     */
39 12
    public function __construct()
40
    {
41 12
        $this->responseParser = new ErrorResponseParser();
42
    }
43

44
    /**
45
     * @return AuthException
46
     */
47 12
    public function convertException(Throwable $exception): FirebaseException
48
    {
49 12
        if ($exception instanceof RequestException) {
50 12
            return $this->convertGuzzleRequestException($exception);
51
        }
52

53 9
        return new AuthError($exception->getMessage(), $exception->getCode(), $exception);
54
    }
55

56 12
    private function convertGuzzleRequestException(RequestException $e): AuthException
57
    {
58 12
        $message = $e->getMessage();
59 12
        $code = $e->getCode();
60

61 12
        if ($e instanceof ConnectException) {
62 9
            return new ApiConnectionFailed('Unable to connect to the API: '.$message, $code, $e);
63
        }
64

65 12
        if ($response = $e->getResponse()) {
66 12
            $message = $this->responseParser->getErrorReasonFromResponse($response);
67 12
            $code = $response->getStatusCode();
68
        }
69

70 12
        if (\mb_stripos($message, 'credentials_mismatch') !== false) {
71 9
            return new CredentialsMismatch('Invalid custom token: The custom token corresponds to a different Firebase project.', $code, $e);
72
        }
73

74 12
        if (\mb_stripos($message, 'email_exists') !== false) {
75 9
            return new EmailExists('The email address is already in use by another account.', $code, $e);
76
        }
77

78 12
        if (\mb_stripos($message, 'email_not_found') !== false) {
79 9
            return new EmailNotFound('There is no user record corresponding to this identifier. The user may have been deleted.', $code, $e);
80
        }
81

82 12
        if (\mb_stripos($message, 'invalid_custom_token') !== false) {
83 9
            return new InvalidCustomToken('Invalid custom token: The custom token format is incorrect or the token is invalid for some reason (e.g. expired, invalid signature, etc.)', $code, $e);
84
        }
85

86 12
        if (\mb_stripos($message, 'invalid_password') !== false) {
87 9
            return new InvalidPassword('The password is invalid or the user does not have a password.', $code, $e);
88
        }
89

90 12
        if (\mb_stripos($message, 'missing_password') !== false) {
91 9
            return new MissingPassword('Missing Password', $code, $e);
92
        }
93

94 12
        if (\mb_stripos($message, 'operation_not_allowed') !== false) {
95 9
            return new OperationNotAllowed('Operation not allowed.', $code, $e);
96
        }
97

98 12
        if (\mb_stripos($message, 'user_disabled') !== false) {
99 9
            return new UserDisabled('The user account has been disabled by an administrator.', $code, $e);
100
        }
101

102 12
        if (\mb_stripos($message, 'user_not_found') !== false) {
103 12
            return new UserNotFound('There is no user record corresponding to this identifier. The user may have been deleted.', $code, $e);
104
        }
105

106 12
        if (\mb_stripos($message, 'weak_password') !== false) {
107 9
            return new WeakPassword('The password must be 6 characters long or more.', $code, $e);
108
        }
109

110 12
        if (\mb_stripos($message, 'phone_number_exists') !== false) {
111 9
            return new PhoneNumberExists('The phone number is already in use by another account.', $code, $e);
112
        }
113

114 12
        if (\mb_stripos($message, 'invalid_idp_response') !== false) {
115 9
            return new ProviderLinkFailed('The supplied auth credential is malformed or has expired.', $code, $e);
116
        }
117

118 12
        if (\mb_stripos($message, 'invalid_id_token') !== false) {
119 9
            return new ProviderLinkFailed('The user\'s credential is no longer valid. The user must sign in again.', $code, $e);
120
        }
121

122 12
        if (\mb_stripos($message, 'federated_user_id_already_linked') !== false) {
123 9
            return new ProviderLinkFailed('This credential is already associated with a different user account.', $code, $e);
124
        }
125

126 12
        if (\mb_stripos($message, 'expired_oob_code') !== false) {
127 9
            return new ExpiredOobCode('The action code has expired.', $code, $e);
128
        }
129

130 12
        if (\mb_stripos($message, 'invalid_oob_code') !== false) {
131 12
            return new InvalidOobCode('The action code is invalid. This can happen if the code is malformed, expired, or has already been used.', $code, $e);
132
        }
133

134 12
        return new AuthError($message, $code, $e);
135
    }
136
}

Read our documentation on viewing source code .

Loading