Completed
Branch add-psr-18-support (ba8bb1)
by Steven
02:27
created

Exception::getResponseBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Stevenmaguire\Services\Trello\Exceptions;
4
5
use Exception as BaseException;
6
use Psr\Http\Client\ClientExceptionInterface;
7
8
class Exception extends BaseException
9
{
10
    /**
11
     * Response body
12
     *
13
     * @var object
14
     */
15
    protected $responseBody;
16
17
    /**
18
     * Creates local exception from http client exception, which attempts to
19
     * include response body.
20
     *
21
     * @param  ClientExceptionInterface  $clientException
22
     *
23
     * @return Exceptions\Exception
24
     */
25
    public static function fromClientException(ClientExceptionInterface $clientException)
26
    {
27
        try {
28
            if (is_callable([$clientException, 'getResponse'])) {
29
                $response = $clientException->getResponse();
0 ignored issues
show
Bug introduced by
The method getResponse() does not seem to exist on object<Psr\Http\Client\ClientExceptionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            } else {
31
                throw new Exception('ClientException does not implement getResponse');
32
            }
33
        } catch (Exception $e) {
34
            $response = null;
35
        }
36
37
        $reason = $response ? $response->getReasonPhrase() : $clientException->getMessage();
38
        $code = $response ? $response->getStatusCode() : $clientException->getCode();
39
        $body = $response ? $response->getBody() : null;
40
41
        $exception = new static($reason, $code, $clientException);
42
43
        $json = json_decode($body);
44
45
        if (json_last_error() == JSON_ERROR_NONE) {
46
            return $exception->setResponseBody($json);
47
        }
48
49
        return $exception->setResponseBody($body);
50
    }
51
52
    /**
53
     * Retrieves the response body property of exception.
54
     *
55
     * @return object
56
     */
57
    public function getResponseBody()
58
    {
59
        return $this->responseBody;
60
    }
61
62
    /**
63
     * Updates the response body property of exception.
64
     *
65
     * @param object
66
     *
67
     * @return Exception
68
     */
69
    public function setResponseBody($responseBody)
70
    {
71
        $this->responseBody = $responseBody;
72
73
        return $this;
74
    }
75
}
76