-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathFailedToSignIn.php
More file actions
55 lines (43 loc) · 1.33 KB
/
FailedToSignIn.php
File metadata and controls
55 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Auth\SignIn;
use Beste\Json;
use InvalidArgumentException;
use Kreait\Firebase\Auth\SignIn;
use Kreait\Firebase\Exception\AuthException;
use Kreait\Firebase\Exception\RuntimeException;
use Psr\Http\Message\ResponseInterface;
use Throwable;
final class FailedToSignIn extends RuntimeException implements AuthException
{
private ?SignIn $action = null;
private ?ResponseInterface $response = null;
public static function withActionAndResponse(SignIn $action, ResponseInterface $response): self
{
$fallbackMessage = 'Failed to sign in';
try {
$message = Json::decode((string) $response->getBody(), true)['error']['message'] ?? $fallbackMessage;
} catch (InvalidArgumentException) {
$message = $fallbackMessage;
}
$error = new self($message);
$error->action = $action;
$error->response = $response;
return $error;
}
public static function fromPrevious(Throwable $e): self
{
return new self(
message: 'Sign in failed: '.$e->getMessage(),
previous: $e
);
}
public function action(): ?SignIn
{
return $this->action;
}
public function response(): ?ResponseInterface
{
return $this->response;
}
}