Completed
Branch add-psr-18-support (358945)
by Steven
01:42 queued 14s
created

Exception::setResponseBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 Exception
24
     */
25 8
    public static function fromClientException(ClientExceptionInterface $clientException)
26
    {
27 8
        $response = static::getResponseFromClientException($clientException);
28 8
        $reason = $response ? $response->getReasonPhrase() : $clientException->getMessage();
0 ignored issues
show
Bug introduced by
The method getReasonPhrase() does not seem to exist on object<Psr\Http\Message\RequestInterface>.

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...
29 8
        $code = $response ? $response->getStatusCode() : $clientException->getCode();
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not seem to exist on object<Psr\Http\Message\RequestInterface>.

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 8
        $body = $response ? $response->getBody() : null;
31
32 8
        $exception = new static($reason, $code, $clientException);
33
34 8
        $json = json_decode($body);
35
36 8
        if (json_last_error() == JSON_ERROR_NONE) {
37 4
            return $exception->setResponseBody($json);
38
        }
39
40 4
        return $exception->setResponseBody($body);
41
    }
42
43
    /**
44
     * Retrieves the response body property of exception.
45
     *
46
     * @return object
47
     */
48 4
    public function getResponseBody()
49
    {
50 4
        return $this->responseBody;
51
    }
52
53
    /**
54
     * Attempts to extract an http response message from the given client exception.
55
     *
56
     * @param  ClientExceptionInterface $clientException
57
     *
58
     * @return \Psr\Http\Message\RequestInterface|null
59
     */
60 8
    public static function getResponseFromClientException(ClientExceptionInterface $clientException)
61
    {
62
        try {
63 8
            if (is_callable([$clientException, 'getResponse'])) {
64 4
                $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...
65
            } else {
66 8
                throw new Exception('ClientException does not implement getResponse');
67
            }
68 4
        } catch (Exception $e) {
69 4
            $response = null;
70
        }
71
72 8
        return $response;
73
    }
74
75
    /**
76
     * Updates the response body property of exception.
77
     *
78
     * @param object
79
     *
80
     * @return Exception
81
     */
82 8
    public function setResponseBody($responseBody)
83
    {
84 8
        $this->responseBody = $responseBody;
85
86 8
        return $this;
87
    }
88
}
89