Test Setup Failed
Push — master ( 9fa851...19bcef )
by
unknown
15:59 queued 13:07
created

PaystackApiException::fromResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Iamolayemi\Paystack\Exceptions;
4
5
use Exception;
6
7
/**
8
 * Exception thrown when Paystack API returns an error response.
9
 */
10
class PaystackApiException extends PaystackException
11
{
12
    protected ?string $endpoint = null;
13
14
    /** @var array<string, mixed>|null */
15
    protected ?array $response = null;
16
17
    /**
18
     * @param  array<string, mixed>|null  $response
19
     */
20
    public function __construct(
21
        string $message = '',
22
        int $code = 0,
23
        ?string $endpoint = null,
24
        ?array $response = null,
25
        ?Exception $previous = null
26
    ) {
27
        parent::__construct($message, $code, $previous);
28
        $this->endpoint = $endpoint;
29
        $this->response = $response;
30
    }
31
32
    public function getEndpoint(): ?string
33
    {
34
        return $this->endpoint;
35
    }
36
37
    /**
38
     * @return array<string, mixed>|null
39
     */
40
    public function getResponse(): ?array
41
    {
42
        return $this->response;
43
    }
44
45
    /**
46
     * @param  array<string, mixed>  $response
47
     */
48
    public static function fromResponse(array $response, string $endpoint): self
49
    {
50
        $message = $response['message'] ?? 'Unknown API error';
51
        $code = $response['status'] ?? 0;
52
53
        return new self($message, $code, $endpoint, $response);
54
    }
55
}
56