Passed
Push — master ( aeae0c...bc4001 )
by Dani
01:11
created

src/Exceptions/RESTfulException.php (1 issue)

Severity
1
<?php
2
3
namespace Postpay\Exceptions;
4
5
use Postpay\Http\Response;
6
7
class RESTfulException extends ApiException
8
{
9
    /**
10
     * @const string Error key returned.
11
     */
12
    const ERROR_KEY = 'error';
13
14
    /**
15
     * @var string API error code.
16
     */
17
    protected $errorCode;
18
19
    /**
20
     * Creates a RESTfulException.
21
     *
22
     * @param Response $response The response that threw the exception.
23
     */
24
    public function __construct(Response $response)
25
    {
26
        $this->response = $response;
27
        $this->decodedBody = $response->json();
28
        $this->errorCode = $this->get('code', 'unknown');
29
        parent::__construct($this->get('message', 'Unknown error.'));
30
    }
31
32
    /**
33
     * Checks isset and returns that or a default value.
34
     *
35
     * @param string $key
36
     * @param mixed  $default
37
     *
38
     * @return mixed
39
     */
40
    private function get($key, $default = null)
41
    {
42
        if (isset($this->decodedBody[self::ERROR_KEY][$key])) {
43
            return $this->decodedBody[self::ERROR_KEY][$key];
44
        }
45
        return $default;
46
    }
47
48
    /**
49
     * Returns API error code.
50
     *
51
     * @return string
52
     */
53
    private function getErrorCode()
0 ignored issues
show
The method getErrorCode() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
54
    {
55
        return $this->errorCode;
56
    }
57
}
58