Test Failed
Pull Request — master (#10)
by
unknown
14:44
created

ApiException   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 62.96%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 27
c 1
b 0
f 1
dl 0
loc 71
ccs 17
cts 27
cp 0.6296
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequest() 0 3 1
A fromResponse() 0 25 6
A __construct() 0 10 1
A getStatusCode() 0 3 1
A getRawBody() 0 3 2
A getResponse() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Level23\Dynadot\Exception;
6
7
use Exception;
8
use Throwable;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
class ApiException extends Exception
13
{
14
    private RequestInterface $request;
15
    private ?ResponseInterface $response;
16
17 2
    public function __construct(
18
        string $message,
19
        int $code,
20
        RequestInterface $request,
21
        ?ResponseInterface $response = null,
22
        ?Throwable $previous = null
23
    ) {
24 2
        parent::__construct($message, $code, $previous);
25 2
        $this->request  = $request;
26 2
        $this->response = $response;
27
    }
28
29
    public function getRequest(): RequestInterface
30
    {
31
        return $this->request;
32
    }
33
34
    public function getResponse(): ?ResponseInterface
35
    {
36
        return $this->response;
37
    }
38
39
    /**
40
     * HTTP status code from the response, if available.
41
     */
42
    public function getStatusCode(): ?int
43
    {
44
        return $this->response?->getStatusCode();
45
    }
46
47
    /**
48
     * Raw response body as string, if available.
49
     */
50
    public function getRawBody(): ?string
51
    {
52
        return $this->response ? (string) $this->response->getBody() : null;
53
    }
54
55
    /**
56
     * Helper to build the right subclass from a 4xx/5xx response.
57
     */
58 2
    public static function fromResponse(
59
        RequestInterface $req,
60
        ResponseInterface $res,
61
        ?Throwable $prev = null
62
    ): self {
63 2
        $body = (string) $res->getBody();
64 2
        $data = json_decode($body, true);
65 2
        $code = $data['code'] ?? $res->getStatusCode();
66
        
67 2
        if (json_last_error() !== JSON_ERROR_NONE) {
68 1
            $msg = 'Invalid JSON in error response: ' . json_last_error_msg();
69
        } else {
70 1
            $msg = $data['error']['description'] ?? $data['message'] ?? $res->getReasonPhrase();
71
        }
72
73
        switch ($code) {
74 2
            case 400:
75 1
                return new ValidationException($msg, $code, $req, $res, $prev);
76 1
            case 401:
77 1
            case 403:
78
                return new AuthenticationException($msg, $code, $req, $res, $prev);
79 1
            case 404:
80 1
                return new NotFoundException($msg, $code, $req, $res, $prev);
81
            default:
82
                return new self($msg, $code, $req, $res, $prev);
83
        }
84
    }
85
}
86